Normalize works, I like it. Image size is the real bandwidth limiter but images look good enough. SHould create a script to onionskin all scans for evaluation. Maybe run the holepunch detection on normal and write text file that prints the centers? Will do that now.
This commit is contained in:
parent
a352ad1062
commit
58955ade4b
|
@ -244,7 +244,7 @@ def find_hole_punches (img) :
|
|||
i = 0
|
||||
for hp in holePunches :
|
||||
hp['order'] = i
|
||||
cv2.putText(img, str(i + 1), (hp['x'], hp['y']), cv2.FONT_HERSHEY_SIMPLEX, 20, (0, 0, 255), 5, cv2.LINE_AA, False)
|
||||
#cv2.putText(img, str(i + 1), (hp['x'], hp['y']), cv2.FONT_HERSHEY_SIMPLEX, 20, (0, 0, 255), 5, cv2.LINE_AA, False)
|
||||
i+=1
|
||||
|
||||
return holePunches
|
||||
|
@ -339,6 +339,62 @@ def crop (img, xoffset, yoffset, w, h) :
|
|||
#crop_img = img[y:y+h, x:x+w].copy()
|
||||
return im[yoffset:yoffset+w, xoffset:xoffset+w].copy()
|
||||
|
||||
def normalize_image(blank, rotated, offset, tl) :
|
||||
rotatedHeight, rotatedWidth = rotated.shape[:2]
|
||||
normalHeight, width = blank.shape[:2]
|
||||
|
||||
diffX = offset[0] - tl["x"]
|
||||
diffY = offset[1] - tl["y"]
|
||||
|
||||
print(f'diffX : {diffX}')
|
||||
print(f'diffY : {diffY}')
|
||||
|
||||
crop = rotated.copy()
|
||||
if diffX < 0 :
|
||||
crop = crop[0:rotatedHeight, abs(diffX):rotatedWidth]
|
||||
rotatedHeight, rotatedWidth = crop.shape[:2]
|
||||
print('Cropped X')
|
||||
print(f'Rotated: {rotatedWidth},{rotatedHeight}')
|
||||
diffX = 0
|
||||
|
||||
if diffY < 0 :
|
||||
crop = crop[abs(diffY):rotatedHeight, 0:rotatedWidth]
|
||||
rotatedHeight, rotatedWidth = crop.shape[:2]
|
||||
print('Cropped Y')
|
||||
print(f'Rotated: {rotatedWidth},{rotatedHeight}')
|
||||
diffY = 0
|
||||
|
||||
if rotatedWidth > width :
|
||||
crop = crop[0:rotatedHeight, 0:rotatedWidth-(rotatedWidth - width)]
|
||||
rotatedHeight, rotatedWidth = crop.shape[:2]
|
||||
print('Cropped X')
|
||||
print(f'Rotated: {rotatedWidth},{rotatedHeight}')
|
||||
|
||||
if rotatedHeight > normalHeight :
|
||||
crop = crop[0:rotatedHeight-(rotatedHeight - normalHeight), 0:width]
|
||||
rotatedHeight, rotatedWidth = crop.shape[:2]
|
||||
print('Cropped Y')
|
||||
print(f'Rotated: {rotatedWidth},{rotatedHeight}')
|
||||
|
||||
print(f'diffX : {diffX}')
|
||||
print(f'diffY : {diffY}')
|
||||
|
||||
print(f'Rotated: {rotatedWidth},{rotatedHeight}')
|
||||
print(f'Blank : {width},{normalHeight}')
|
||||
|
||||
cropHeight = normalHeight
|
||||
cropWidth = width
|
||||
|
||||
if normalHeight > rotatedHeight :
|
||||
cropHeight = rotatedHeight
|
||||
|
||||
if width > rotatedWidth :
|
||||
cropWidth = rotatedWidth
|
||||
|
||||
blank[diffY:cropHeight, diffX:cropWidth] = crop[0:cropHeight-diffY, 0:cropWidth - diffX]
|
||||
|
||||
return blank
|
||||
|
||||
if len(sys.argv) < 2:
|
||||
print('Please provide path of scan to normalize')
|
||||
exit(1)
|
||||
|
@ -393,6 +449,10 @@ print(f'Topleft: {tl["x"]},{tl["y"]}')
|
|||
print(f'Rotated: {rotatedWidth},{rotatedHeight}')
|
||||
print(f'Blank : {width},{normalHeight}')
|
||||
|
||||
cv2.rectangle(blank, offset, (offset[0]+meanRect[0], offset[1]+meanRect[1]), (255, 0, 0), thickness=20)
|
||||
#cv2.rectangle(blank, offset, (offset[0]+meanRect[0], offset[1]+meanRect[1]), (255, 0, 0), thickness=20)
|
||||
|
||||
display(blank)
|
||||
normal = normalize_image(blank, rotated, offset, tl)
|
||||
|
||||
print(f'Writing normalized image to {normalImage}')
|
||||
cv2.imwrite(normalImage, normal)
|
||||
#display(normal)
|
Loading…
Reference in New Issue