Calibrate script work, having trouble finding pattern so will have user select

This commit is contained in:
Matt McWilliams 2022-11-24 19:41:01 -05:00
parent 81d79a1602
commit dea1fb8134
2 changed files with 35 additions and 25 deletions

View File

@ -18,17 +18,18 @@ Two scripts:
## Page Hole Punch Order ## Page Hole Punch Order
``` ```
______________ human zero-index
| | _______________ _______________
| 1 3 | | | | |
| | | 1 3 | | 0 2 |
| | | | | |
| | | | | |
| 3 4 | | | | |
| | | 2 4 | | 1 3 |
| | | | | |
| | | | | |
| 5 6 | | | | |
| | | 5 6 | | 4 5 |
--------------- | | | |
--------------- ---------------
``` ```

View File

@ -4,21 +4,17 @@ import numpy as np
import math import math
from os.path import exists, basename from os.path import exists, basename
from common import image_resize, display, normalize_angle from common import image_resize, display, normalize_angle
from json import load
#clockwise from top left #clockwise from top left
order = [ 1, 3, 4, 6, 5, 2 ] 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']
def read_text (textPath) : def read_text (textPath) :
holePunches = {} holePunches = {}
with open(textPath) as t: with open(textPath) as json:
for line in t: holePunches = load(json)
i = int(line[0])
parts = line.split(' : ')
vals = parts[1].split(',')
holePunches[i] = {
'x' : int(vals[0]),
'y' : int(vals[1])
}
return holePunches return holePunches
# #
@ -39,7 +35,7 @@ if not exists(normalImage) :
print('Normalized scan does not exist, please provide one that does') print('Normalized scan does not exist, please provide one that does')
exit(2) exit(2)
normalText = normalImage + '.txt' normalText = normalImage + '.json'
if not exists(normalText) : if not exists(normalText) :
print('Corresponding normalized scan text does not exist, please generate one') print('Corresponding normalized scan text does not exist, please generate one')
@ -49,6 +45,8 @@ 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)
w, h = registrationMark.shape[:2]
holePunches = read_text(normalText) holePunches = read_text(normalText)
original = cv2.imread(normalImage) original = cv2.imread(normalImage)
img = original.copy() img = original.copy()
@ -59,4 +57,15 @@ if not orientation :
print(f'Scan is not in portrait mode, exiting...') print(f'Scan is not in portrait mode, exiting...')
exit(3) exit(3)
display(img) 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)
display(topHalf)