
Hi, I’m Higashi.
Here’s how to composite one image onto another with Python and OpenCV.
Left: the background image.
Center: the image to put on the background.
Right: the finished composite.

I expected this to be simple, but adding the two images directly gave me a surprising result.
Let’s look at what happens and how to handle it.
Let’s get started!
Install OpenCV if you don’t have it already.
Adding images with cv2.add
First, let’s look at the operation used in this example.
cv2.add() adds the values of corresponding pixels or channels.
composite_image_array=cv2.add(image1_array, image2_array)
The operation adds the arrays element by element.
Here’s what happens when we use it to combine the images above.
Use images with matching dimensions and channel layouts for this example.
My two images are already the same size.
If yours differ, resize them before combining them.
What goes wrong when you add the images directly
Here’s a common mistake.
Example: direct addition
#impot library
import cv2
#image read and composite
pic1=cv2.imread('pic1.jpg',cv2.IMREAD_COLOR)
pic2=cv2.imread('pic2.jpg',cv2.IMREAD_COLOR)
pic3=cv2.add(pic2,pic1)
#output image
cv2.imwrite('pic3.jpg',np.array(pic3))
Load the two images and add them with cv2.add().
Here’s the result.
Left: pic1.jpg.
Center: pic2.jpg.
Right: pic3.jpg.

For comparison, here’s the result I wanted.


The two results look very different.
The difference comes from the way the pixel values are added.
Look at the center of each input image.
Both are bright in that area.
Adding them makes the center too bright. In an 8-bit image, values above 255 are clipped.
That’s what you see in the first result.
Composite the images using a mask
Here’s how I avoid adding the two bright regions together.
I’ll call the background image A and the foreground image B.

Step 1: Convert image B to a binary mask.
Here’s the binary image.

Step 2: Find contours in the binary image.
Step 3: Select the largest contour.
Step 4: Fill the inside of that contour.
Here’s the resulting mask.

Step 5: Use the mask to clear the corresponding area in image A.
Set pixels in A to zero wherever the mask is white.

Step 6: Combine the original B image with the processed A image.
Here’s the finished composite.


This took a few more steps than a direct addition.
The sketch at the start simplified the process.

Here’s the complete flow.

Sample code for compositing the images
Here’s the program that puts those steps together.
#import library
import cv2
#image read
pic1=cv2.imread('pic1.jpg',cv2.IMREAD_COLOR)
pic2=cv2.imread('pic2.jpg',cv2.IMREAD_COLOR)
#binarization process
pic2gray=cv2.imread('pic2.jpg',cv2.IMREAD_GRAYSCALE)
ret, thresh = cv2.threshold(pic2gray, 5, 255, cv2.THRESH_BINARY)
#find contour
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_LIST,cv2.CHAIN_APPROX_NONE)
max_cnt =max(contours, key=lambda x: cv2.contourArea(x))
#make mask image
pic2thresh = cv2.drawContours(pic2gray, [max_cnt], -1, 255, -1)
cv2.imwrite('pic2thresh2.jpg',np.array(pic2thresh))
#preprocess of composite
pic2[pic2thresh<255]=[0,0,0]
pic1[pic2thresh==255]=[0,0,0]
cv2.imwrite('pic2thres3.jpg',np.array(pic1))
#composite process
pic3=cv2.add(pic1,pic2)
cv2.imwrite('add.jpg',np.array(pic3))

There may be a simpler way to get the same result.
If you know one, let me know in the comments.
Thanks for reading!


コメント