gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 16:56:24 25 4
gpt4 key购买 nike

给定一个图像和一组点(点数> = 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仍然很陌生,并且在学习过程中仍然学习所有内容。如果有更好的方法/图书馆使用,请提出建议。谢谢!

最佳答案

我刚刚找到了我的问题的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。如果发现任何缺陷,请发表评论。

关于python - Python opencv过滤感兴趣区域之外的所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30474792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com