Home > Blockchain >  Image blending: transparant black areas
Image blending: transparant black areas

Time:05-07

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)

Edit2: Awnser Multiply the background pixels to black by multiplying by 0: imgChessboard1 = cv2.addWeighted(0*input_1, 1, warpBoard1, 2, 0)

The ratio's for the images will stay the same for your input image and new found checkboard image. So now you can use findChessboardCorners() and drawChessboardCorners() to calibrate the camera

Chessboard image

Scene

CodePudding user response:

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

  • Related