forked from yptheangel/opencv-starter-pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeskew.py
32 lines (27 loc) · 922 Bytes
/
deskew.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import cv2
import numpy as np
def preprocess(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.bitwise_not(gray)
thresh = cv2.threshold(gray, 0, 255,
cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
return thresh
def deskew(image, src):
coords = np.column_stack(np.where(image > 0))
angle = cv2.minAreaRect(coords)[-1]
if angle < -45:
angle = -(90 + angle)
else:
angle = -angle
(h, w) = image.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, angle, 1.0)
rotated = cv2.warpAffine(src, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)
return rotated
if __name__ == '__main__':
frame = cv2.imread("receipt.jpg")
preproceesed = preprocess(frame)
corrected = deskew(preproceesed, frame)
cv2.imshow("Original", frame)
cv2.imshow("Deskewed", corrected)
cv2.waitKey(0)