Ask Your Question
0

Find squares and save the region of interest

asked 2018-05-13 05:19:50 -0600

dura gravatar image

updated 2018-05-13 05:24:32 -0600

berak gravatar image

Here is a part of the code I got from opencv sample code:https://github.com/opencv/opencv/blob/master/samples/cpp/squares.cpp

static void drawSquares( Mat& image, const vector<vector<Point> >& squares )
{
   //cout << "square size " << squares.size();

    for( size_t i = 0; i < squares.size(); i++ )
    {
        const Point* p = &squares[i][0];

        int n = (int)squares[i].size();
        //cout << "square size  " << n;

        //dont detect the border
        if (p-> x > 3 && p->y > 3){
        //  printf("x value %u y value %u\n",p-> x,p-> y);
          polylines(image, &p, &n, 1, true, Scalar(255,255,0), 3, LINE_AA);
        }

    }
    imshow(wndname, image);
}

I want to save (imwrite) only the detected squares image description out of the whole imageimage description

I tried printing out x and y value of p but it only gives me the only a corner pixel value of the square. Could anyone please help how to save the squares only?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-05-13 05:32:12 -0600

berak gravatar image

given, you have a vector< vector<Point> >& squares, you could try like this:

for (size_t i=0; i<squares.size(); i++) {
    Rect r = boundingRect(squares[i]);
    Mat crop = image(r);
    imwrite(format("rect_%d.png",i), crop);
}
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-05-13 05:19:50 -0600

Seen: 130 times

Last updated: May 13 '18