Is it possible to copy only a specific region from an image and paste it to another image using either OpenCV or Numpy in python?
Lets say I have a RGB image and a grayscale mask of an image. (both are the same size)
Is it possible to copy the boat only where the pixels of the mask overlap to a specific part of a new image?
Here is the code I tried.
image1 = cv2.imread("boat.png")
mask = cv2.imread("boat_mask.png")
image2 = cv2.imread("sea.png")
width, height = image1.shape
image2[y:y+height,x:x+width] = image1[mask==255]
This doesn't work because image1[mask==255] no longer has a 2d shape.
Solution:
image2_cutout = image2[y:y+height,x:x+width]
image2_cutout[mask==255] = image1[mask==255]
image2[y:y+height,x:x+width] = image2_cutout


