diff --git a/fourcell/calibrate.py b/fourcell/calibrate.py index ee32b9f..1ffc941 100644 --- a/fourcell/calibrate.py +++ b/fourcell/calibrate.py @@ -8,8 +8,7 @@ from json import load #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'] def read_text (textPath) : holePunches = {} @@ -52,6 +51,7 @@ original = cv2.imread(normalImage) img = original.copy() height, width = img.shape[:2] orientation = height > width +marks = [] if not orientation : print(f'Scan is not in portrait mode, exiting...') @@ -79,9 +79,11 @@ def group_points(points): groups[key].append(points[i]) points[i] = None points = list(filter(lambda x: x is not None, points)) - # perform average operation on each group return list([[int(np.mean(list([x[0] for x in groups[arr]]))), int(np.mean(list([x[1] for x in groups[arr]])))] for arr in groups]) +def find_closest (pt, pts) : + return pts[min(range(len(pts)), key = lambda i: get_distance(pts[i], pt))] + def find_in_half (half) : halfGray = cv2.cvtColor(half, cv2.COLOR_BGR2GRAY) res = cv2.matchTemplate(halfGray, registrationMark, cv2.TM_CCOEFF_NORMED) @@ -89,19 +91,40 @@ def find_in_half (half) : 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) - #display(half) return list(zip(*loc[::-1])) -topHalf = img[holePunches['0']['y']-round(height*0.05):holePunches['1']['y']+round(height*0.1), holePunches['0']['x']:holePunches['2']['x']] + + +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) thpts = group_points(topHalfPts) for pt in thpts : - print(f'{pt[0]},{pt[1]}') + #print(f'{ttlx + pt[0]},{ttly + pt[1]}') + marks.append((ttlx + pt[0], ttly + pt[1],)) -bottomHalf = img[holePunches['4']['y']-round(height*0.1):holePunches['5']['y']+round(height*0.05), holePunches['3']['x']:holePunches['4']['x']] +print(f'Found {len(thpts)} points') +#display(topHalf) + +btly = holePunches['4']['y']-round(height*0.1) +btlx = holePunches['3']['x'] +bottomHalf = img[btly:holePunches['5']['y']+round(height*0.05), btlx:holePunches['4']['x']] bottomHalfPts = find_in_half(bottomHalf) bhpts = group_points(bottomHalfPts) for pt in bhpts : - print(f'{pt[0]},{pt[1]}') -display(bottomHalf) + #print(f'{btlx + pt[0]},{btly + pt[1]}') + marks.append((btlx + pt[0], btly + pt[1], )) + +print(f'Found {len(bhpts)} points') + +if len(marks) != 16 : + print(f'{len(marks)} != 16 marks') + exit(1) + +print(find_closest((0,0,), marks)) +print(find_closest((width,0,), marks)) + +print(find_closest((0,height,), marks)) +print(find_closest((width,height,), marks))