1 | initial version |
I think you already found contour and Convexhull of your source image,
Next,
Draw your contour to a new Mat with fill option.
Draw Convexhull to new Mat with fill option.
Now perform bitwise_xor with above two image.
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
cvtColor(src,tmp,CV_BGR2GRAY);
threshold(tmp,thr,100,255,THRESH_BINARY_INV);
//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
if(a>largest_area){
largest_area=a;
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;
Source:
Filled edge:
Filled envelop:
Result: