Created
May 12, 2021 08:52
-
-
Save hoangnt2601/5a5cd6fe57ba96ec04272cb9b72e5f0d to your computer and use it in GitHub Desktop.
preprocess image for ocr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import cv2 | |
| import numpy as np | |
| import PIL.Image as Image | |
| import imutils | |
| def increase_brightness(image, value=10): | |
| hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) | |
| h, s, v = cv2.split(hsv) | |
| lim = 255 - value | |
| v[v > lim] = 255 | |
| v[v <= lim] += value | |
| final_hsv = cv2.merge((h, s, v)) | |
| output = cv2.cvtColor(final_hsv, cv2.COLOR_HSV2BGR) | |
| return output | |
| def perspective_transform(image, hull): | |
| height, width = image.shape[:2] | |
| image_corner = hull.reshape(4, 2) | |
| image_corner = np.float32(image_corner) | |
| center_x, center_y = np.mean(image_corner, axis=0) | |
| sort_key = np.select( | |
| [ | |
| np.logical_and( | |
| image_corner[:, 0] < center_x, image_corner[:, 1] < center_y | |
| ), | |
| np.logical_and( | |
| image_corner[:, 0] > center_x, image_corner[:, 1] < center_y | |
| ), | |
| np.logical_and( | |
| image_corner[:, 0] > center_x, image_corner[:, 1] > center_y | |
| ), | |
| np.logical_and( | |
| image_corner[:, 0] < center_x, image_corner[:, 1] > center_y | |
| ), | |
| ], | |
| [0.0, 1.0, 2.0, 3.0], | |
| ) | |
| sort_key = np.expand_dims(sort_key, axis=0) | |
| temp = np.concatenate((image_corner, sort_key.T), axis=1) | |
| sorted_corner = temp[np.lexsort(temp.T)] | |
| sorted_corner = sorted_corner[:, 0:2] | |
| dst_corner = np.array( | |
| ((0, 0), (width - 1, 0), (width - 1, height - 1), (0, height - 1)), | |
| np.float32, | |
| ) | |
| M, _ = cv2.findHomography(sorted_corner, dst_corner, cv2.RANSAC, 10) | |
| warped = cv2.warpPerspective(image, M, (width, height)) | |
| return warped | |
| def alignment(image, margin=30): | |
| thresh = cv2.threshold(image[:, :, 3], 0, 255, cv2.THRESH_BINARY)[1] | |
| blur = cv2.medianBlur(thresh, 15) | |
| cnts = cv2.findContours(blur, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) | |
| cnts = imutils.grab_contours(cnts) | |
| cnts = sorted(cnts, key=cv2.contourArea, reverse=True) | |
| perimeter = cv2.arcLength(cnts[0], True) | |
| approx = cv2.approxPolyDP(cnts[0], 0.02 * perimeter, True) | |
| hull = cv2.convexHull(approx, clockwise=True) | |
| warped = perspective_transform(image, hull) | |
| height, width = warped.shape[:2] | |
| output = warped[margin : height - margin, margin : width - margin] | |
| return output | |
| def remove_shadow(image): | |
| image = increase_brightness(image) | |
| rgb_planes = cv2.split(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) | |
| result_norm_planes = [] | |
| for plane in rgb_planes: | |
| dilated_img = cv2.dilate(plane, np.ones((5, 5), np.uint8)) | |
| opening_img = cv2.morphologyEx(dilated_img, cv2.MORPH_OPEN, np.ones((11, 11))) | |
| closing_img = cv2.morphologyEx(opening_img, cv2.MORPH_CLOSE, np.ones((11, 11))) | |
| diff_img = 255 - cv2.absdiff(plane, closing_img) | |
| norm_img = cv2.normalize( | |
| diff_img, | |
| None, | |
| alpha=0, | |
| beta=255, | |
| norm_type=cv2.NORM_MINMAX, | |
| dtype=cv2.CV_8UC1, | |
| ) | |
| result_norm_planes.append(norm_img) | |
| result_norm = cv2.merge(result_norm_planes) | |
| return result_norm |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment