warpAffine works with a mask

This commit is contained in:
Matt McWilliams 2023-02-20 18:54:08 -05:00
parent 57b9c1ebbc
commit 01c67e16dc
1 changed files with 17 additions and 40 deletions

View File

@ -19,12 +19,13 @@ def apply_image_to_points (original, target, points) :
targetPts = np.float32([[0, 0], [ic, 0], [0, ir]])
M = cv2.getAffineTransform(targetPts, atPts)
dst = cv2.warpAffine(target, M, (cols, rows)) #, borderMode=cv2.BORDER_TRANSPARENT
dst = cv2.warpAffine(target, M, (cols, rows),
flags = cv2.INTER_CUBIC,
borderMode = cv2.BORDER_CONSTANT,
borderValue = [0, 0, 0, 0])
original[0:rows, 0:cols] = dst
#return original
return cv2.cvtColor(original, cv2.COLOR_BGR2BGRA)
mask = get_mask(dst)
return combine_images(original, dst, mask)
def create_blank(width, height):
rgb_color=(255,255,255)
@ -92,46 +93,22 @@ print(f"Image size {tmpl['width']}x{tmpl['height']}")
hole = round(tmpl['width'] * holeConstant)
blank = create_blank(tmpl['width'], tmpl['height'])
output = blank.copy() #
#output = cv2.imread(templateFile.replace('.calibration.json', ''))
#blank = cv2.imread(templateFile.replace('.calibration.json', ''))
output = blank.copy()
hp = tmpl['holePunches']
for i in hp:
print(hp[i])
cv2.circle(output, (hp[i]['x'], hp[i]['y'],), hole, (0, 0, 0,), -1)
#cv2.imwrite(outputFile, output)
for i in range(0, len(images)) :
frame = cv2.imread(images[i])
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2RGBA)
cv2.imwrite(f'test{i}.png', output)
output = apply_image_to_points(output, frame, to_points(tmpl[f'{i}']))
print(f'Applied {basename(images[i])}')
#for i in range(0, len(images)) :
# frame = cv2.imread(images[i])
# frame = cv2.cvtColor(frame, cv2.COLOR_RGB2RGBA)
# cv2.imwrite(f'test{i}.png', output)
# output = apply_image_to_points(output, frame, to_points(tmpl[f'{i}']))
# print(f'Applied {basename(images[i])}')
target = cv2.imread(images[0])
target = cv2.cvtColor(target, cv2.COLOR_BGR2BGRA)
points = to_points(tmpl['0'])
rows, cols, ch = output.shape
ir, ic, ich = target.shape
print('Using points: ')
print(points)
atPts = np.float32(points)
targetPts = np.float32([[0, 0], [ic, 0], [0, ir]])
M = cv2.getAffineTransform(targetPts, atPts)
dst = cv2.warpAffine(target, M, (cols, rows),
flags = cv2.INTER_CUBIC,
borderMode = cv2.BORDER_CONSTANT,
borderValue = [0, 0, 0, 0])
#mask = cv2.inRange(dst, [0, 0, 0, 0], [0, 0, 0, 0])
output[0:rows, 0:cols] = dst
display(output)
#cv2.imwrite(outputFile, output)
output = cv2.cvtColor(output, cv2.COLOR_BGRA2BGR)
#display(output)
cv2.imwrite(outputFile, output)
##print(f'Wrote {outputFile}')