From adf465fd4c00ebede54dd08c46e36301dc81a934 Mon Sep 17 00:00:00 2001 From: mmattmcw Date: Sun, 29 Jan 2023 22:53:51 -0500 Subject: [PATCH] Select and group registration marks --- fourcell/calibrate.py | 56 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 46 insertions(+), 10 deletions(-) diff --git a/fourcell/calibrate.py b/fourcell/calibrate.py index c4bf35c..ee32b9f 100644 --- a/fourcell/calibrate.py +++ b/fourcell/calibrate.py @@ -45,7 +45,7 @@ outputTmpl = sys.argv[-1] print(f'Calibrating to scan {basename(normalImage)}') -registrationMark = cv2.imread('./registration_mark.png', 0) +registrationMark = cv2.imread('./registrationMark.png', 0) w, h = registrationMark.shape[:2] holePunches = read_text(normalText) original = cv2.imread(normalImage) @@ -59,13 +59,49 @@ if not orientation : print(holePunches) -topHalf = img[holePunches['0']['y']:holePunches['1']['y']+round(height*0.1), holePunches['0']['x']:holePunches['2']['x']] -topHalfGray = cv2.cvtColor(topHalf, cv2.COLOR_BGR2GRAY) -res = cv2.matchTemplate(topHalfGray, registrationMark, cv2.TM_CCOEFF_NORMED) -threshold = 0.5 -loc = np.where( res >= threshold) -for pt in zip(*loc[::-1]): - print(f'{pt[0]},{pt[1]}') - cv2.rectangle(topHalf, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2) +def get_distance(ref, point): + # print('ref: {} , point: {}'.format(ref, point)) + x1, y1 = ref[0], ref[1] + x2, y2 = point[0], point[1] + return math.hypot(x2 - x1, y2 - y1) + +def group_points(points): + groups = {} + groupnum = 0 + while len(points) > 1: + groupnum += 1 + key = str(groupnum) + groups[key] = [] + ref = points.pop(0) + for i, point in enumerate(points): + d = get_distance(ref, point) + if d < 30: + 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_in_half (half) : + halfGray = cv2.cvtColor(half, cv2.COLOR_BGR2GRAY) + res = cv2.matchTemplate(halfGray, registrationMark, cv2.TM_CCOEFF_NORMED) + threshold = 0.7 + 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']] +topHalfPts = find_in_half(topHalf) +thpts = group_points(topHalfPts) +for pt in thpts : + print(f'{pt[0]},{pt[1]}') + +bottomHalf = img[holePunches['4']['y']-round(height*0.1):holePunches['5']['y']+round(height*0.05), holePunches['3']['x']:holePunches['4']['x']] +bottomHalfPts = find_in_half(bottomHalf) +bhpts = group_points(bottomHalfPts) +for pt in bhpts : + print(f'{pt[0]},{pt[1]}') +display(bottomHalf) -display(topHalf) \ No newline at end of file