Adding two images using special add points in OpenCV using python

I am trying to add two images of different sizes using bitwise operations in OpenCV using python. I want a specific point in Image1 (image of a person’s face) to coincide with a specific point in Image2 (image of a frame of glasses). Special moments are not the most corrupt image points. I know the two midpoints of the frame points and the pupil of the eyes. I want the midpoints of the frame to coincide with the points of the pupil of the eyes in the face. The code I use adds the second left upper corner point of the image to a specific point of Image1, as in line 10, while I want to add the middle point of the left glass frame.

The image of the face can be any random image, and the image of the spectacle -

Image 2

I am using the code:

import cv2
import numpy as np

img_frame = cv2.imread('image1.jpg',1)
img_in = cv2.imread('face.jpg',1)
new_image = np.zeros(img_frame.shape,dtype=np.uint8)

i,j,k = img_frame.shape

for ii in range (1,i):
    for jj in range (1,j):
        pixel = img_frame[ii,jj]

        img_in[339+ii,468+jj] = pixel

cv2.imwrite('pc2_with_frame_7.jpg',img_in)            
cv2.imshow('win',img_in)
cv2.waitKey(0)
cv2.destroyWindow('win')

Any help would be appreciated.

Thank.

+4
source share
1 answer

Well, it seems no one else can help, so I suggest what I can ...

What you are trying to do is called an alpha composition. You can read about it here on Wikipedia, as well as here in the OpenCV documentation.

My choice tool for this would be ImageMagick, which is free and has Perl, Python, C / C ++ bindings, as well as command line tools. If I start with this photo (face.jpg):

enter image description here

and take the glasses.jpg file and convert it to PNG with transparency, which looks like this:

enter image description here

ImageMagick

composite glasses.png face.jpg out.jpg

:

enter image description here

, OpenCV , , , . , @ypnos , , :

glasses.png -

input-mask.png

+1

Source: https://habr.com/ru/post/1541064/


All Articles