简体   繁体   English

如何仅将图像上的白色区域视为轮廓

[英]how to consider only the white region on the image as contour

I have a binary image, from which I need to consider only the white regions as contours but it also takes black region which is surrounded by white part as contour.我有一个二值图像,我只需要将白色区域视为轮廓,但它也将被白色部分包围的黑色区域作为轮廓。 I don't want to use contour area, can we ignore the black regions while finding contours?我不想使用轮廓区域,我们可以在寻找轮廓时忽略黑色区域吗? Here is the binary image and the orange color marked is also considered as contour, so do not want the black region surrounded with white to be considered as contour.这里是二值图像,标记的橙色也被认为是轮廓,所以不希望被白色包围的黑色区域被认为是轮廓。

在此处输入图像描述 在此处输入图像描述

Contour image is:轮廓图为:

在此处输入图像描述

My contouring code:我的轮廓代码:

//contouring
    vector<vector<Point> > contours;
    findContours(img, contours, RETR_LIST, CHAIN_APPROX_SIMPLE);
    vector<vector<Point> > contours_poly(contours.size());
    vector<Rect> boundRect(contours.size());
    vector<Point2f>centers(contours.size());
    vector<float>radius(contours.size());
    for (size_t i = 0; i < contours.size(); i++)
    {
        approxPolyDP(contours[i], contours_poly[i], 3, true);
        boundRect[i] = boundingRect(contours_poly[i]);
        minEnclosingCircle(contours_poly[i], centers[i], radius[i]);
    }
    Mat drawing = Mat::zeros(img.size(), CV_8UC3);
    
    for (size_t i = 0; i < contours.size(); i++)
    {
        Scalar color = Scalar(rng.uniform(0, 256), rng.uniform(0, 256), rng.uniform(0, 256));
        
        drawContours(drawing, contours_poly, (int)i, color);
}

Your code snippets do not compose a minimal reproducible example ( https://stackoverflow.com/help/minimal-reproducible-example ).您的代码片段不构成最小的可重现示例( https://stackoverflow.com/help/minimal-reproducible-example )。 Therefore I could not run it for testing.因此我无法运行它进行测试。

However - you might be able to achieve what you want by "playing" with the 2 last parameters of cv::findContours .但是 - 您可以通过使用cv::findContours的最后两个参数“玩”来实现您想要的。

From opencv documentation of cv::findContours :来自cv::findContours的 opencv 文档:

    mode    Contour retrieval mode, see [RetrievalModes][1]

    method  Contour approximation method, see [ContourApproximationModes][1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM