Opencv Detect boundary and ROI mask

梦想的初衷 提交于 2020-05-27 16:59:40

问题


Hi , I have attached the image below with an yellow bounding box. Is there any algorithm or (sequence of algorithms) in Opencv by which I can detect the yellow pixels and create a ROI mask (which will block out all the pixels outside of it).


回答1:


You can do:

  1. Find the yellow polygon
  2. Fill the inside of the polygon
  3. Copy only the inside of the polygon to a black-initialized image

Find the yellow polygon

Unfortunately, you used anti-aliasing to draw the yellow line, so the yellow color is not pure yellow, but has a wider range due to interpolation. This affects also the final results, since some not yellow pixels will be included in the result image. You can easily correct this by not using anti-aliasing.

So the best option is to convert the image in the HSV space (where it's easier to segment a single color) and keep only values in a range around the pure yellow.

If you don't use anti-aliasing, you don't even need to convert to HSV and simply keep points whose value is pure yellow.

Fill the inside of the polygon You can use floodFill to fill the polygon. You need a starting point for that. Since we don't know if a point is inside the polygon (and taking the baricenter may not be safe since the polygon is not convex), we can safely assume that the point (0,0), i.e. the top-left corner of the image is outside the polygon. We can then fill the outside of the polygon, and then invert the result.

Copy only the inside of the polygon to a black-initialized image

Once you have the mask, simply use copyTo with that mask to copy on a black image the content under non-zero pixels in the mask.

Here the full code:

#include <opencv2\opencv.hpp>
using namespace cv;

int main()
{
    Mat3b img = imread("path_to_image");

    // Convert to HSV color space
    Mat3b hsv;
    cvtColor(img, hsv, COLOR_BGR2HSV);

    // Get yellow pixels
    Mat1b polyMask;
    inRange(hsv, Scalar(29, 220, 220), Scalar(31, 255, 255), polyMask);

    // Fill outside of polygon
    floodFill(polyMask, Point(0, 0), Scalar(255));

    // Invert (inside of polygon filled)
    polyMask = ~polyMask;

    // Create a black image
    Mat3b res(img.size(), Vec3b(0,0,0));

    // Copy only masked part
    img.copyTo(res, polyMask);

    imshow("Result", res);
    waitKey();

    return 0;
}

Result:

NOTES

Please note that there are almost yellow pixels in the result image. This is due to anti-aliasing, as explained above.



来源:https://stackoverflow.com/questions/33077851/opencv-detect-boundary-and-roi-mask

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!