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

7

32

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

Filmdom answered 25/9, 2015 at 4:44 Comment(1)
This answer, for a similar question, explains the reason much betterSewerage
L
14

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

Lurlene answered 4/4, 2017 at 8:53 Comment(0)
D
14

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

Digged answered 10/6, 2017 at 10:56 Comment(0)
S
13

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()
Shrewsbury answered 1/1, 2019 at 3:50 Comment(0)
O
7

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

Ordnance answered 18/8, 2018 at 5:29 Comment(0)
I
0

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

Incursion answered 25/9, 2015 at 4:59 Comment(0)
A
0

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.

Acetous answered 18/11, 2017 at 3:37 Comment(0)
P
0

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

src1 first input array or a scalar.

src2 second input array or a scalar.

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(I)=src1(I)∧src2(I)if mask(I)≠0

and mask operate on dst

computes bitwise conjunction of the two arrays (dst = src1 & src2) Calculates the per-element bit-wise conjunction of two arrays or an array and a scalar.

Preteritive answered 25/3, 2021 at 9:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.