Ask Your Question

how to Ignore bounding box inside bounding box

asked 2014-03-31 07:23:55 -0600

Amit gravatar image

updated 2020-10-29 17:19:00 -0600

when i draw the bounding box with centriod, i got the bounding box inside bounding box like this

bounding box

Actually i want to ignore the inner bounding box in all my bounding box.

I am using below code with ignoring all circles. link

edit retag flag offensive close merge delete



Just cross reference the corners with eachother. If the topleft corner of the smaller object has an x and y larger then topleft of the larger object AND it has an bottomright corner with an x and y value that is smaller than the x and y of the larger object, then you can simply remove the detection. Seems pretty feasible to me.

StevenPuttemans gravatar imageStevenPuttemans ( 2014-03-31 07:33:45 -0600 )edit

Ah confused, can you provide some link or code(rough code).

Amit gravatar imageAmit ( 2014-03-31 23:48:30 -0600 )edit

I find it very strange that you cannot solve this yourself or by the directions given. This is a basic mathematical problem... However @Haris gave you a good indication on how to solve your problem also if you do not want to do it during postprocessing!

StevenPuttemans gravatar imageStevenPuttemans ( 2014-04-01 01:35:39 -0600 )edit

2 answers

Sort by ยป oldest newest most voted

answered 2014-04-01 00:15:16 -0600

Haris gravatar image

updated 2014-04-01 00:16:46 -0600

You can change findcontour() mode parameter to CV_RETR_EXTERNAL which will retrieves only the extreme outer contours, and sets hierarchy[i][2]=hierarchy[i][3]=-1 for all the contours. That is CV_RETR_EXTERNAL will exclude all child contour(contour inside contour).

And if your code need to extract all contour(including child) you can iterate through only outer contour(first hierarchy level) using below code,

for( int i = 0; i< contours.size(); i=hierarchy[i][0] ) // iterate through outer  contour exclude child

Also see this question, might be helpful.

edit flag offensive delete link more

answered 2014-04-02 23:45:28 -0600

Amit gravatar image

Well, thanks haris for a new concept(really useful), but i use Steven code ie

    int cmin= 50;
    int cmax= 1000;        
    vector<vector<Point> >::iterator itc=contours.begin();
    while (itc!=contours.end()) {
            if (itc->size() < cmin || itc->size() > cmax){
                itc= contours.erase(itc);}
            vector<Point> pts = *itc;
            Mat pointsMatrix = Mat(pts);
            Scalar color( 0, 255, 0 );          
            Rect r0= boundingRect(pointsMatrix);

above sample code will solve my issue. But @Haris and @steven thanks for immediate reply & good luck

edit flag offensive delete link more

Question Tools


Asked: 2014-03-31 07:23:55 -0600

Seen: 6,446 times

Last updated: Apr 02 '14