0
int main()
{
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
int largest_area=0;
int largest_contour_index=0;
OriginalImage = imread("C:\\Data Drive\\opencv Projects\\testwithC++\\Plant001-9\\SideView90\\Day_021.png",CV_LOAD_IMAGE_GRAYSCALE);
BackgroundImage = imread("C:\\Data Drive\\opencv Projects\\testwithC++\\Plant001-9\\SideView90\\Day_001.png",CV_LOAD_IMAGE_GRAYSCALE);
absdiff(OriginalImage,BackgroundImage,GrayImage);
threshold(GrayImage,Binary,80,255,CV_THRESH_BINARY);
namedWindow("OriginalImage", WINDOW_NORMAL);
imshow("OriginalImage", OriginalImage);
namedWindow("BackgroundImage", WINDOW_NORMAL);
imshow("BackgroundImage", BackgroundImage);
namedWindow("GrayImage", WINDOW_NORMAL);
imshow("GrayImage", GrayImage);
namedWindow("Binary", WINDOW_NORMAL);
imshow("Binary", Binary);
ImageROI = Binary(Rect(300,0,Binary.size().width-600,Binary.size().height));
namedWindow("ImageROI", WINDOW_NORMAL);
imshow("ImageROI", ImageROI);
dilate(ImageROI,BinaryMorph,Mat(),Point(-1,-1),2);
namedWindow("BinaryMorph", WINDOW_NORMAL);
imshow("BinaryMorph", BinaryMorph);
findContours(BinaryMorph, contours, hierarchy, RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
for(int i = 0; i< contours.size(); i++)
{
double a=contourArea(contours[i],false);
if(a>largest_area)
{
largest_area=a;
largest_contour_index=i;
}
}
Contour = Mat(ImageROI.size().width,ImageROI.size().height,CV_8UC1,Scalar::all(0));
drawContours(Contour, contours,largest_contour_index,Scalar(255),CV_FILLED, 8,hierarchy);
vector<Point>hull;
convexHull(contours[largest_contour_index],hull,CV_CLOCKWISE,true);
drawContours(Contour, Mat(hull),largest_contour_index,Scalar(255),3, 8);
namedWindow("Contour", WINDOW_NORMAL);
imshow("Contour", Contour);
OriginalImage.release();
BackgroundImage.release();
GrayImage.release();
Binary.release();
BinaryMorph.release();
ImageROI.release();
Contour.release();
waitKey(0);
return 0;}
我已經寫上面的代碼來繪製使用的OpenCV 2.4.9使用Microsoft Visual Studio 2010所明示的最大輪廓的凸包的最大輪廓的凸包。該代碼遵循並執行沒有任何錯誤,成功繪製最大輪廓,但無法顯示輪廓。拉絲使用的OpenCV C++
請注意,我使用C api到目前爲止,現在試圖轉換爲C++。所以,我對於使用openCV和C++來說是新的。你的意見,使程序工作繪製凸包將不勝感激。
非常感謝您的所有有用的提示!我很感激您的時間來修改我的代碼。但是,這個問題並沒有解決,因爲我在下面的代碼中得到了編譯錯誤:drawContours(contour,vector> {hull},0 .......它被加下劃線爲紅色並且表示,type名稱不允許在這裏,請幫助我 –
你可能沒有啓用C++ 11,只需要:'vector> tmp; tmp.push_back(hull); drawContours(contour,tmp,0,Scalar 128),3);' –
Miki
你好,我提出了建議的改變,編譯錯誤消失了,但凸起不像以前那樣顯示,而且我想用紅色的凸面白色顯示輪廓,而這個背景是黑色的,請幫助我,我被困在這裏,因爲沒有顯示凸字 –