图像混合:透明黑色区域

发布于 2025-01-26 08:25:50 字数 1470 浏览 3 评论 0原文

我正在尝试将棋盘添加到图像中,以找到失真系数。但是,当我使用函数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)

“棋盘映像”

“

I'm trying to add a chessboard to an image in order to find the distortion coefficients. However when I use the function addWeighted() the black areas of my chessboard are transparant.

Transparant black areas?

First I warp my chessboard image with the functions findHomography() and warpPerspective()

Then I try to add the image of the scene and the warped chessboard together with addWeighted(). What do I need to do in order to get it not transparant?

Edit code:

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)

Chessboard image

Scene

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜唯美灬不弃 2025-02-02 08:25:50

这是在Python/OpenCV中做到这一点的一种方法。确保您的“棋盘”图像将黑色映射到1而不是0。然后,在您的透视图上,请确保非图像背景颜色为纯黑色,即0。然后使用NP.使用NP。在其中融合了两个图像。

这是相应修改的代码,

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)

结果:

”在此处输入图像说明”

Here is one way to do that in Python/OpenCV. Make sure your "chessboard" image has black mapped to 1 rather than 0. Then in your perspective warp, be sure the non-image background is colored as pure black, i.e. 0. Then use np.where to blend the two images.

Here is your code modified accordingly,

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)

Result:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文