1

我正在查看 OpenCV 的文档,发现一些我无法理解的东西。我试图在网上找到它,但找不到任何令人满意的东西。你能帮我写一行代码吗?这是代码:

# Load two images
img1 = cv.imread('messi5.jpg')
img2 = cv.imread('opencv-logo-white.png')
# 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 create a mask of logo and create its inverse mask also
img2gray = cv.cvtColor(img2,cv.COLOR_BGR2GRAY)
ret, mask = cv.threshold(img2gray, 10, 255, cv.THRESH_BINARY)
mask_inv = cv.bitwise_not(mask)
# Now black-out the area of logo in ROI
img1_bg = cv.bitwise_and(roi,roi,mask = mask_inv)
# Take only region of logo from logo image.
img2_fg = cv.bitwise_and(img2,img2,mask = mask)
# Put logo in ROI and modify the main image
dst = cv.add(img1_bg,img2_fg)
img1[0:rows, 0:cols ] = dst
cv.imshow('res',img1)
cv.waitKey(0)
cv.destroyAllWindows()

我实际上不明白的是这两行

img1_bg = cv.bitwise_and(roi,roi,mask = mask_inv)

img2_fg = cv.bitwise_and(img2,img2,mask = mask)

这些行实际上做了什么以及如何应用掩蔽?

如果有人可以解释在 bitwise_and 操作中应用的掩码,那将非常有帮助。谢谢

4

1 回答 1

1

如果你看教程

蒙版是 OpenCV 徽标的黑白图像,它是通过对 OpenCV 徽标应用阈值创建的。

bitwise_and操作是逻辑与操作

在这种情况下,它采用两个代表一个像素的 8 位数字并对这些数字应用与运算。

文档描述了这个函数的作用。

由于前两个参数相同(两者roiimg2),如果不使用蒙版,结果将是相同的图像。蒙版为黑色的位置与目标图像相同。

在这种情况下,没有提供目标图像,因此 OpenCV 为函数中使用的目标图像分配了一个黑色图像(零)(这通常是 OpenCV 在没有为函数提供矩阵时的工作方式)。

Specifically img1_bg = cv.bitwise_and(roi,roi,mask = mask_inv) will create a black matrix used in the function which later becomes the output img1_bg. Only the parts of this black image that match up with white pixels in mask_inv are filled with the pixels from roi.This means that in the mask_inv where there are white pixels. the roi value will be copied in the pure black image generated by the function in the corresponding coordinate.

Similarly img2_fg = cv.bitwise_and(img2,img2,mask = mask) will create a black matrix used in the function which later becomes the output img2_fg. Only the parts of this black image that match up with white pixels in mask are filled with the pixels from img2.

This makes it so when you add img1_bg and img2_fg the result is only the part of each image that is masked.

Personally, I think this is a confusing use of bitwise_and. I think to demonstrate the function of bitwise_and it would be clearer to remove the mask parameter as follows: img1_bg = cv.bitwise_and(roi, mask_inv). This would give the same result, zeros where the mask is black, and the ROI values where it is not since the mask has pixels that are all ones or all zeroes.

If you don't care to demonstrate bitwise_and usage, in python I think it would be clearer to use logical indexing as follows:

output = np.zeros(img1.shape, np.uint8)
output[mask_inv] = img1_bg[mask_inv]
output[mask] = img2_fg[mask]
于 2019-07-16T03:57:30.237 回答