python - 如何使用 Numpy/OpenCV 屏蔽图像?

标签 python opencv numpy

我加载了一张图片:

im = cv2.imread(filename)

我想保留图像中心的数据。我创建了一个圆圈作为我想要保留的区域的 mask 。

我用以下方法创建了圆圈:

height,width,depth = im.shape
circle = np.zeros((height,width))
cv2.circle(circle,(width/2,height/2),280,1,thickness=-1)

如何从原始图像中屏蔽掉圆圈外的数据?

masked_data = im * circle

不起作用。

最佳答案

使用 cv2.bitwise_and 并将圆作为掩码传递。

im = cv2.imread(filename)
height,width,depth = im.shape
circle_img = np.zeros((height,width), np.uint8)
cv2.circle(circle_img,(width/2,height/2),280,1,thickness=-1)

masked_data = cv2.bitwise_and(im, im, mask=circle_img)

cv2.imshow("masked", masked_data)
cv2.waitKey(0)

关于python - 如何使用 Numpy/OpenCV 屏蔽图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25074488/

相关文章:

python - Python 的 C 预处理器宏等效项

Python从elasticsearch中获取 float 作为 float 数组

xcode - 带有 Xcode 6.3 的 OpenCV 3.0.0

OpenCV:如何忽略颜色聚类中的背景像素

Python 2.7 程序(带搁置字典)为特定的键和值组合返回致命的 "dbrunrecoveryerror",但不是其他

python - 有什么办法可以让 tkinter 中的输入框等待输入? (在 Python 3 中)

python - 我怎样才能把结束日期推迟到明天呢?尝试安排一个程序每天运行

python - 识别图像目标线

python - 处理来自 python Numpy 数组 : Using values from one column to sum over adjacent value 的数据

python - Pandas /Numpy : remove leading/trailing nan in a pandas series or numpy array