animation/fourcell/notes/affine_poc3.py

51 lines
1.3 KiB
Python
Raw Normal View History

2023-02-20 23:16:16 +00:00
import cv2
import numpy as np
import os
2023-02-20 23:38:03 +00:00
def combine_images (bg, fg, mask) :
fk = cv2.bitwise_or(fg, fg, mask=mask)
mask = cv2.bitwise_not(mask)
bk = cv2.bitwise_or(bg, bg, mask=mask)
return cv2.bitwise_or(fk, bk)
2023-02-20 23:16:16 +00:00
username = os.getlogin()
#outer template
outer = cv2.imread(f"/home/{username}/Desktop/running/shot1-scans/image-2-normal.png")
outer = cv2.cvtColor(outer, cv2.COLOR_BGR2BGRA)
2023-02-20 23:16:16 +00:00
#inner image
inner = cv2.imread(f"/home/{username}/Desktop/frame.jpg")
inner = cv2.cvtColor(inner, cv2.COLOR_BGR2BGRA)
2023-02-20 23:16:16 +00:00
rows, cols, ch = outer.shape
ir, ic, ich = inner.shape
print(f'{cols}x{rows}')
print(f'{ic}x{ir}')
# destination ponts on outer image
pts1 = np.float32([[1445, 11429], [1445, 6832], [5009, 11429]])
# corresponding points on inner image
pts2 = np.float32([[0, 0], [ic, 0], [0, ir]])
# transform inner to points on outer
M = cv2.getAffineTransform(pts2, pts1)
# apply
dst = cv2.warpAffine(inner, M, (cols, rows),
flags = cv2.INTER_CUBIC,
borderMode = cv2.BORDER_CONSTANT,
borderValue = [0, 0, 0, 0]
)
2023-02-20 23:16:16 +00:00
lower = np.array([0,0,0,0])
upper= np.array([0,0,0,0])
2023-02-20 23:38:03 +00:00
# Create a mask. Threshold the HSV image to get only yellow colors
mask = cv2.inRange(dst, lower, upper)
mask = cv2.bitwise_not(mask)
final = combine_images(outer, dst, mask)
final = cv2.cvtColor(final, cv2.COLOR_BGRA2BGR)
2023-02-20 23:38:03 +00:00
cv2.imwrite("affine_poc3.png", final)