-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeskew.py
48 lines (36 loc) · 1.22 KB
/
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import numpy as np
import math
import cv2
def rotate_image(image, angle):
image_center = tuple(np.array(image.shape[1::-1]) / 2)
rot_mat = cv2.getRotationMatrix2D(image_center, angle, 1.0)
result = cv2.warpAffine(
image, rot_mat, image.shape[1::-1], flags=cv2.INTER_LINEAR)
return result, angle
def compute_skew(src_img):
if len(src_img.shape) == 3:
h, w, _ = src_img.shape
elif len(src_img.shape) == 2:
h, w = src_img.shape
else:
print('upsupported image type')
img = cv2.medianBlur(src_img, 3)
edges = cv2.Canny(img, threshold1=30, threshold2=100,
apertureSize=3, L2gradient=True)
lines = cv2.HoughLinesP(edges, 1, math.pi / 180, 30,
minLineLength=w / 4.0, maxLineGap=h / 4.0)
angle = 0.0
nlines = lines.size
# print(nlines)
cnt = 0
for x1, y1, x2, y2 in lines[0]:
ang = np.arctan2(y2 - y1, x2 - x1)
# print(ang)
if math.fabs(ang) <= 30: # excluding extreme rotations
angle += ang
cnt += 1
if cnt == 0:
return 0.0
return (angle / cnt) * 180 / math.pi
def deskew(src_img):
return rotate_image(src_img, compute_skew(src_img))