我正在嘗試向影像添加棋盤以找到失真系數。但是,當我使用該功能時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)


uj5u.com熱心網友回復:
這是在 Python/OpenCV 中執行此操作的一種方法。確保您的“棋盤”影像將黑色映射到 1 而不是 0。然后在透視扭曲中,確保非影像背景顏色為純黑色,即 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)
結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/471835.html
下一篇:錯誤:(-215:Assertionfailed)(int)_numAxes==inputs[0].size()infunction'getMemoryShapes'
