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
```
______________
| |
| 1 3 |
| |
| |
| |
| 3 4 |
| |
| |
| |
| 5 6 |
| |
---------------
human zero-index
_______________ _______________
| | | |
| 1 3 | | 0 2 |
| | | |
| | | |
| | | |
| 2 4 | | 1 3 |
| | | |
| | | |
| | | |
| 5 6 | | 4 5 |
| | | |
--------------- ---------------
```

View File

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