Extracting an image portion opencv C + +

Hello, I am trying to create a project that extract parts of an image, knowing that I have determined the contour points and I determined points related envelope and now I want to calculate the surface of the parties are between the edge points and the points of the envelope.

Here is an example:

I found on the net a function that retrieves rectangles cvSetImageROI but the problem parts that I want to extract have different shapes. In short, help me!

edit retag close merge delete

Sort by ยป oldest newest most voted

Thanks but i want calculate the surface inside the contour. I used findcontour and convexhull with a matrices.

more

You should post this as a comment...and you mean surface area or perimeter ?

( 2014-02-22 05:47:07 -0600 )edit

Ok, I mean surface area of the contour

( 2014-02-22 09:29:59 -0600 )edit

Either use contourArea() or directly count non-zero pixel.

( 2014-02-22 12:15:27 -0600 )edit

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

Next,

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;
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:

more

Stats

Asked: 2014-02-18 06:10:59 -0600

Seen: 1,316 times

Last updated: Feb 22 '14