# Create a mat ROI for each contour blob?

How would one create multiple ROI Mat based on a

vector <vector < point >> contours.


I have this function

vector<vector<Point> > MarkCountours(Mat binaryImg)
{
vector<vector<Point> > contours;
Mat test = binaryImg.clone();
vector<Vec4i> hierarchy;
RNG rng(12345);
findContours( test, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
vector<vector<Point> > contours_poly( contours.size() );
vector<Rect> boundRect( contours.size() );
vector<Point2f>center( contours.size() );
for( int i = 0; i < contours.size(); i++ )
{
approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
boundRect[i] = boundingRect( Mat(contours_poly[i]) );
}
Mat drawing = Mat::zeros( binaryImg.size(), CV_8UC3 );
for( int i = 0; i< contours.size(); i++ )
{
Scalar color = Scalar(255,0,255);
drawContours( drawing, contours_poly, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
rectangle( drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0 );
}

return contours;
}


This draws rectangle on the binary image, based on contours, but how do i based on those countours create multiple Mat which consist of ROI where the contours are in interest..

edit retag close merge delete

A simpler way to detect the bounding rect of the contour is the boundingRect. If you want to create more images with just the wanted contour, you can read this

( 2015-03-30 07:50:54 -0500 )edit

Sort by ยป oldest newest most voted

Since you already compute the bounding box of the contours, you can do something like this

std::vector<cv::Mat> contour_rois;
for(int i = 0; boundRect.size(); i++)
{
cv::Mat roi( binaryImg, boundRect[i]);
contour_rois.push_back(roi);
}

more

Ok.. I use the Binary image to find contours, so i can extract features from the original.. Is it possible to extracts ROI's based on the original use the contours I found to extract ROI from the original picture?

( 2015-03-30 08:01:46 -0500 )edit
3

if the binary image is the same size as the original image then you can replace the binaryImg in the above code with the original image ;-)

( 2015-03-30 08:21:45 -0500 )edit