[opencv]拟合vector<Mat>集合区域接近的元素

 

vector<Rect> PublicCardFrameDetection::fitrect(vector<Rect> rects){

    int size = rects.size();
    cout << "执行去重操作:" << "待去重数量="<< size << endl;

    int idx = 0;
    Rect temp;
    vector<Rect> temp_rects = rects;
    for (;;) {
        temp = temp_rects[idx];
        vector<Rect> rms;
        for (Rect &r: rects) {
            if (abs(r.x - temp.x) < temp.width /2 &&
                abs(r.y - temp.y) < temp.height / 2 && r != temp)
                if (r.width * r.height <= temp.width * temp.height) {
                    if (find(rects.begin(), rects.end(), temp) != rects.end()) {
                        rms.push_back(temp);
                    }
                } else {
                    if (find(rects.begin(), rects.end(), r) != rects.end()) {
                        rms.push_back(r);
                    }
                }
        }
        rects = TempHelper::removeAll(rects, rms);
        idx++;
        if (idx >= size) {
            break;
        }
    }

    //截取列表rects为4个元素
    int rsize = rects.size();
    if (rsize > 4){
        rects = splitRectList(rects,4);
    }
    return rects;
}

 

/**
 * 切片:截取列表为1到第几个元素
 * @param rects 
 * @param i 
 * @return 
 */
vector<Rect> PublicCardFrameDetection::splitRectList(vector<Rect> rects, int i) {
    int list_num = rects.size();
    if (list_num > i){
        rects.pop_back();
        return splitRectList(rects,i);
    } else if (list_num == i) {
        return rects;
    }
}

 

posted @ 2019-10-16 16:43  Xu_Lin  阅读(394)  评论(0编辑  收藏  举报