Python opencv过滤感兴趣区域之外的所有内容

2024-09-28 17:25:37 发布

您现在位置:Python中文网/ 问答频道 /正文

给定一个图像和一组点(点数>;=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的数组。在这一步之后,我所做的是:

^{pr2}$

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还是个新手,在学习的过程中我学到了很多东西。如果有更好的方法/库使用,请建议。谢谢!在


Tags: ofthetorect图像区域sizecreate
1条回答
网友
1楼 · 发布于 2024-09-28 17:25:37

我刚找到一个解决问题的办法。所以不要写这个

output = cv2.bitwise_and(image, image2)

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

^{pr2}$

在这个二进制区域之外执行0的操作将使该值的所有内容都有兴趣。如果你看到任何缺陷,请评论。在

相关问题 更多 >