-
Notifications
You must be signed in to change notification settings - Fork 0
/
fapiaorgb.py
348 lines (289 loc) · 8.67 KB
/
fapiaorgb.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#coding:utf-8
import numpy as np
import os
# import imtool
import shutil
import cv2
from PIL import Image,ImageDraw
from pylab import *
from sklearn.cluster import KMeans
from scipy.misc import imsave
from scipy.ndimage import morphology,measurements
# import matplotlib.pyplot as plt
from skimage import draw
import matplotlib.pyplot as plt
import image_pross
import time
imgname = './fapiao/009.jpeg'
time_start=time.time()
#放弃7图和2图,3图
img_org = Image.open(imgname)
img = np.array(Image.open(imgname))
# print(img.shape)
#lab色彩空间去背景,不够自适应
# img_lab = cv2.cvtColor(np.uint8(img),cv2.COLOR_BGR2Lab)[:,:,0]
# _ , img_bn = cv2.threshold(img_lab,160,255,cv2.THRESH_BINARY)
# print("lab")
# time_end=time.time()
# print( time_end-time_start)
# gray()
# figure()
# imshow(img_bn)
# show()
# print(img_bn)
# img_bn = img_bn/255
#去背景,kmeans,得到无背景图imgnoBG
img = img/255.0
# w,h,d = tuple(img.shape)
# img2D = img.reshape((w*h),d)
# kmeans = KMeans(n_clusters = 2,random_state = 0).fit(img2D)
# labels2D = kmeans.predict(img2D)
# img = img/255
w,h,d = tuple(img.shape)
# wthird = int(w/3)
# hthird = int(h/3)
# img_third = img[0:wthird,0:hthird,:]
# img2D = img_third.reshape((wthird*hthird),d)
# kmeans = KMeans(n_clusters = 2,random_state = 0).fit(img2D)
# labels2D = kmeans.predict(img2D)
# labels = labels2D.reshape(w,h)
# num1 = sum(labels)
# #labels=1 的像素点占多数,labels=1的像素点为背景点
# if num1>w*h/2:
# maskbg = 1-labels
# else:
# maskbg = labels.copy()
# imgcopy = img.copy()
# maskbg = (maskbg==0)
# imgcopy[maskbg] = 0
# imgnoBG = imgcopy
# print("KMeans")
# time_end=time.time()
# print( time_end-time_start)
# figure()
# imshow(imgnoBG)
# show()
imgnoBG = img
# 在去背景图的基础上提取红色信息,包括表格和红章
imgr = imgnoBG[:,:,0]
imgg = imgnoBG[:,:,1]
imgb = imgnoBG[:,:,2]
maskR1 = imgr>imgg
maskR2 = imgr>imgb
maskR = maskR1 * maskR2
imgcopy = imgnoBG.copy()
imgcopy[maskR] = 0
imgR = imgnoBG-imgcopy
maskB1 = imgb>imgr*1.1
maskB2 = imgb>imgg
maskB = maskB1*maskB2
maskB_inv = np.logical_not(maskB)
# imgcopy2 = imgnoBG.copy()
imgcopy2 = np.zeros((w,h))
imgcopy2[maskB] = 1
imgcopy2[maskB_inv] = 0
# imgB = imgnoBG-imgcopy2
imgB = imgcopy2
print("rgb separate")
time_end=time.time()
print( time_end-time_start)
# gray()
# figure()
# imshow(imgB)
# show()
# imsave("fapiaoR.png",imgR)
# 在红色图上提取红章
imgRr = imgR[:,:,0]
imgRg = imgR[:,:,1]
imgRb = imgR[:,:,2]
maskstamp = imgRr>(imgRg+imgRb)*0.8
imgcopy = imgR.copy()
imgcopy[maskstamp] = 0
img_stamp = imgR-imgcopy
thirdw = int(w/3)
img_stamp_half = img_stamp[0:thirdw,:,:]
# figure()
# imshow(img_stamp_half)
# show()
# 对红章图进行连通域标记,选择面积第二大的区域为红章区域
bn_stamp = np.zeros((thirdw,h))
maskstamp_third = maskstamp[0:thirdw,:]
bn_stamp[maskstamp_third] = 1
# bn_stamp = np.ones((w,h))
# bn_stamp[maskstamp] = 0
# gray()
# figure()
# imshow(bn_stamp)
# show()
# stamp_open = morphology.binary_erosion(bn_stamp,ones((2,2)),iterations = 2)
# stamp_open = morphology.binary_dilation(stamp_open,ones((2,2)),iterations = 1)
labels_open,nbr = measurements.label(bn_stamp)
# imsave("label.png",labels_open)
# gray()
# figure()
# imshow(stamp_open)
# show()
count = zeros(nbr)
for i in range(nbr):
count[i] = np.sum(labels_open==i)
# print(count[i])
index = np.argsort(-count)[1]
# print(index)
maskstamponly = (labels_open==index)
# print(a.shape)
stamp_only = zeros((thirdw,h))
stamp_only[maskstamponly] = 1
# gray()
# figure()
# imshow(stamp_only)
# show()
#计算红章中心点坐标,计算红章宽高
stamp_points = np.where(stamp_only==1)
# print(points)
stamp_x = np.average(stamp_points[0])
stamp_y = np.average(stamp_points[1])
stamp_h = np.max(stamp_points[0])-np.min(stamp_points[0])
stamp_w = np.max(stamp_points[1])-np.min(stamp_points[1])
# print(stamp_x,stamp_y,stamp_w,stamp_h)
# 在原图上绘制红章中心点显示
# rr, cc=draw.circle(int(stamp_x),int(stamp_y),5)
# draw.set_color(img,[rr, cc],[0,255,0])
# figure()
# imshow(img)
# show()
#按比例获取截图区域,右下(金额),右上(发票号,日前),左上(号)三个区域
ratio_right = 1.15
ratio_right_r = 7
start_rt =int(stamp_w/ratio_right+np.max(stamp_points[1])) #列
# end_rt = int(np.max(stamp_points[0])+5) #行
end_rt = int(stamp_h/ratio_right_r+np.max(stamp_points[0]))
croprighttop = imgB[0:end_rt,start_rt:h] # [行,列,:]
# gray()
# figure()
# imshow(croprighttop)
# show()
ratio_right2 = 0.28#0.3
start_rb_r = int(w/2)
end_rb_r = int(stamp_h/ratio_right2+np.max(stamp_points[0]))
start_rb_c = int(stamp_w/2+np.max(stamp_points[1]))
croprightbotm = imgB[start_rb_r:end_rb_r,start_rb_c:h]
# figure()
# imshow(croprightbotm)
# show()
ratio_left = 1.15
ratio_left2 = 0.475
star_lf_c = int(np.min(stamp_points[1])-(stamp_w/ratio_left2))
end_lf_c = int(np.min(stamp_points[1])-(stamp_w/ratio_left))
end_lf_r = int(stamp_x)
cropleft = img[0:end_lf_r,star_lf_c:end_lf_c,:]
# figure()
# imshow(cropleft)
# show()
#在局部区域分割四要素
#左上提取发票号
cropleft_lab = cv2.cvtColor(np.uint8(cropleft*255),cv2.COLOR_BGR2Lab)[:,:,0]
_ , cropleft_bn = cv2.threshold(cropleft_lab,120,255,cv2.THRESH_BINARY)
# croprightbotm_lab = cv2.cvtColor(np.uint8(croprightbotm*255),cv2.COLOR_BGR2Lab)[:,:,0]
# _ , croprightbotm_lab_bn = cv2.threshold(croprightbotm_lab,180,255,cv2.THRESH_BINARY)
# print(cropleft_lab)
# gray()
# figure()
# imshow(croprighttop_bn)
# show()
# print(cropleft_bn_c)
# 右上提取发票号,编号,日期
#行方向投影,取最后一行日期
# print(croprighttop[3,4,0])
# croprighttop_bn = 1- np.array(croprighttop)/255
index = image_pross.projection(croprighttop,"row",8)
index = -np.sort(-index[0])
# print(index)
data_start_r = int((index[1]+index[2])/2)
crop_numbers = croprighttop[0:data_start_r,:]
# gray()
# figure()
# imshow(crop_numbers)
# show()
index = image_pross.projection(crop_numbers,"col",5)[0]
# print(index)
#取 index序列中距离最大的两个点求中间位置
diff_index = image_pross.diff_seq_sec(index)
# print(diff_index)
indexmax = np.argmax(diff_index)*2
# print(indexmax)
# number1_end_c = int((index[15]+index[16])/2)
number1_end_c = int((index[indexmax]+index[indexmax-1])/2)
# print(index)
crop_numbers1 = croprighttop[0:data_start_r,number1_end_c:]
# gray()
# figure()
# imshow(crop_numbers1)
# show()
index = image_pross.projection(crop_numbers1,"row",5)
index = -np.sort(-index[0])
code2_end_r = int((index[1]+index[2])/2)
code2_start_r = int(index[3]-10)
crop_code2 = croprighttop[code2_start_r:code2_end_r,number1_end_c:]
# gray()
# figure()
# imshow(crop_code2)
# show()
crop_number2 = croprighttop[code2_end_r:data_start_r,number1_end_c:]
# gray()
# figure()
# imshow(crop_number2)
# show()
crop_data = croprighttop[data_start_r:,:]
index = image_pross.projection(crop_data,"col",5)[0]
data_start_c = int(index[0]-10)
crop_data = croprighttop[data_start_r:,data_start_c:]
# gray()
# figure()
# imshow(crop_data)
# show()
#右下提取不含税金额
index = image_pross.projection(croprightbotm,"row",5)[0]
mh = croprightbotm.shape[0]
money_start_r = int(index[0]-7)
money_end_r = np.min((int(index[1]+7),mh))
crop_money_tmp = croprightbotm[money_start_r:money_end_r,:]
# gray()
# figure()
# imshow(crop_money_tmp)
# show()
ratio_money_end_c = 0.53
index = image_pross.projection(crop_money_tmp,"col",5)[0]
money_start_c = int(index[0]-7)
diff_index = image_pross.diff_seq(index)
# print(diff_index)
indexmax = np.argmax(diff_index)
# print(indexmax)
# number1_end_c = int((index[15]+index[16])/2)
money_end_c = int((index[indexmax]+index[indexmax-1])/2)
# print(index)
# money_end_c = int(stamp_w/ratio_money_end_c+np.max(stamp_points[1]))-start_rt
crop_money = croprightbotm[money_start_r:money_end_r,money_start_c:money_end_c]
# gray()
# figure()
# imshow(crop_money)
# show()
# print(money_end_c)
#box = [左,上,右,下]
code1_box = [star_lf_c,0,end_lf_c,end_lf_r]
code2_box = [start_rt+number1_end_c,code2_start_r,h-1,code2_end_r]
number1_box = [start_rt,0,start_rt+number1_end_c,code2_end_r]
number2_box = [start_rt+number1_end_c,code2_end_r,h-1,data_start_r]
data_box = [start_rt+data_start_c,data_start_r,h-1,end_rt]
money_box = [start_rb_c+money_start_c,start_rb_r+money_start_r,start_rb_c+money_end_c,start_rb_r+money_end_r]
# print(img.shape,data_box)
time_end=time.time()
print( time_end-time_start)
imgout = image_pross.draw_box(img_org,code1_box,"code1")
imgout = image_pross.draw_box(imgout,code2_box,"code2")
imgout = image_pross.draw_box(imgout,number1_box,"number1")
imgout = image_pross.draw_box(imgout,number2_box,"number2")
imgout = image_pross.draw_box(imgout,data_box,"date")
imgout = image_pross.draw_box(imgout,money_box,"money")
figure()
imshow(imgout)
show()