Select and group registration marks

This commit is contained in:
Matt McWilliams 2023-01-29 22:53:51 -05:00
parent 727713f41b
commit adf465fd4c
1 changed files with 46 additions and 10 deletions

View File

@ -45,7 +45,7 @@ outputTmpl = sys.argv[-1]
print(f'Calibrating to scan {basename(normalImage)}') 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] w, h = registrationMark.shape[:2]
holePunches = read_text(normalText) holePunches = read_text(normalText)
original = cv2.imread(normalImage) original = cv2.imread(normalImage)
@ -59,13 +59,49 @@ if not orientation :
print(holePunches) print(holePunches)
topHalf = img[holePunches['0']['y']:holePunches['1']['y']+round(height*0.1), holePunches['0']['x']:holePunches['2']['x']] def get_distance(ref, point):
topHalfGray = cv2.cvtColor(topHalf, cv2.COLOR_BGR2GRAY) # print('ref: {} , point: {}'.format(ref, point))
res = cv2.matchTemplate(topHalfGray, registrationMark, cv2.TM_CCOEFF_NORMED) x1, y1 = ref[0], ref[1]
threshold = 0.5 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) loc = np.where( res >= threshold)
for pt in zip(*loc[::-1]): for pt in zip(*loc[::-1]):
print(f'{pt[0]},{pt[1]}') cv2.rectangle(half, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)
cv2.rectangle(topHalf, 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)