calibrate.py should actually be template.py. Calibration will be for printers and plotters. Different process.

This commit is contained in:
Matt McWilliams 2023-02-11 19:07:41 -05:00
parent 7581cc75b5
commit 479e9f5207
1 changed files with 17 additions and 6 deletions

View File

@ -7,6 +7,7 @@ from common import image_resize, display, normalize_angle, read_json
from json import dumps from json import dumps
DEBUG = True DEBUG = True
registrationMarkThreshold = 0.65
#clockwise from top left #clockwise from top left
order = [ 0, 2, 3, 5, 4, 1 ] order = [ 0, 2, 3, 5, 4, 1 ]
@ -17,7 +18,7 @@ matchMethods = ['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED', 'cv2.TM_CCORR', 'cv2.TM
# #
if len(sys.argv) < 2: if len(sys.argv) < 2:
print('Please provide path of normalized scan to calibrate to') print('Please provide path of normalized scan to build template from to')
exit(1) exit(1)
@ -34,7 +35,7 @@ if not exists(normalText) :
exit(3) exit(3)
print(f'Calibrating to scan {basename(normalImage)}') print(f'Building template from 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]
@ -79,7 +80,7 @@ def find_closest (pt, pts) :
def find_in_half (half) : def find_in_half (half) :
halfGray = cv2.cvtColor(half, cv2.COLOR_BGR2GRAY) halfGray = cv2.cvtColor(half, cv2.COLOR_BGR2GRAY)
res = cv2.matchTemplate(halfGray, registrationMark, cv2.TM_CCOEFF_NORMED) res = cv2.matchTemplate(halfGray, registrationMark, cv2.TM_CCOEFF_NORMED)
threshold = 0.7 threshold = registrationMarkThreshold
loc = np.where( res >= threshold) loc = np.where( res >= threshold)
for pt in zip(*loc[::-1]): for pt in zip(*loc[::-1]):
cv2.rectangle(half, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2) cv2.rectangle(half, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)
@ -88,7 +89,17 @@ def find_in_half (half) :
ttly = holePunches['0']['y']-round(height*0.05) ttly = holePunches['0']['y']-round(height*0.05)
ttlx = holePunches['0']['x'] ttlx = holePunches['0']['x']
topHalf = img[ttly:holePunches['1']['y']+round(height*0.1), ttlx:holePunches['2']['x']] topHalf = img[ttly:holePunches['1']['y']+round(height*0.1), ttlx:holePunches['2']['x']]
while True:
topHalfPts = find_in_half(topHalf) topHalfPts = find_in_half(topHalf)
if len(topHalfPts) < 12 :
registrationMarkThreshold -= 0.05
print(f'Found only {len(topHalfPts)}, decreasing threshold to {registrationMarkThreshold}')
elif len(topHalfPts) > 12 :
registrationMarkThreshold += 0.05
print(f'Found {len(topHalfPts)}, increasing threshold to {registrationMarkThreshold}')
else :
break
thpts = group_points(topHalfPts) thpts = group_points(topHalfPts)
for pt in thpts : for pt in thpts :
#print(f'{ttlx + pt[0]},{ttly + pt[1]}') #print(f'{ttlx + pt[0]},{ttly + pt[1]}')
@ -288,9 +299,9 @@ jsonOut = {
} }
} }
calibrationFile = f'{normalImage}.calibration.json' templateFile = f'{normalImage}.template.json'
with open(calibrationFile, 'w') as output: with open(calibrationFile, 'w') as output:
output.write(dumps(jsonOut, sort_keys = True, indent = 4)) output.write(dumps(jsonOut, sort_keys = True, indent = 4))
print(f'Wrote calibration file to {calibrationFile}') print(f'Wrote template file to {templateFile}')
display(clean) display(clean)