Ask Your Question
1

Create a mat ROI for each contour blob?

asked 2015-03-30 07:22:17 -0600

215 gravatar image

updated 2015-03-30 07:25:38 -0600

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() );
    vector<float>radius( 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 flag offensive close merge delete

Comments

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

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-03-30 07:50:54 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
4

answered 2015-03-30 08:00:04 -0600

Siegfried gravatar image

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);
}
edit flag offensive delete link more

Comments

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?

215 gravatar image215 ( 2015-03-30 08:01:46 -0600 )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 ;-)

theodore gravatar imagetheodore ( 2015-03-30 08:21:45 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-03-30 07:22:17 -0600

Seen: 4,418 times

Last updated: Mar 30 '15