From 58955ade4bf2d8b5ca80baeb67cfdd1a4022887a Mon Sep 17 00:00:00 2001 From: mattmcw Date: Tue, 15 Nov 2022 13:59:51 -0500 Subject: [PATCH] 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. --- fourcell/normalize.py | 66 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 3 deletions(-) diff --git a/fourcell/normalize.py b/fourcell/normalize.py index cb88930..3f2a7de 100644 --- a/fourcell/normalize.py +++ b/fourcell/normalize.py @@ -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) \ No newline at end of file +normal = normalize_image(blank, rotated, offset, tl) + +print(f'Writing normalized image to {normalImage}') +cv2.imwrite(normalImage, normal) +#display(normal) \ No newline at end of file