Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python opencv过滤感兴趣区域之外的所有内容_Python_Opencv_Image Processing - Fatal编程技术网

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

Python opencv过滤感兴趣区域之外的所有内容,python,opencv,image-processing,Python,Opencv,Image Processing,给定一幅图像和一组点(点数>=3),其中点集将形成一个多边形,这是我感兴趣的区域,我的目标是过滤该图像中位于感兴趣区域之外的所有内容,而其中的区域未被触及 例如,给定大小为712 x 480 px的图像以及点 [[120160] [100,130] [120,100] [140130]] 我所做的是 #Create an array of object rect which represents the region of interest rect = [[120,160], [100,130

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

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

[[120160]
[100,130]
[120,100]
[140130]]

我所做的是

#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还是一个新手,在学习的过程中,我仍然在学习一切。如果有更好的方法做/使用图书馆,请提出建议。谢谢

我刚刚找到了一个解决问题的方法。所以与其写这个

output = cv2.bitwise_and(image, image2)
我首先将
image2
转换成一个二进制掩码,然后
按位_并将其与原始图像一起转换。所以代码应该是这样的

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

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

我刚刚找到了一个解决问题的方法。所以与其写这个

output = cv2.bitwise_and(image, image2)
我首先将
image2
转换成一个二进制掩码,然后
按位_并将其与原始图像一起转换。所以代码应该是这样的

maskimage2 = cv2.inRange(image2, 1, 255)
out = cv2.bitwise_and(image, image, mask=maskimage2)
这样做将使感兴趣区域之外的所有内容都具有二进制值0。如果您发现任何缺陷,请进行评论