Answer the question
In order to leave comments, you need to log in
How to overlay images in python?
Hello, I have a question about overlaying images. There are two identical images 1 and 2 (different formats), something is superimposed on the 2nd image. How can we overlay image 2 on the first one in such a way that we get the effect that we have just one whole image, i.e. so that all elements of the second image (naturally, except for what is superimposed on the second image) are corresponding to the same elements of the first image? Interested in working in python with the opencv library. Thanks in advance
Answer the question
In order to leave comments, you need to log in
Here, the usual work with the RGB components of each of the images is going through each pixel (i, j).
It is important to take into account the dimensions so that they match.
For overlay, we need to enter analogs of the alpha component (A) for each image so that their sum is equal to 1.
Then, if we have images a, b and the result c, then for each pixel and color component we need to calculate
Rc = Ra*Aa +Rb*Ab; same for G and B:
Gc = Ga*Aa + Gb * Ab
Bc = Ba*Aa + Bb * Ab
where Aa + Ab = 1
Those. to reformulate the question:
There are two images (A and B) with the same set of markers. It is necessary to transform image B so that its markers coincide in position with the markers of image A, and then overlay the result on image A.
I understand that?
If so, then it's done like this.
1. You form two arrays of coordinates - markers on images A and B. How - depends on the type of markers, but the order must be the same in both arrays. The shape of the arrays must be (N, 1, 2), where the last index is the x or y coordinate and N is the number of markers.
2. You call
homography, ptmask = cv2.findHomography(arrayB, arrayA, cv2.RANSAC)
. Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question