Home > front end >  Possible solutions to process two images into one
Possible solutions to process two images into one

Time:04-28

Hi Im new to computing vision and I am currently stuck at this problem. I have 2shots of component(One is classing RGB and other is IR) and I would like them to overlap with small opacity. Problem is shots are in different dimensions and on slightly different zoom/angle so simlpe resize of smaller one wont put component on correct place. I was able to achieve it using programs like Inkscape but would like to have it automatic since there are more pictures and sets might be from different angle as well. Here are pictures and something I would like to achieve: enter image description here

CodePudding user response:

Ok. No needed to worry replacing IRshot.png. All you have to do is resizing valaue to 875. Here is resizing.py:

import cv2
 
src = cv2.imread('IRshot.png', cv2.IMREAD_UNCHANGED)

# set a new width in pixels
new_width = 875

# dsize
dsize = (new_width, src.shape[0])

# resize image
output = cv2.resize(src, dsize, interpolation = cv2.INTER_AREA)

cv2.imwrite('IRshot.png',output)

And here is working code:

#!/usr/bin/env python3.9.2
#OpenCV 4.5.5, Raspberry pi3B/ , 4b/4g/8g 
#Date: 26th April, 2022

import cv2
import numpy as np

img1 = cv2.imread('shot.png')
overlay_img1 = np.ones(img1.shape,np.uint8)*255

img2 = cv2.imread('IRshot.png')
rows,cols,channels = img2.shape
overlay_img1[488:rows 488, 488:cols 488] = img2

img2gray = cv2.cvtColor(overlay_img1,cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray,220,55,cv2.THRESH_BINARY_INV)
mask_inv = cv2.bitwise_not(mask)
temp1 = cv2.bitwise_and(img1,img1,mask = mask_inv)
temp2 = cv2.bitwise_and(overlay_img1,overlay_img1, mask = mask)

result = cv2.add(temp1,temp2)
cv2.imshow("Result",result)
cv2.imwrite("Result1.jpg",result)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output: enter image description here

  • Related