this is a code to perform perspective transform on a license plate . i want to replace a vehicle license plate . when i run this code , the license plate of original vehicle substitute with a black plate . the orientation calculation is true but i can understand why the plate is totally black with zero values
def map_plate_to_image(pts, target_plate, src_image):
“””
Maps the rotated target license plate onto the source image using perspective transformation.
Args:
pts (list of tuples): List of four points (P1, P2, P3, P4) representing the license plate region in src_image.
target_plate (numpy.ndarray): The license plate image to be replaced (already rotated if needed).
src_image (numpy.ndarray): The original image containing the car with the license plate.
Returns:
numpy.ndarray: Resulting image with the license plate replaced.
"""
# Compute the size of the license plate region in src_image
plate_width = np.sqrt((pts[1][0] - pts[0][0])**2 + (pts[1][1] - pts[0][1])**2)
plate_height = np.sqrt((pts[2][0] - pts[1][0])**2 + (pts[2][1] - pts[1][1])**2)
# Resize the target_plate to match the license plate size
target_plate_resized = cv2.resize(target_plate, (int(plate_width), int(plate_height)))
# Define the quadrangle vertices for the source image (license plate region)
src_pts = np.array(pts, dtype=np.float32)
# Define the quadrangle vertices for the destination image (target_plate)
dst_pts = np.array([[0, 0], [plate_width, 0], [plate_width, plate_height], [0, plate_height]], dtype=np.float32)
# Get the perspective transformation matrix
M = cv2.getPerspectiveTransform(src_pts, dst_pts)
# Apply the transformation to the target_plate
perspective_plate = cv2.warpPerspective(target_plate_resized, M, (src_image.shape[1], src_image.shape[0]))
cv2.imshow("target_plate_resized", target_plate_resized)
cv2.imshow("perspective_plate", perspective_plate)
cv2.waitKey()
# Create a mask for the license plate region
mask = np.zeros_like(src_image)
cv2.fillConvexPoly(mask, src_pts.astype(int), (255, 255, 255))
# Combine the transformed plate with the original image
result_image = cv2.bitwise_and(src_image, cv2.bitwise_not(mask))
result_image += perspective_plate
return result_image
as u can see , the plate in destination image is blank
whats wrong with my code ?