Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 760
SACHIN Avatar asked Sep 25 '15 04:09

SACHIN


People also ask

What is cv2 Bitwise_and?

cv2. bitwise_and() is a function that performs bitwise AND processing as the name suggests. The AND of the values for each pixel of the input images src1 and src2 is the pixel value of the output image.

What is mask in OpenCV?

Masking is a common technique to extract the Region of Interest (ROI). In openCV, it is possible to construct arbitrary masking shape using draw function and bitwise operation.

What is image masking in Python?

Masking of images using Python OpenCV Masking is used in Image Processing to output the Region of Interest, or simply the part of the image that we are interested in. We tend to use bitwise operations for masking as it allows us to discard the parts of the image that we do not need.


4 Answers

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

like image 136
Animesh Srivastava Avatar answered Oct 04 '22 10:10

Animesh Srivastava


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

like image 20
Mohammed Awney Avatar answered Oct 04 '22 10:10

Mohammed Awney


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()
like image 20
Huynh Nguyen Avatar answered Oct 04 '22 09:10

Huynh Nguyen


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

like image 26
phchen2 Avatar answered Oct 04 '22 09:10

phchen2