c++ - 搜索一个轮廓是否存在于另一个轮廓内

标签 c++ opencv image-processing contour

我有两个轮廓 vector OUTERCONT 和 INNERCONT 在 openCV 中定义为 vector ( vector (点))。我想检查一个轮廓是否存在于另一个轮廓内。我还想知道每个 OUTERCONT 内存在多少个轮廓。 我目前正在每个轮廓周围绘制一个 minEnendingRect 并检查以下内容:

for (int i = 0; i < outerrect.size(); i++)
{
    count = 0;
    for (int j = 0; j < innerrect.size(); j++)
    {
        bool is_inside = ((innerrect[j] & outerrect[i]) == innerrect[j]);
        if (is_inside == 1)
            count++;

    }
    if (count > 0)
    {
       //DO SOMETHING 
    }
    cout << count << endl;

这似乎不起作用,它总是将计数返回为 120 左右的某个数字,这是不正确的。您能否建议进行任何更改以使其正常工作?

注意:我无法使用层次结构,因为它们是从 2 个不同函数返回的两组独立的轮廓。

我知道 PointPloygon 测试是一种选择,但是您能建议更多的方法吗?

最佳答案

这是我从评论中得到的想法:

// stacked contours
int main(int argc, char* argv[])
{
    cv::Mat input = cv::imread("C:/StackOverflow/Input/Contours_in_Contours.png");

    cv::Mat input_red = cv::imread("C:/StackOverflow/Input/Contours_in_Contours_RED.png");

    cv::Mat reds;
    cv::inRange(input_red, cv::Scalar(0, 0, 200), cv::Scalar(50, 50, 255), reds);
    std::vector<std::vector<cv::Point> > contours1;
    cv::findContours(reds, contours1, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);

    cv::Mat input_yellow = cv::imread("C:/StackOverflow/Input/Contours_in_Contours_YELLOW.png");

    cv::Mat yellows;
    cv::inRange(input, cv::Scalar(0, 200, 200), cv::Scalar(0, 255, 255), yellows);
    std::vector<std::vector<cv::Point> > contours2;
    cv::findContours(yellows, contours2, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);

    // now we have 2 sets of contours and want to find out whether contours of set2 are completely within a contour of contours1 without hierarchy information.

    std::vector<cv::Mat> masks1;
    std::vector<int> nMaskPixels1;
    // for each contour in contours1: create a contour mask:
    for (int i = 0; i < contours1.size(); ++i)
    {
        cv::Mat mask1 = cv::Mat::zeros(input.size(), CV_8UC1);
        cv::drawContours(mask1, contours1, i, cv::Scalar::all(255), -1); // draw filled
        int nPixel1 = cv::countNonZero(mask1);

        masks1.push_back(mask1);
        nMaskPixels1.push_back(nPixel1);
    }

    std::vector<cv::Mat> masks2;
    std::vector<int> nMaskPixels2;
    // for each contour in contours2: test whether it is completely within the reference contour:
    for (int j = 0; j < contours2.size(); ++j)
    {
        cv::Mat mask2 = cv::Mat::zeros(input.size(), CV_8UC1);
        cv::drawContours(mask2, contours2, j, cv::Scalar::all(255), -1); // draw filled
        int nPixel2 = cv::countNonZero(mask2);

        masks2.push_back(mask2);
        nMaskPixels2.push_back(nPixel2);
    }

    for (int i = 0; i < masks1.size(); ++i)
    {
        cv::Mat mask1 = masks1[i];

        // draw mask again for visualization:
        cv::Mat outIm = input.clone();
        cv::drawContours(outIm, contours1, i, cv::Scalar(0, 0, 0), 3);

        for (int j = 0; j < masks2.size(); ++j)
        {
            cv::Mat mask2 = masks2[j];

            cv::Mat overlap = mask1 & mask2;
            int nOverlapPixels = cv::countNonZero(overlap);
            if (nOverlapPixels == 0) continue; // no overlap at all. Test next contour.

            if (nOverlapPixels == nMaskPixels2[j] && nOverlapPixels < nMaskPixels1[i])
            {
                // second contour is completely within first contour
                cv::drawContours(outIm, contours2, j, cv::Scalar(0, 255, 0), 3);
            }
            else if (nOverlapPixels == nMaskPixels2[j] && nOverlapPixels == nMaskPixels1[i])
            {
                // both contours are identical
                std::cout << "WARNING: " << "contours " << i << " and " << j << " are identical" << std::endl;
            }
            else if (nOverlapPixels < nMaskPixels2[j] && nOverlapPixels == nMaskPixels1[i])
            {
                // first contour is completely within second contour
                std::cout << "WARNING: " << "contour " << i << " of the first set is inside of " << j << std::endl;
            }
            else if (nOverlapPixels < nMaskPixels2[j] && nOverlapPixels < nMaskPixels1[i])
            {
                // both contours intersect
                cv::drawContours(outIm, contours2, j, cv::Scalar(255, 0, 255), 3);
            }

        }

        cv::imshow("contours", outIm);
        cv::imwrite("C:/StackOverflow/Output/contours.png", outIm);
        cv::waitKey(0);

    }



    cv::imshow("input", input);
    cv::waitKey(0);
    return 0;
}

此代码将从这两个图像创建两组轮廓:

enter image description here

enter image description here

计算轮廓掩模并比较它们。

结果将按轮廓显示。黑色轮廓是引用,绿色是完全在引用内的轮廓,紫色是相交轮廓。

我使用此图像来绘制结果:

enter image description here

得到这些结果:

轮廓1:

enter image description here

轮廓2:

enter image description here

轮廓3: enter image description here

轮廓4: enter image description here

轮廓5: enter image description here

如您所见,未检测到孤独的黄色轮廓与任何红色轮廓相交或包含在任何红色轮廓中。

关于c++ - 搜索一个轮廓是否存在于另一个轮廓内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37239872/

相关文章:

c++ - 对于使用 OpenGL 的程序,我需要类似于 sleep 功能的东西

c++ - Windows 上有复制文件夹的界面吗?

java - 如何使用 OpenCV matchShapes 检查两个轮廓是否匹配?

algorithm - 使用模糊 C 均值确定图像中的片段数

C++ 对 typedef void (* somename) (arg1, arg2) 的警告;

python - OpenCV,将物体分成几部分

python - 在 MAC 上为 python 2.7 安装 opencv

c++ - 使用openCV在C++中查看灰度RAW图像文件

algorithm - 地形中的匹配路径

c++ - 编译器错误? g++ 允许可变大小的静态数组,除非函数是模板化的