From 1a8491919c1139e18656a204026fa813533b7757 Mon Sep 17 00:00:00 2001 From: mmattmcw Date: Mon, 6 Feb 2023 00:13:05 -0500 Subject: [PATCH] Can now find all 4 frames and correctly orient the rectangles that contain them --- fourcell/calibrate.py | 101 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 94 insertions(+), 7 deletions(-) diff --git a/fourcell/calibrate.py b/fourcell/calibrate.py index 5f944b9..efe6c22 100644 --- a/fourcell/calibrate.py +++ b/fourcell/calibrate.py @@ -6,6 +6,8 @@ from os.path import exists, basename from common import image_resize, display, normalize_angle from json import load +DEBUG = True + #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'] @@ -93,8 +95,6 @@ def find_in_half (half) : cv2.rectangle(half, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2) return list(zip(*loc[::-1])) - - 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']] @@ -118,7 +118,7 @@ for pt in bhpts : clean = original.copy() for pt in marks : - print(pt) + #print(pt) cv2.circle(clean, pt, 50, (0,0,255), -1) print(f'Found {len(bhpts)} points') @@ -127,10 +127,97 @@ if len(marks) != 16 : print(f'{len(marks)} != 16 marks') exit(1) -print(find_closest((0,0,), marks)) -print(find_closest((width,0,), marks)) +## +# TOP LEFT +## -print(find_closest((0,height,), marks)) -print(find_closest((width,height,), marks)) +topLeftTR = find_closest((0,0,), marks) +#print(topLeftTR) +topLeftSorted = sorted(marks, key=lambda e: get_distance(e, topLeftTR)) +topLeftSorted.remove(topLeftTR) + +if abs(topLeftSorted[0][1] - topLeftTR[1]) < abs(topLeftSorted[1][1] - topLeftTR[1]) : + topLeftBR = topLeftSorted[0] +else : + topLeftBR = topLeftSorted[1] + +topLeftTL = topLeftSorted[2] +topLeftBL = topLeftSorted[4] + +if DEBUG : + cv2.line(clean, topLeftTR, topLeftBR, (0,0,255,), 3) + cv2.line(clean, topLeftTR, topLeftTL, (0,0,255,), 3) + cv2.line(clean, topLeftBR, topLeftBL, (0,0,255,), 3) + cv2.line(clean, topLeftTL, topLeftBL, (0,0,255,), 3) + +## +# TOP RIGHT +## + +topRightTL = find_closest((width,0,), marks) +#print(topRightTL) + +topRightSorted = sorted(topLeftSorted, key=lambda e: get_distance(e, topRightTL)) +topRightSorted.remove(topRightTL) +topRightSorted.remove(topLeftBR) +topRightSorted.remove(topLeftTL) +topRightSorted.remove(topLeftBL) + +topRightBL = topRightSorted[0] +topRightTR = topRightSorted[1] +if abs(topRightTL[0] - topRightSorted[2][0]) > abs(topRightTL[0] - topRightSorted[3][0]) : + topRightBR = topRightSorted[2] +else : + topRightBR = topRightSorted[3] + +if DEBUG : + cv2.line(clean, topRightTR, topRightBR, (0,0,255,), 3) + cv2.line(clean, topRightTR, topRightTL, (0,0,255,), 3) + cv2.line(clean, topRightBR, topRightBL, (0,0,255,), 3) + cv2.line(clean, topRightTL, topRightBL, (0,0,255,), 3) + +## +# BOTTOM LEFT +## + +bottomLeftTL = find_closest((0,height,), marks) + +bottomLeftSorted = sorted(topRightSorted, key=lambda e: get_distance(e, bottomLeftTL)) +bottomLeftSorted.remove(bottomLeftTL) +bottomLeftSorted.remove(topRightBR) +bottomLeftSorted.remove(topRightTR) +bottomLeftSorted.remove(topRightBL) + +bottomLeftBL = bottomLeftSorted[0] +bottomLeftTR = bottomLeftSorted[2] +bottomLeftBR = bottomLeftSorted[3] + +if DEBUG : + cv2.line(clean, bottomLeftTR, bottomLeftBR, (0,0,255,), 3) + cv2.line(clean, bottomLeftTR, bottomLeftTL, (0,0,255,), 3) + cv2.line(clean, bottomLeftBR, bottomLeftBL, (0,0,255,), 3) + cv2.line(clean, bottomLeftTL, bottomLeftBL, (0,0,255,), 3) + +## +# BOTTOM RIGHT +## + +bottomRightTR = find_closest((width,height,), marks) + +bottomRightSorted = sorted(bottomLeftSorted, key=lambda e: get_distance(e, bottomRightTR)) +bottomRightSorted.remove(bottomRightTR) +bottomRightSorted.remove(bottomLeftBR) +bottomRightSorted.remove(bottomLeftTR) +bottomRightSorted.remove(bottomLeftBL) + +bottomRightBR = bottomRightSorted[0] +bottomRightTL = bottomRightSorted[1] +bottomRightBL = bottomRightSorted[2] + +if DEBUG : + cv2.line(clean, bottomRightTR, bottomRightBR, (0,0,255,), 3) + cv2.line(clean, bottomRightTR, bottomRightTL, (0,0,255,), 3) + cv2.line(clean, bottomRightBR, bottomRightBL, (0,0,255,), 3) + cv2.line(clean, bottomRightTL, bottomRightBL, (0,0,255,), 3) display(clean)