2023-02-08 15:42:45 +00:00
|
|
|
import sys
|
|
|
|
import cv2
|
|
|
|
import numpy as np
|
|
|
|
from json import load
|
|
|
|
from os.path import exists, basename
|
|
|
|
from common import image_resize, display, read_json
|
|
|
|
|
2023-02-08 15:58:41 +00:00
|
|
|
holeConstant = .0156862745 # 160/10200
|
|
|
|
|
2023-02-08 15:42:45 +00:00
|
|
|
# use top left, top right, bottom left
|
|
|
|
def apply_image_to_points (original, target, points) :
|
|
|
|
rows, cols, ch = original.shape
|
|
|
|
ir, ic, ich = target.shape
|
|
|
|
|
2023-02-08 15:58:41 +00:00
|
|
|
print('Using points: ')
|
|
|
|
print(points)
|
2023-02-08 15:42:45 +00:00
|
|
|
|
|
|
|
atPts = np.float32(points)
|
|
|
|
targetPts = np.float32([[0, 0], [ic, 0], [0, ir]])
|
|
|
|
|
|
|
|
M = cv2.getAffineTransform(targetPts, atPts)
|
2023-02-20 23:54:08 +00:00
|
|
|
dst = cv2.warpAffine(target, M, (cols, rows),
|
|
|
|
flags = cv2.INTER_CUBIC,
|
|
|
|
borderMode = cv2.BORDER_CONSTANT,
|
|
|
|
borderValue = [0, 0, 0, 0])
|
2023-02-08 15:42:45 +00:00
|
|
|
|
2023-02-20 23:54:08 +00:00
|
|
|
mask = get_mask(dst)
|
|
|
|
return combine_images(original, dst, mask)
|
2023-02-08 15:42:45 +00:00
|
|
|
|
|
|
|
def create_blank(width, height):
|
|
|
|
rgb_color=(255,255,255)
|
|
|
|
image = np.zeros((height, width, 3), np.uint8)
|
|
|
|
color = tuple(reversed(rgb_color))
|
|
|
|
image[:] = color
|
2023-02-10 03:17:18 +00:00
|
|
|
#return image
|
|
|
|
return cv2.cvtColor(image, cv2.COLOR_BGR2BGRA)
|
2023-02-08 15:42:45 +00:00
|
|
|
|
|
|
|
# get points 0, 1 and 3 = top left, top right and bottom left
|
|
|
|
def to_points (d) :
|
2023-02-08 20:49:23 +00:00
|
|
|
return [[d['0']['x'], d['0']['y']], [d['1']['x'], d['1']['y']], [d['3']['x'], d['3']['y']]]
|
2023-02-08 15:42:45 +00:00
|
|
|
|
2023-02-20 23:44:20 +00:00
|
|
|
def get_mask (img) :
|
|
|
|
lower = np.array([0,0,0,0])
|
|
|
|
upper= np.array([0,0,0,0])
|
|
|
|
mask = cv2.inRange(img, lower, upper)
|
|
|
|
return cv2.bitwise_not(mask)
|
|
|
|
|
|
|
|
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-08 15:42:45 +00:00
|
|
|
if len(sys.argv) < 2 :
|
|
|
|
print('Please provide an output destination file')
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
outputFile = sys.argv[1]
|
|
|
|
|
|
|
|
if len(sys.argv) < 3:
|
|
|
|
print('Please provide a calibration template to apply images to')
|
|
|
|
exit(2)
|
|
|
|
|
|
|
|
templateFile = sys.argv[2]
|
|
|
|
|
|
|
|
if not exists(templateFile) :
|
|
|
|
print('Calibration template does not exist, please provide one that does')
|
|
|
|
exit(3)
|
|
|
|
|
|
|
|
if len(sys.argv) < 4:
|
|
|
|
print('Please provide at least one image to apply')
|
|
|
|
exit(4)
|
|
|
|
|
|
|
|
if len(sys.argv) > 7:
|
|
|
|
print('Please provide maximum four images to apply')
|
|
|
|
exit(5)
|
|
|
|
|
|
|
|
images = []
|
|
|
|
for i in range(3, len(sys.argv)):
|
|
|
|
imagePath = sys.argv[i]
|
|
|
|
if not exists(imagePath) :
|
|
|
|
print(f'Image {imagePath} does not exist, exiting...')
|
|
|
|
exit(6)
|
|
|
|
images.append(imagePath)
|
|
|
|
|
|
|
|
print('Using images: ')
|
|
|
|
for img in images:
|
|
|
|
print(f' -> {basename(img)}')
|
|
|
|
|
|
|
|
tmpl = read_json(templateFile)
|
|
|
|
|
|
|
|
print(f"Image size {tmpl['width']}x{tmpl['height']}")
|
|
|
|
|
2023-02-08 15:58:41 +00:00
|
|
|
hole = round(tmpl['width'] * holeConstant)
|
2023-02-08 20:34:40 +00:00
|
|
|
blank = create_blank(tmpl['width'], tmpl['height'])
|
2023-02-20 23:54:08 +00:00
|
|
|
#blank = cv2.imread(templateFile.replace('.calibration.json', ''))
|
|
|
|
output = blank.copy()
|
2023-02-08 15:58:41 +00:00
|
|
|
|
|
|
|
hp = tmpl['holePunches']
|
|
|
|
for i in hp:
|
|
|
|
print(hp[i])
|
|
|
|
cv2.circle(output, (hp[i]['x'], hp[i]['y'],), hole, (0, 0, 0,), -1)
|
|
|
|
|
2023-02-20 23:54:08 +00:00
|
|
|
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])}')
|
2023-02-08 15:42:45 +00:00
|
|
|
|
2023-02-20 23:54:08 +00:00
|
|
|
output = cv2.cvtColor(output, cv2.COLOR_BGRA2BGR)
|
|
|
|
#display(output)
|
|
|
|
cv2.imwrite(outputFile, output)
|
2023-02-10 03:17:18 +00:00
|
|
|
##print(f'Wrote {outputFile}')
|