C ++ OpenCV组关闭轮廓并获取数据

时间:2018-09-04 05:05:54

标签: c++ image-recognition

我有使用OpenCV的C ++项目“容器ID识别”。在“查找轮廓”步骤中, 我的程序返回结果: enter image description here

我的ID的边框覆盖物分为多个部分。我的问题是:“请问如何获取我的ID并将其从图片中删除!”

enter image description here

非常感谢您!

这是我的源代码:

// find contours
    Mat mask = Mat::zeros(bw.size(), CV_8UC1);
    vector<vector<Point> > contours2;
    vector<Vec4i> hierarchy;
    vector<Rect> txtRect;
    vector<vector<Point> > txtContour;
    findContours(source, contours2, hierarchy, CV_RETR_CCOMP,
        CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
    std::sort(contours2.begin(), contours2.end(), compareContourAreas);



    // filter contours
    for (int i = 0; i >= 0; i = hierarchy[i][0]) {
        Rect rect = boundingRect(contours2[i]);
        Mat maskROI(mask, rect);
        maskROI = Scalar(0, 0, 0);

        // fill the contour
        drawContours(mask, contours2, i, Scalar(255, 255, 255), CV_FILLED);

        // ratio of non-zero pixels in the filled region
        double r = (double)countNonZero(maskROI) / (rect.width*rect.height);

        /* assume at least 45% of the area is filled if it contains text */
        if (r > .45 && (rect.height > 10 && rect.width > 10)) {
            rectangle(rgb, rect, Scalar(0, 255, 0), 2);
            txtRect.push_back(rect);
            txtContour.push_back(contours2[i]);
        }

    }
    if (debugging) { imshow("Characters", rgb); }

    Mat text(rgb.size(), CV_8U, Scalar(255));
    drawContours(text, txtContour, -1, Scalar(0), FILLED, 4);
    if (debugging) { imshow("Detected value", text); }

0 个答案:

没有答案