Hi, I’m higashi.
In this page, I explain how to composite image on another background image as shown below.
Left Image : Background Image
Center Image : Image to be Composited
Right Image : Composited Image
I think you image that is is easily, but this process is difficult unexpectedly.
This process will be very informative for beginner of python & openCV.
So, let’s get started!!
If you not install openCV, please install first.
Image Composition Method:cv2.add
At first, I explain the key skill of this process.
The method “cv2.add” is used for composite two images like shown in this page.
composite_image_array=cv2.add(image1_array, image2_array)
By this method, you can composite arrays that has brightness, BGR value and HSV value and so on of images.
In this time, by using this method, I want to make the image introduced begin of this page.
*The size of images should be match for using this method.
Since my Images has same size, so I don’t need change size change process.
But if you want to use images that has mismatch size, please change image size first.
Example of Common Mistake.
At first, I introduce the example of common mistake.
◆Common Mistake Sample
#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))
This is just load images, and composite them by using “cv2.add” method.
Let’s check result.
Left Image : pic1.jpg
Center Image : pic2.jpg
Right Image : pic3.jpg
On the other hand, the result shown at beginning of this page is below.
Completely different.
The reason is easy.
Please check the center of two images respectively.
Left image has high brightness and center image is also.
By using “cv2.add” in this condition, the center position of processed image has too high brightness.
So the result I display above is produced.
How to Composite Correctly
I explain the flow of how to composite correctly.
To explain, I name two image to “A” and “B” as shown below.
★Step1: Binarization of “B” Image
The below is image after binarization.
★Step2: Extract Contours of Step1 Image
★Step3: Choose the Maximum Contour from Step2 Process
★Step4:Fill in the inside of the contour that is obtained by Step3
The below is image after Step4 process.
★Step5:By Using Step4 Image, Processing A Image as shown below.
Make white areas in B image to black(value=0) in A image.
★Step6:Composite Original B Image and Above Image.
It is finish.
This is too hard process.
So, in fact, the sketch of process as shown below that is introduced at the beginning of this page is wrong.
As shown below is correct process.
Sample Code of Correct Composite Images
Finally, I introduce the sample code that can implement what has been explained so far.
#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))
Too long.
I feel there is more easily method.
If you know how to implement easily, please tell me from comment section.
That’s all. Thank you!!
コメント