Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
472 views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...