-->

explain arguments meaning in res = cv2.bitwise_and

2020-05-21 05:41发布

问题:

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. 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 ?

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

回答1:

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



回答2:

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



回答3:

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

Why the same image is used at src1 and 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

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

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

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,

otherwise the dst(I) keep its original value and the default value of all elements of the dst array is 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)

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



回答4:

what is actually each arguments mean? 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

# 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()


回答5:

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

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.

src2 – second input array or a scalar.

src – single input array.

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



回答6:

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.