diff --git a/fourcell/calibrate.py b/fourcell/template.py similarity index 91% rename from fourcell/calibrate.py rename to fourcell/template.py index 47a4541..c9a5140 100644 --- a/fourcell/calibrate.py +++ b/fourcell/template.py @@ -7,6 +7,7 @@ from common import image_resize, display, normalize_angle, read_json from json import dumps DEBUG = True +registrationMarkThreshold = 0.65 #clockwise from top left 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: - print('Please provide path of normalized scan to calibrate to') + print('Please provide path of normalized scan to build template from to') exit(1) @@ -34,7 +35,7 @@ if not exists(normalText) : exit(3) -print(f'Calibrating to scan {basename(normalImage)}') +print(f'Building template from scan {basename(normalImage)}') registrationMark = cv2.imread('./registrationMark.png', 0) w, h = registrationMark.shape[:2] @@ -79,7 +80,7 @@ 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) - threshold = 0.7 + threshold = registrationMarkThreshold 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) @@ -88,7 +89,17 @@ def find_in_half (half) : ttly = holePunches['0']['y']-round(height*0.05) ttlx = holePunches['0']['x'] topHalf = img[ttly:holePunches['1']['y']+round(height*0.1), ttlx:holePunches['2']['x']] -topHalfPts = find_in_half(topHalf) +while True: + 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) for pt in thpts : #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: 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)