OpenCV - 将蒙版应用于彩色图像 [英] OpenCV - Apply mask to a color image

查看:34
本文介绍了OpenCV - 将蒙版应用于彩色图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在最新的 python 绑定 (cv2) 中将蒙版应用到彩色图像?在之前的 python 绑定中,最简单的方法是使用 cv.Copy 例如

How can I apply mask to a color image in latest python binding (cv2)? In previous python binding the simplest way was to use cv.Copy e.g.

cv.Copy(dst, src, mask)

但是这个功能在cv2绑定中是没有的.有没有不使用样板代码的解决方法?

But this function is not available in cv2 binding. Is there any workaround without using boilerplate code?

推荐答案

这里,如果你已经有了遮罩图像,你可以使用 cv2.bitwise_and 函数.

Here, you could use cv2.bitwise_and function if you already have the mask image.

检查以下代码:

img = cv2.imread('lena.jpg')
mask = cv2.imread('mask.png',0)
res = cv2.bitwise_and(img,img,mask = mask)

lena 图像和矩形蒙版的输出如下.

The output will be as follows for a lena image, and for rectangular mask.

这篇关于OpenCV - 将蒙版应用于彩色图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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