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

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


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 maskNow when we add both we get a perfect combined roiFor full description and understanding visit:OPEN CV CODE FILES AND FULL DESCRIPTION


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 linkhttp://docs.opencv.org/2.4.13.2/modules/core/doc/operations_on_arrays.html?highlight=bitwise#bitwise-and


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 cv2import numpy as np# Load two imagesimg1 = 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.shaperoi = img1[0:rows, 0:cols]# NOw we need to create a mask of the logo, mask is conversion to grayscale of an imageimg2gray = 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()