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;}
I have written the above code to draw the convex hull of the biggest contour using OpenCV 2.4.9 using microsoft visual studio 2010 express. The code complies and executes without any error, draws the biggest contour successfully, but CANNOT DISPLAY THE CONTOUR.
Please be informed that I used C api so far, and now trying to convert to C++. So, I am new to use openCV with C++. Your advice to make the program work for drawing the convex hull would be greatly appreciated.