Ask Your Question

Extracting an image portion opencv C + +

asked 2014-02-18 06:10:59 -0500

zowpro gravatar image

updated 2014-02-22 06:11:32 -0500

berak gravatar image

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:image description

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 flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2014-02-18 10:56:19 -0500

Haris gravatar image

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

edit flag offensive delete link more

answered 2014-02-22 05:43:14 -0500

zowpro gravatar image

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

edit flag offensive delete link more


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

Haris gravatar imageHaris ( 2014-02-22 05:47:07 -0500 )edit

Ok, I mean surface area of the contour

zowpro gravatar imagezowpro ( 2014-02-22 09:29:59 -0500 )edit

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

Haris gravatar imageHaris ( 2014-02-22 12:15:27 -0500 )edit
Login/Signup to Answer

Question Tools


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

Seen: 1,128 times

Last updated: Feb 22 '14