Work fixing template

This commit is contained in:
Matt McWilliams 2023-02-12 12:26:39 -05:00
parent 5fda5144cb
commit 7b67e586d9
1 changed files with 44 additions and 13 deletions

View File

@ -7,11 +7,13 @@ from common import image_resize, display, normalize_angle, read_json
from json import dumps
DEBUG = True
registrationMarkThreshold = 0.65
registrationMarkThreshold = 0.7
methodIndex = 0
marks = []
#clockwise from top left
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]
#
# CALIBRATE
@ -21,8 +23,7 @@ if len(sys.argv) < 2:
print('Please provide path of normalized scan to build template from to')
exit(1)
normalImage = sys.argv[-1]
normalImage = sys.argv[1]
if not exists(normalImage) :
print('Normalized scan does not exist, please provide one that does')
@ -34,17 +35,35 @@ if not exists(normalText) :
print('Corresponding normalized scan text does not exist, please generate one')
exit(3)
if len(sys.argv) > 2 :
if not exists(sys.argv[2]) :
registrationMark = cv2.imread(sys.argv[2])
print(f'Registration mark {sys.argv[2]} does not exist')
exit(4)
print(f'Using registration mark {sys.argv[2]}')
def get_dpi (width) :
if width == 2550 :
return 300
if width == 5100 :
return 600
if width == 10200 :
return 1200
print(f'Building template from scan {basename(normalImage)}')
registrationMark = cv2.imread('./registrationMark.png', 0)
w, h = registrationMark.shape[:2]
holePunches = read_json(normalText)
original = cv2.imread(normalImage)
img = original.copy()
height, width = img.shape[:2]
orientation = height > width
marks = []
dpi = get_dpi(width)
try: registrationMark
except NameError: registrationMark = None
if registrationMark is None :
registrationMark = cv2.imread(f'./registrationMark/{dpi}.png', 0)
w, h = registrationMark.shape[:2]
if not orientation :
print(f'Scan is not in portrait mode, exiting...')
@ -79,9 +98,9 @@ def find_closest (pt, pts) :
def find_in_half (half) :
halfGray = cv2.cvtColor(half, cv2.COLOR_BGR2GRAY)
res = cv2.matchTemplate(halfGray, registrationMark, cv2.TM_CCOEFF_NORMED)
res = cv2.matchTemplate(halfGray, registrationMark, matchMethods[methodIndex])
threshold = registrationMarkThreshold
loc = np.where( res >= threshold)
loc = np.where( res >= threshold )
for pt in zip(*loc[::-1]):
cv2.rectangle(half, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)
return list(zip(*loc[::-1]))
@ -92,11 +111,23 @@ topHalf = img[ttly:holePunches['1']['y']+round(height*0.1), ttlx:holePunches['2'
while True:
topHalfPts = find_in_half(topHalf)
if len(topHalfPts) < 12 :
registrationMarkThreshold -= 0.05
print(f'Found only {len(topHalfPts)}, decreasing threshold to {registrationMarkThreshold}')
if methodIndex == len(matchMethods) :
registrationMarkThreshold -= 0.005
methodIndex = 0
print(f'Found only {len(topHalfPts)}, decreasing threshold to {registrationMarkThreshold}')
else :
methodIndex += 1
print(f'Found only {len(topHalfPts)}, switching to method {matchMethods[methodIndex]}')
elif len(topHalfPts) > 12 :
registrationMarkThreshold += 0.05
print(f'Found {len(topHalfPts)}, increasing threshold to {registrationMarkThreshold}')
if methodIndex == len(matchMethods) :
registrationMarkThreshold += 0.005
methodIndex = 0
print(f'Found {len(topHalfPts)}, increasing threshold to {registrationMarkThreshold}')
else :
methodIndex += 1
print(f'Found {len(topHalfPts)}, switching to method {matchMethods[methodIndex]}')
else :
break