解释 res = cv2.bitwise_and(img,img,mask = mask) 中的参数含义 [英] explain arguments meaning in res = cv2.bitwise_and(img,img,mask = mask)

查看:836
本文介绍了解释 res = cv2.bitwise_and(img,img,mask = mask) 中的参数含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试提取输入图像的蓝色.为此,我使用以下命令创建了一个蓝色 HSV 颜色边界和阈值 HSV 图像

mask_img = cv2.inRange(hsv,lower_blue,upper_blue)

之后我在输入图像和阈值图像上使用了bitwise_and

res = cv2.bitwise_and(img, img, mask = mask_img)

其中 img 是输入图像.我从 opencv 得到了这段代码.但是我不明白为什么在 bitwise_and 中使用了三个参数以及每个参数的实际含义?为什么在 src1 和 src2 使用相同的图像?

还有这里mask关键字的用途是什么?请帮我找出答案

解决方案

这背后的基本概念是颜色 black 的值,它在 OPEN_CV 中的值为 0.所以 black + anycolor= anycolor 因为 black 的值为 0.

现在假设我们有两张图片,一张名为 img1,另一张名为 img2.img2 包含我们想要放置在 img1 上的徽标.我们创建threshold,然后创建img2的maskmask_inv,同时创建img1的roi.现在我们要做两件事,在img1上添加img2的logo.我们在以下帮助下将 roi 的背景创建为 img1_bg:mask_inv,mask_inv 将有两个区域,一个黑色和一个白色,在白色区域中,我们将放置 img1 部分并保留黑色原样 -

img1_bg = cv2.bitwise_and(roi,roi,mask = mask_inv)

在您的问题中,您直接使用了创建的 img 的掩码

res = cv2.bitwise_and(img,img,mask = mask_img)

在 img2 中,我们需要创建徽标作为 roi 的前景,

img2_fg = cv2.bitwise_and(img2,img2,mask = mask)

这里我们使用了遮罩层,img2的logo部分填充到了遮罩的白色部分现在,当我们将两者相加时,我们会得到一个完美的组合投资回报率如需完整说明和了解,请访问:打开简历代码文件和完整描述>

I am trying to extract blue colour of an input image. For that I create a blue HSV colour boundary and threshold HSV image by using the command

mask_img = cv2.inRange(hsv, lower_blue, upper_blue)

After that I used a bitwise_and on the input image and the threshold image by using

res = cv2.bitwise_and(img, img, mask = mask_img)

Where img is the input image. I got this code from opencv. But I didn't understand why are three arguments used in bitwise_and and what actually each arguments mean? Why the same image is used at src1 and src2 ?

And also what is the use of mask keyword here? Please help me to find out the answer

解决方案

The basic concept behind this is the value of color black ,it's value is 0 in OPEN_CV.So black + anycolor= anycolor because value of black is 0.

Now suppose we have two images one is named img1 and other is img2. img2 contains a logo which we want to place on the img1. We create threshold and then the mask and mask_inv of img2,and also create roi of img1. Now we have to do two things to add the logo of img2 on img1. We create background of roi as img1_bg with help of : mask_inv,mask_inv will have two region one black and one white, in the white region we will put img1 part and leave black as it is-

img1_bg = cv2.bitwise_and(roi,roi,mask = mask_inv)

In your question you have used directly the mask of the img created

res = cv2.bitwise_and(img,img,mask = mask_img)

and in img2 we need to create the logo as foreground of roi ,

img2_fg = cv2.bitwise_and(img2,img2,mask = mask)

here we have used mask layer , the logo part of img2 gets filled in the white part of mask Now when we add both we get a perfect combined roi For full description and understanding visit: OPEN CV CODE FILES AND FULL DESCRIPTION

这篇关于解释 res = cv2.bitwise_and(img,img,mask = mask) 中的参数含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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