Ask Your Question
1

Remove contours/bounding rect with an area < n

asked 2014-04-08 20:08:36 -0600

Luek gravatar image

updated 2020-10-23 09:35:20 -0600

How can I iterate through a list of contours and remove any contours that don't have an area of at least n?

if contour(i).area < 500 {
    contour.remove(i)
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2014-04-08 22:09:22 -0600

Haris gravatar image

updated 2014-04-08 23:39:52 -0600

Suppose you already found contours,

Then using the code,

 double min_area=500; // area threshold
 for( int i = 0; i< contours.size(); i++ ) // iterate through each contour.
 {
   double area=contourArea( contours[i],false);  //  Find the area of contour
   if(area<min_area)
    contours.erase(contours.begin() + i);
 }

you can iterate through contours and remove. I hope it will work.

edit flag offensive delete link more

Comments

how can I do it with 2 threshold? i tried using || at the if and it stops working.

active92 gravatar imageactive92 ( 2015-08-01 14:01:47 -0600 )edit

Question Tools

Stats

Asked: 2014-04-08 20:08:36 -0600

Seen: 6,694 times

Last updated: Apr 08 '14