OpenCV检测边界和ROI蒙版 [英] Opencv Detect boundary and ROI mask

查看:249
本文介绍了OpenCV检测边界和ROI蒙版的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我在下面的图像上附加了一个黄色的边框. Opencv中是否有任何算法或(算法序列),我可以通过这些算法或算法检测黄色像素并创建一个ROI蒙版(该蒙版将遮挡其外部的所有像素).

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. 找到黄色多边形
  2. 填充多边形的内部
  3. 仅将多边形的内部复制到黑色初始化的图像中

找到黄色多边形

不幸的是,您使用了抗锯齿来绘制黄线,因此黄色不是纯黄色,而是由于插值而具有更宽的范围.这也会影响最终结果,因为结果图像中会包含一些不是黄色像素.您可以通过不使用抗锯齿来轻松纠正此问题.

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.

所以最好的选择是在HSV空间(更容易分割单色)中转换图像,并仅将值保持在纯黄色范围内.

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.

如果您不使用抗锯齿功能,则甚至无需转换为HSV,只需保留其值为纯黄色的点即可.

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.

填充多边形的内部 您可以使用floodFill填充多边形.您需要一个起点.由于我们不知道某个点是否在多边形内(并且由于多边形不是凸的,所以获取重心可能并不安全),因此我们可以安全地假定该点(0,0),即该点的左上角图像在多边形之外.然后,我们可以填充多边形的外部,然后反转结果.

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.

仅将多边形内部复制到黑色初始化的图像

有了蒙版后,只需将copyTo与该蒙版一起使用,即可在黑色图像上复制蒙版中非零像素以下的内容.

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.

这里是完整代码:

#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;
}

结果:

注释

请注意,结果图像中几乎有黄色像素.如上所述,这是由于抗锯齿造成的.

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

这篇关于OpenCV检测边界和ROI蒙版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆