【问题标题】:Python opencv filtering everything outside region of interestPython opencv过滤感兴趣区域之外的所有内容
【发布时间】:2015-05-27 06:30:38
【问题描述】:

给定一个图像和一组点(点数 >= 3),其中这组点将形成一个多边形,这是我感兴趣的区域,我的目标是过滤该图像中该区域之外的所有内容感兴趣的,而里面的区域没有被触及。

例如,给定大小为712 x 480 px 的图像和点

[[120,160] [100,130] [120,100] [140,130]]

我所做的是

#Create an array of object rect which represents the region of interest
rect = [[120,160], [100,130], [120,100],[140,130]]
mask = np.array([rect], dtype=np.int32)

#Create a new array filled with zeros, size equal to size of the image to be filtered
image2 = np.zeros((480, 712), np.int8)

cv2.fillPoly(image2, [mask],255)

在这一步之后,image2 将是一个数组,除了位置与我感兴趣的区域完全相同的区域之外,其他任何地方都为 0。在这一步之后,我所做的是:

output = cv2.bitwise_and(image, image2)

image 这是我的输入图像。我收到此错误:

cv2.error: ..\..\..\..\opencv\modules\core\src\arithm.cpp:1021: error: (-209) The operation is neither 'array op array' (where arrays have the same size and type), nor 'array op scalar', nor 'scalar op array' in function cv::binary_op

我真的不明白我在这里做错了什么。另外,我的问题有其他解决方案吗?我对opencv还是很陌生,并且还在学习一切。如果有更好的方法/库使用,请提出建议。谢谢!

【问题讨论】:

    标签: python opencv image-processing


    【解决方案1】:

    我刚刚找到了 1 个解决我的问题的方法。所以不要写这个

    output = cv2.bitwise_and(image, image2)
    

    我首先将image2 转换为二进制掩码,然后将bitwise_and 与我的原始图像一起使用。所以代码应该是这样的

    maskimage2 = cv2.inRange(image2, 1, 255)
    out = cv2.bitwise_and(image, image, mask=maskimage2)
    

    这样做会使感兴趣区域之外的所有内容的二进制值为 0。如果您发现任何缺陷,请发表评论。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-03
      • 2020-08-12
      • 1970-01-01
      • 2012-04-25
      • 2013-02-28
      • 2017-09-28
      • 2011-08-10
      相关资源
      最近更新 更多