S
S
ShamiLLL2022-02-17 10:35:07
Python
ShamiLLL, 2022-02-17 10:35:07

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

2 answer(s)
N
Nikolay Savelyev, 2022-02-17
@AgentSmith

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

V
Vindicar, 2022-02-17
@Vindicar

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)
.
homography will be the projective transformation matrix - how to rotate markers B to fit markers A. ptmask will contain information about which pairs of points managed to match.
3. You call cv2.warpPerspective() to rotate image B according to the homography matrix. The size parameter should be such that the second image fits exactly with the first one. For example, you can pre-create a large size image.
4. You do the combination. You use cv2.PerspectiveTransform() to figure out where the corners of image B will be in the final image, you do cv2.fillPoly() to create a binary mask to overlay. In the mask, a white pixel will mean "put the pixel from rotated B here" and a black one will mean "leave the pixel as it is". Use this mask to overlay.
An example of code and source data is not exactly what you need, but it will convey the idea, I hope.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question