-
Notifications
You must be signed in to change notification settings - Fork 25
/
image-straighten.py
165 lines (142 loc) · 4.87 KB
/
image-straighten.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import numpy as np
import matplotlib.pyplot as plt
import cv2
import math
from collections import OrderedDict
# In[deskew]:
def deskew(img):
thresh=img
edges = cv2.Canny(thresh,50,200,apertureSize = 3)
lines = cv2.HoughLines(edges,1,np.pi/1000, 55)
try:
d1 = OrderedDict()
for i in range(len(lines)):
for rho,theta in lines[i]:
deg = np.rad2deg(theta)
# print(deg)
if deg in d1:
d1[deg] += 1
else:
d1[deg] = 1
t1 = OrderedDict(sorted(d1.items(), key=lambda x:x[1] , reverse=False))
print(list(t1.keys())[0],'Angle' ,thresh.shape)
non_zero_pixels = cv2.findNonZero(thresh)
center, wh, theta = cv2.minAreaRect(non_zero_pixels)
angle=list(t1.keys())[0]
if angle>160:
angle=180-angle
if angle<160 and angle>20:
angle=12
root_mat = cv2.getRotationMatrix2D(center, angle, 1)
rows, cols = img.shape
rotated = cv2.warpAffine(img, root_mat, (cols, rows), flags=cv2.INTER_CUBIC)
except:
rotated=img
pass
return rotated
def unshear(img):
gray = img
thresh = img.copy()
#print(thresh)
plt.imshow(thresh)
plt.show()
trans = thresh.transpose()
arr=[]
for i in range(thresh.shape[1]):
arr.insert(0,trans[i].sum())
arr=[]
for i in range(thresh.shape[0]):
arr.insert(0,thresh[i].sum())
y = thresh.shape[0]-1-np.nonzero(arr)[0][0]
y_top = thresh.shape[0]-1-np.nonzero(arr)[0][-1]
trans1 = thresh.transpose()
sum1=[]
for i in range(trans1.shape[0]):
sum1.insert(i,trans1[i].sum())
height = y - y_top
max_value = 255*height
prev_num = len([i for i in sum1 if i>=(0.6*max_value)])
final_ang = 0
# # print(arr)
# # print(x,y)
for ang in range(-25,25,3):
thresh = gray.copy()
#print(thresh[0].shape)
#print(ang)
print('Ang',ang)
if ang>0:
#print(ang)
for i in range(y):
temp = thresh[i]
move = int((y-i)*(math.tan(math.radians(ang))))
if move >= temp.size:
move = temp.size
thresh[i][:temp.size-move]=temp[move:]
thresh[i][temp.size-move:] = [0 for m in range(move)]
else:
#print(ang)
for i in range(y):
temp = thresh[i]
move = int((y-i)*(math.tan(math.radians(-ang))))
if move >= temp.size:
move = temp.size
#print(temp[:-3])
#print(temp[:temp.size-move].shape, thresh[i][move%temp.size:].shape)
thresh[i][move:]=temp[:temp.size-move]
thresh[i][:move]=[0 for m in range(move)]
# plt.imshow(thresh)
# plt.show()
trans1 = thresh.transpose()
sum1=[]
for i in range(trans1.shape[0]):
sum1.insert(i,trans1[i].sum())
#print(sum1)
num = len([i for i in sum1 if i>=(0.60*max_value)])
#print(num, prev_num)
if(num>=prev_num):
prev_num=num
final_ang = ang
#plt.imshow(thresh)
#plt.show()
#print("final_ang:", final_ang)
thresh= gray.copy()
if final_ang>0:
for i in range(y):
temp = thresh[i]
move = int((y-i)*(math.tan(math.radians(final_ang))))
if move >= temp.size:
move = temp.size
thresh[i][:temp.size-move]=temp[move:]
thresh[i][temp.size-move:] = [0 for m in range(move)]
else:
for i in range(y):
temp = thresh[i]
move = int((y-i)*(math.tan(math.radians(-final_ang))))
#print(move)
if move >= temp.size:
move = temp.size
thresh[i][move:]=temp[:temp.size-move]
thresh[i][:move]=[0 for m in range(move)]
# plt.imshow(thresh)
# plt.show()
return thresh
# In[Main]:
def pad_with(vector, pad_width, iaxis, kwargs):
pad_value = kwargs.get('padder', 40)
vector[:pad_width[0]] = pad_value
vector[-pad_width[1]:] = pad_value
return vector
if __name__ == '__main__':
img = cv2.imread('./sample_images/c.png',0)
thresh = cv2.threshold(img,127,255,1)[1]
thresh=np.pad(thresh, 100, pad_with, padder=0)
plt.imshow(thresh)
plt.show()
deskew(thresh)
sheared_img = unshear(thresh)
ret, thresh = cv2.threshold(sheared_img,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
plt.imshow(thresh)
plt.show()
cv2.imwrite('./result/data/c.png', thresh)
else:
print("shear code: 2.34")