图像混合:透明黑色区域

Image blending: transparant black areas

我正在尝试向图像添加棋盘以查找失真系数。但是,当我使用函数 addWeighted() 时,棋盘的黑色区域是透明的。

首先,我使用函数 findHomography()warpPerspective()

扭曲我的棋盘图像

然后我尝试将场景图像和扭曲的棋盘与 addWeighted() 一起添加。我需要做什么才能让它不透明?

编辑代码:

input_1 = cv2.imread('RV_CV_Assignment_3_image_1.jpg')
imageSize = input_1.shape[:2]

chessboard = cv2.imread('Chessboard.jpg')
boardSize = np.float32([[0,0],[3632,0],[0,2816],[3632,2816]])

# These are the corner coordinates for the chessboard in the image. For now I do this manually
cornersBoard1 = np.float32([[348,233],[2004,233],[291,1555],[2025,1555]])

homography1, status1 = cv2.findHomography(boardSize,cornersBoard1)
warpBoard1 = cv2.warpPerspective(chessboard, homography1, (imageSize[1], imageSize[0]))

imgChessboard1 = cv2.addWeighted(input_1, 1, warpBoard1, 1, 0)
cv2.namedWindow('Chessboard in image 1', cv2.WINDOW_NORMAL)
cv2.imshow('Chessboard in image 1',imgChessboard1)

这是 Python/OpenCV 中的一种方法。确保您的“棋盘”图像已将黑色映射到 1 而不是 0。然后在您的透视扭曲中,确保 non-image 背景着色为纯黑色,即 0。然后使用 np.where 混合两张图片。

这是您相应修改的代码,

import cv2
import numpy as np

input_1 = cv2.imread('buildings.jpg')
imageSize = input_1.shape[:2]

chessboard = cv2.imread('checks.png')
boardSize = np.float32([[0,0],[3632,0],[0,2816],[3632,2816]])

# modify chessboard to map (0,0,0) to (1,1,1) so no pure black
chessboard[np.where((chessboard == [0,0,0]).all(axis=2))] = [1,1,1]

# These are the corner coordinates for the chessboard in the image. For now I do this manually
cornersBoard1 = np.float32([[348,233],[2004,233],[291,1555],[2025,1555]])

homography1, status1 = cv2.findHomography(boardSize,cornersBoard1)

# make sure background of warpParspective is pure black
warpBoard1 = cv2.warpPerspective(chessboard, homography1, (imageSize[1], imageSize[0]), borderMode=cv2.BORDER_CONSTANT, borderValue=(0,0,0))

# use np.where to blend the two images with the chessboard over the buildings
imgChessboard1 = np.where(warpBoard1==0, input_1, warpBoard1)

cv2.namedWindow('Chessboard in image 1', cv2.WINDOW_NORMAL)
cv2.imshow('Chessboard in image 1',imgChessboard1)
cv2.waitKey(0)
cv2.destroyAllWindows()

cv2.imwrite('checkerboard_on_buildings.jpg', imgChessboard1)

结果: