OpenCV; sort the <Rect> vector and delete specific records

I have this problem, I can not envelop my head. I am trying to detect and track something in a video. Therefore, I use features like GaussianBlur(), threshold(), findContours(),.

findContours()gives me a path vector that later converts to bounding rectangles. So far, so good.

Now I need the bounding rectangle vector to be sorted by size (area)and contain only rectangles that are not enclosed in another rectangle.

I tried to draw a small sketch for a better understanding, click here for image .

So, I am looking for what #8is the first entry followed by #1, #3,.... Entries such as #2,#4, #9, #10 and #11should be deleted.

I understand that vectors are not ideal for sorting and deleting. So I tried to copy the vector to the list as follows:

std::list<Rect> sorted_list(boundRect_temp.begin(), boundRect_temp.end());

But now I can’t access member variables such as scope. The fact is that the algorithm should not be too time-consuming, so I'm looking for a good solution. Maybe there is a function for this already?

+4
source share
2 answers

1) , findContours() CV_RETR_EXTERNAL. , , , . , , .

2) , , . . , , .

3) , . , , :

vector < Rect > nonIntersect;
for(unsigned int i=0; i < sortedBoxes.size(); i++) {
    bool toAdd = true;
    Point center = (sortedBoxes[i].tl()+sortedBoxes[i].br())*0.5;
    for(unsigned int j=0; j < nonIntersect.size(); j++)
        if (nonIntersect[j].contains(center)) {
            toAdd = false;
            break;
        }
    if (toAdd)
        nonIntersect.push back(sortedBoxes[i]);
 }

"inplace", . , findContours.

+5

, burdinov,

struct byArea {
    bool operator () (const Rect & a,const Rect & b) {
         return a.width*a.height < b.width*b.height ;
    }
};

vector<Rect> rects;
std::sort(rects.begin(), rects.end(), byArea());
+3

All Articles