Python 解释 res = cv2.bitwise_and(img,img,mask = mask) 中的参数含义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32774956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:14:09  来源:igfitidea点击:

explain arguments meaning in res = cv2.bitwise_and(img,img,mask = mask)

pythonopencvbitwise-and

提问by SACHIN

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

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

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

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

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

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

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

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

其中 'img' 是输入图像。我从opencv得到的这段代码。但我不明白为什么在 bitwise_and 中使用了三个参数,每个参数实际上是什么意思?为什么在 src1 和 src2 使用相同的图像?

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

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

回答by shri

The below link explains clearly the bitwise operation and also the significance of each parameters. http://opencvexamples.blogspot.com/2013/10/bitwise-and-or-xor-and-not.html

下面的链接清楚地解释了按位运算以及每个参数的意义。 http://opencvexamples.blogspot.com/2013/10/bitwise-and-or-xor-and-not.html

void bitwise_and(InputArray src1, InputArray src2, OutputArray dst, InputArray mask=noArray())

void bitwise_and(InputArray src1, InputArray src2, OutputArray dst, InputArray mask=noArray())

Calculates the per-element bit-wise conjunction of two arrays or an array and a scalar. Parameters: src1 – first input array or a scalar.

计算两个数组或一个数组和一个标量的每个元素的按位连接。参数: src1 – 第一个输入数组或标量。

src2 – second input array or a scalar.

src2 – 第二个输入数组或标量。

src – single input array.

src – 单个输入数组。

value – scalar value.

值 - 标量值。

dst – output array that has the same size and type as the input arrays. mask – optional operation mask, 8-bit single channel array, that specifies elements of the output array to be changed

dst – 与输入数组具有相同大小和类型的输出数组。mask – 可选操作掩码,8 位单通道数组,指定要更改的输出数组的元素

回答by Mohammed Awney

The operation of "And" will be performed only if mask[i] doesn't equal zero, else the the result of and operation will be zero. The mask should be either white or black image with single channel. you can see this link http://docs.opencv.org/2.4.13.2/modules/core/doc/operations_on_arrays.html?highlight=bitwise#bitwise-and

只有当 mask[i] 不等于 0 时,才会执行“And”运算,否则 and 运算的结果将为零。遮罩应该是具有单通道的白色或黑色图像。你可以看到这个链接 http://docs.opencv.org/2.4.13.2/modules/core/doc/operations_on_arrays.html?highlight=bitwise#bitwise-and

回答by Animesh Srivastava

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.

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

Now suppose we have two images one is named img1and other is img2. img2 contains a logo which we want to place on the img1. We create thresholdand then the maskand mask_invof img2,and also create roiof 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,另一个是img2. img2 包含我们想要放置在 img1 上的徽标。我们先创建img2thresholdmaskmask_inv,然后创建roiimg1 的和。现在我们要做两件事,在img1上添加img2的logo。我们在以下帮助下将 roi 的背景创建为 img1_bg:mask_inv,mask_inv 将有两个区域,一个黑色和一个白色,在白色区域中,我们将放置 img1 部分并保留黑色 -

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

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

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

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

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

在 img2 中,我们需要创建徽标作为 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

这里我们使用了遮罩层,img2 的 logo 部分被填充到了遮罩的白色部分 现在当我们添加两者时,我们得到了一个完美的组合 roi 完整的描述和理解请访问: 打开 CV 代码文件和完整描述

回答by Cartesian Theater

Regarding using img twice, my guess is that we don't really care what img[i] and img[i] is, as it's just img[i] for binary. What matters is that, as mentioned by Mohammed Awney, when the mask is 0, we make img[i] be 0, otherwise we leave the pixel alone. This is a way to make certain pixels in img black, according to our mask.

关于使用 img 两次,我的猜测是我们并不真正关心 img[i] 和 img[i] 是什么,因为它只是二进制的 img[i] 。重要的是,正如 Mohammed Awney 所提到的,当掩码为 0 时,我们让 img[i] 为 0,否则我们不理会像素。根据我们的掩码,这是一种使 img 中的某些像素为黑色的方法。

回答by phchen2

From above answers we may know the definitions of the parameters of bitwise_and(), but they all do not answer the other question

从上面的回答我们可能知道bitwise_and()的参数定义,但都没有回答另一个问题

Why the same image is used at src1 and src2 ?

为什么在 src1 和 src2 使用相同的图像?

This question should be caused by the too simplified function definition in the document of OpenCV, it may be ambiguous to some people, in the document the bitwise_and() is defined as

这个问题应该是OpenCV文档中函数定义过于简单导致的,可能有些人会比较含糊,文档中bitwise_and()定义为

dst(I)=sur1(I) ^ sur2(I), if mask(I) != 0, where ^ represents the 'and' operator

dst(I)=sur1(I) ^ sur2(I), if mask(I) != 0, 其中^代表'与'运算符

from this definition at first sight I cannot get the picture about how to process the dst(I) when the mask(I) is 0.

从这个定义乍一看,我无法获得有关如何在掩码(I)为 0 时处理 dst(I) 的图片。

From the test result, I think that it should give a more clear function definition as

从测试结果来看,我认为它应该给出更清晰的函数定义为

dst(I)=sur1(I) ^sur2(I), if mask(I) != 0,

dst(I)=sur1(I) ^sur2(I), 如果 mask(I) != 0,

otherwise the dst(I) keep its original value and the default value of all elements of the dst array is 0.

否则 dst(I) 保持其原始值,dst 数组所有元素的默认值为 0。

Now we may know that using the same image for sur1 and sur2, it will only keep the original image parts in the area of mask(I) !=0 and the other area shows the part of the dst image (as the mask shape)

现在我们可能知道,sur1 和 sur2 使用相同的图像,它只会保留 mask(I) !=0 区域中的原始图像部分,其他区域显示 dst 图像的部分(作为掩码形状)

Additionally for other bitwise operations the definitions should be the same as above, they also need to add the otherwise condition and the default value description of the dst array

另外其他按位运算的定义应该和上面一样,还需要添加otherwise条件和dst数组的默认值描述

回答by Huynh Nguyen

what is actually each arguments mean? res = cv2.bitwise_and(img,img,mask = mask_img)

每个论点实际上是什么意思? res = cv2.bitwise_and(img,img,mask = mask_img)

src1: the first image (the first object for merging) src2: the second image (the second object for merging) mask: understood as rules to merge. If region of image ( which is gray-scaled, and then masked.) has black color ( valued as 0), then it is not combined (merging region of the first image with that of the second one) . Vice versa, it will be carried out. In your code, referenced image is "mask_img". In my case, my code is correct when it makes white + anycolor= anycolor; import cv2 import numpy as np

src1:第一张图片(合并的第一个对象) src2:第二个图片(合并的第二个对象) mask:理解为要合并的规则。如果图像区域(灰度化,然后被屏蔽。)具有黑色(值为 0),则不合并(将第一张图像的区域与第二张图像的区域合并)。反之,则执行。在您的代码中,引用的图像是“mask_img”。就我而言,当它使 white + anycolor= anycolor; 时,我的代码是正确的;导入 cv2 导入 numpy 作为 np

# Load two images
img1 = cv2.imread('bongSung.jpg')
img2 = cv2.imread('opencv.jpg')

# I want to put logo on top-left corner, so I create a ROI 
rows, cols, channels = img2.shape
roi = img1[0:rows, 0:cols]

# NOw we need to create a mask of the logo, mask is conversion to grayscale of an image
img2gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) 
ret, mask = cv2.threshold(img2gray, 220, 255, cv2.THRESH_BINARY_INV)
cv2.imshow('mask', mask)

mask_inv = cv2.bitwise_not(mask)
#cv2.imshow("mask_inv", mask_inv)

#When using bitwise_and() in opencv with python then white + anycolor = anycolor; black + anycolor = black 
img1_bg = cv2.bitwise_and(roi,roi,mask = mask_inv)
#cv2.imshow("img1_bg", img1_bg)

cv2.imshow("img2", img2)

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

dst = cv2.add(img1_bg,img2_fg)

img1[0:rows, 0:cols] = dst

#cv2.imshow("Image", img1)
cv2.waitKey(0)

cv2.destroyAllWindows()