Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I think you already found contour and Convexhull of your source image,


  1. Draw your contour to a new Mat with fill option.

  2. Draw Convexhull to new Mat with fill option.

  3. Now perform bitwise_xor with above two image.

  4. countNonZero of the above result will give you the total number of pixel between edge points and envelop .

Here is a simple example for the above steps

Mat tmp,thr;
Mat src=imread("src.jpg",1); //Your processed  image

//Find biggest contour to segment exact object
vector< vector <Point> > contours; // Vector for storing contour
vector< Vec4i > hierarchy;
int largest_contour_index=0;
int largest_area=0;
Mat edge(src.rows,src.cols,CV_8UC1,Scalar::all(0)); //create destination image
Mat envelop(src.rows,src.cols,CV_8UC1,Scalar::all(0)); //create destination image
Mat dst(src.rows,src.cols,CV_8UC1,Scalar::all(0)); //create destination image

findContours( thr, contours, hierarchy,CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE ); // Find the contours in the image
 for( int i = 0; i< contours.size(); i++ ) // iterate through each contour.
   double a=contourArea( contours[i],false);  //  Find the area of contour
   largest_contour_index=i;                //Store the index of largest contour
 //Draw edge with filled
 drawContours( edge,contours, largest_contour_index, Scalar(255,255,255),CV_FILLED, 8, hierarchy ); // Draw the largest contour using previously stored index.
 //Draw envelop with filled option
 vector<vector<Point> >hull(1);
 convexHull(contours[largest_contour_index],  hull[0],false,true );
 drawContours( envelop, hull, 0, Scalar(255), CV_FILLED, 8, vector<Vec4i>(), 0, Point() );
 // Exor
 bitwise_xor(edge, envelop, dst);
 cout<<"No: pixel = "<< countNonZero(dst)<<endl;


image description

Filled edge:

image description

Filled envelop:

image description


image description