I recently worked on a project that required blending multiple images together. I successfully completed all the necessary steps and successfully blended the images. However, I noticed that when I blended the images, black lines appeared on the borders of each piece of the final image, indicating where each image was placed. You can see an example of the final image with these artifacts in the following link:
These artifacts are noticeable even with a quick glance at the image.
This is my code for blending the images:
def normalize_piece(img):
return cv2.normalize(img.astype('float'), None, 0.0, 1.0, cv2.NORM_MINMAX)
def blend_pieces(piece1, piece2, transformation_matrix, width, height):
piece1_norm = normalize_piece(piece1)
piece2_norm = normalize_piece(piece2)
piece1_warped = cv2.warpPerspective(src=piece1_norm, M=transformation_matrix, dsize=(width, height))
black_rgb_pixel = np.zeros(3)
mask_left = np.all(piece1_warped != black_rgb_pixel, axis=-1)
mask_right = np.all(piece2_norm != black_rgb_pixel, axis=-1)
mask_overlap = mask_left & mask_right
mask_left_only = mask_left & ~mask_right
mask_right_only = mask_right & ~mask_left
piece1_warped[mask_left_only] = piece1_warped[mask_left_only]
piece1_warped[mask_right_only] = piece2_norm[mask_right_only]
piece1_warped[mask_overlap] = (piece1_warped[mask_overlap] + piece2_norm[mask_overlap]) / 2
return piece1_warped
Edit
I have these 3 parts of the final image for example and I want to merge them together:
I need to blend part 1 with part 2. And then, I need to blend the blended part with part 3.
I was able to achieve an answer to my question.
Here is a simple code to merge the parts that were provided in the question:
The
blend_imagesfunction creates a canvas of the specified shape (height, width) and merges the two input images using an element-wise maximum operation. Specifically, the function iterates over each pixel of the canvas and selects the maximum RGB value from the corresponding pixels in the two input images. The resulting pixel values are then assigned to the corresponding pixel in the new image.Update
Thanks to a helpful comment from 'Christoph Rackwitz', it was pointed out that the merging operation in the
blend_imagesfunction can be performed more efficiently using a Numpy routine.Here's an updated version of the code:
In this updated code, the
blend_imagesfunction is modified to use the Numpymaximumfunction instead of nested loops to perform the merging operation. The function also includes an assertion to ensure that the input images have the same shape.