如何使用Numpy/OpenCV遮罩图像? [英] How to Mask an image using Numpy/OpenCV?

查看:175
本文介绍了如何使用Numpy/OpenCV遮罩图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我加载了一张图片:

im = cv2.imread(filename)

我想保留位于图像中心的数据.我创建了一个圆圈作为要保留的区域的遮罩.

I want to keep data that is in the center of the image. I created a circle as a mask of the area I want to keep.

我通过以下方式创建了圈子:

I created the circle with:

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

如何从原始图像中遮盖圆以外的数据?

How can I mask out the data outside of the circle from the original image?

masked_data = im * circle

不起作用.

推荐答案

使用cv2.bitwise_and并将圆作为遮罩传递.

Use cv2.bitwise_and and pass the circle as mask.

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)

这篇关于如何使用Numpy/OpenCV遮罩图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆