Apply_image.py script is correctly placing images in the template locationg BUT is overwriting the starting blank image with a black background and doing so for all subsequent images. Not sure how to properly address this
This commit is contained in:
parent
89678edd8e
commit
aabdac74b5
|
@ -0,0 +1,85 @@
|
||||||
|
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
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
|
print(f'{cols}x{rows}')
|
||||||
|
print(f'{ic}x{ir}')
|
||||||
|
|
||||||
|
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), borderMode=cv2.BORDER_TRANSPARENT)
|
||||||
|
|
||||||
|
original[0:rows, 0:cols] = dst
|
||||||
|
|
||||||
|
return original
|
||||||
|
|
||||||
|
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
|
||||||
|
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
||||||
|
return cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB)
|
||||||
|
|
||||||
|
# get points 0, 1 and 3 = top left, top right and bottom left
|
||||||
|
def to_points (d) :
|
||||||
|
return ((d['0']['x'], d['0']['y']),(d['1']['x'], d['1']['y']),(d['3']['x'], d['3']['y']),)
|
||||||
|
|
||||||
|
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']}")
|
||||||
|
|
||||||
|
blank = create_blank(tmpl['width'], tmpl['height'])
|
||||||
|
output = blank.copy()
|
||||||
|
for i in range(0, len(images)) :
|
||||||
|
frame = cv2.imread(images[i])
|
||||||
|
output = apply_image_to_points(output, frame, to_points(tmpl[f'{i}']))
|
||||||
|
|
||||||
|
cv2.imwrite(outputFile, output)
|
||||||
|
print(f'Wrote {outputFile}')
|
|
@ -3,8 +3,8 @@ import cv2
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import math
|
import math
|
||||||
from os.path import exists, basename
|
from os.path import exists, basename
|
||||||
from common import image_resize, display, normalize_angle
|
from common import image_resize, display, normalize_angle, read_json
|
||||||
from json import load,dumps
|
from json import dumps
|
||||||
|
|
||||||
DEBUG = True
|
DEBUG = True
|
||||||
|
|
||||||
|
@ -12,12 +12,6 @@ DEBUG = True
|
||||||
order = [ 0, 2, 3, 5, 4, 1 ]
|
order = [ 0, 2, 3, 5, 4, 1 ]
|
||||||
matchMethods = ['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED', 'cv2.TM_CCORR', 'cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED']
|
matchMethods = ['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED', 'cv2.TM_CCORR', 'cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED']
|
||||||
|
|
||||||
def read_text (textPath) :
|
|
||||||
holePunches = {}
|
|
||||||
with open(textPath) as json:
|
|
||||||
holePunches = load(json)
|
|
||||||
return holePunches
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# CALIBRATE
|
# CALIBRATE
|
||||||
#
|
#
|
||||||
|
@ -44,7 +38,7 @@ print(f'Calibrating to scan {basename(normalImage)}')
|
||||||
|
|
||||||
registrationMark = cv2.imread('./registrationMark.png', 0)
|
registrationMark = cv2.imread('./registrationMark.png', 0)
|
||||||
w, h = registrationMark.shape[:2]
|
w, h = registrationMark.shape[:2]
|
||||||
holePunches = read_text(normalText)
|
holePunches = read_json(normalText)
|
||||||
original = cv2.imread(normalImage)
|
original = cv2.imread(normalImage)
|
||||||
img = original.copy()
|
img = original.copy()
|
||||||
height, width = img.shape[:2]
|
height, width = img.shape[:2]
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import cv2
|
import cv2
|
||||||
import math
|
import math
|
||||||
|
from json import load
|
||||||
|
|
||||||
def image_resize(image, width = None, height = None, inter = cv2.INTER_AREA):
|
def image_resize(image, width = None, height = None, inter = cv2.INTER_AREA):
|
||||||
dim = None
|
dim = None
|
||||||
|
@ -120,3 +121,9 @@ def normalize_angle (num, lower=0.0, upper=360.0, b=False):
|
||||||
res = num * 1.0 # Make all numbers float, to be consistent
|
res = num * 1.0 # Make all numbers float, to be consistent
|
||||||
|
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
def read_json (textPath) :
|
||||||
|
jsonOut = {}
|
||||||
|
with open(textPath) as json:
|
||||||
|
jsonOut = load(json)
|
||||||
|
return jsonOut
|
Loading…
Reference in New Issue