-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.py
1136 lines (1062 loc) · 37 KB
/
function.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# #!/usr/bin/python
# -*- coding: utf-8 -*-
from PIL import Image,ImageDraw
from numpy import *
from scipy.ndimage import filters
from copy import deepcopy
cell3 = [(-1,-1),(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
cell5 = [(-2,-1),(-2,0),(-2,1),(-1,2),(0,2),(1,2),(2,1),(2,0),(2,-1),(1,-2),(0,-2),(-1,-2),(-2,-1)]
cell7 = [(-3,-3), (-3,-2), (-3,-1), (-3,0), (-3,1), (-3,2), (-3,3), (-2,3), (-1,3), (0,3), (1,3), (2,3), (3,3), (3,2), (3,1), (3,0),
(3,-1), (3,-2), (3,-3), (2,-3), (1,-3), (0,-3), (-1,-3), (-2,-3)]
def get_angle(left,right,flag) :
angle = right - left
if flag >= 1 :
if angle < 0 :
angle += 10
elif flag <= -1 :
if angle > 0 :
angle -= 10
return angle
# ************************************************************#
# 求梯度函数 返回值包含x方向,y方向以及总梯度矢量的图像, 后续也用于分割算法
def Magnitude(image) :
im = array(image)
imx = zeros(im.shape)
filters.sobel(im,1,imx)
imy = zeros(im.shape)
filters.sobel(im,0,imy)
magnitude = sqrt(imx ** 2 + imy ** 2)
x_gradient = Image.fromarray(imx)
y_gradient = Image.fromarray(imy)
mag = Image.fromarray(magnitude)
return (mag,x_gradient,y_gradient)
# ************************************************************#
# 方向图算法,效果平滑,但是速度还是有些慢,需要加速改进
# image1 : 分割后的正常图 image12 : 分割后的黑白图
def Direction(image1,image2) :
width = image1.size[0]
height = image1.size[1]
iDirection_Dot = Image.new("RGB",(width,height),'white') # 点方向图 彩色
iDirection_succession = Image.new("RGB",(width,height),"white") # 连续分布方向图 彩色
array1 = image1.load()
array2 = image2.load()
array3 = iDirection_Dot.load()
array4 = iDirection_succession.load()
for y in range(5,height-4) :
for x in range(5,width-4) :
if array2[x,y] == 255 :
array3[x,y] = (255,255,255)
continue
aver_grey_d1 = sum([array1[x,y],array1[x,y-4],array1[x,y-2],array1[x,y+2],array1[x,y+4]]) / 5 # 90度
aver_grey_d2 = sum([array1[x,y],array1[x-2,y-4],array1[x-1,y-2],array1[x+1,y+2],array1[x+2,y+4]]) / 5 # 67.5度
aver_grey_d3 = sum([array1[x,y],array1[x-4,y-4],array1[x-2,y-2],array1[x+2,y+2],array1[x+4,y+4]]) / 5 # 45度
aver_grey_d4 = sum([array1[x,y],array1[x-4,y-2],array1[x-2,y-1],array1[x+2,y+1],array1[x+4,y+2]]) / 5 # 22.5度
aver_grey_d5 = sum([array1[x,y],array1[x-4,y],array1[x-2,y],array1[x+2,y],array1[x+4,y]]) / 5 # 0 度
aver_grey_d6 = sum([array1[x,y],array1[x+4,y-2],array1[x+2,y-1],array1[x-2,y+1],array1[x-4,y+2]]) / 5 # 112.5度
aver_grey_d7 = sum([array1[x,y],array1[x+4,y-4],array1[x+2,y-2],array1[x-2,y+2],array1[x-4,y+4]]) / 5 # 135度
aver_grey_d8 = sum([array1[x,y],array1[x+2,y-4],array1[x+1,y-2],array1[x-1,y+2],array1[x-2,y+4]]) / 5 # 157.5度
aver_diff1 = abs(aver_grey_d1-aver_grey_d5) # 90度和0度
aver_diff2 = abs(aver_grey_d2-aver_grey_d8) # 67.5度和157.5度
aver_diff3 = abs(aver_grey_d3-aver_grey_d7) # 45度和135度
aver_diff4 = abs(aver_grey_d4-aver_grey_d6) # 22.5度和112.5度
list_diff = [aver_diff1,aver_diff2,aver_diff3,aver_diff4]
Max_Diff = max(list_diff)
perhap_direction = list_diff.index(Max_Diff)
if perhap_direction == 0 :
if abs(aver_grey_d1-array1[x,y]) < abs(aver_grey_d5-array1[x,y]) :
array3[x,y] = (255,0,0) # 90度
else :
array3[x,y] = (255,0,255) # 0度
elif perhap_direction == 1 :
if abs(aver_grey_d2-array1[x,y]) < abs(aver_grey_d8-array1[x,y]) :
array3[x,y] = (0,255,0) #67.5度
else :
array3[x,y] = (0,0,0) #157.5度
elif perhap_direction == 2 :
if abs(aver_grey_d3-array1[x,y]) < abs(aver_grey_d7-array1[x,y]) :
array3[x,y] = (0,0,255) # 45度
else :
array3[x,y] = (255,255,255) #135度
elif perhap_direction == 3 :
if abs(aver_grey_d4-array1[x,y]) < abs(aver_grey_d6-array1[x,y]) :
array3[x,y] = (255,255,0) #22.5度
else :
array3[x,y] = (0,255,255) #112.5度
for j in range(5,height-4) :
for i in range(5,width-4) :
if array2[i,j] == 255 :
array4[i,j] = (255,255,255)
continue
a = [0] * 8
for n in range(-8,9) : # 变成17*17的平滑模板 时间增加 效果非常不错 想想怎么快一点
for m in range(-8,9) :
if -1 < i-m < width and -1 < j-n < height :
if array3[i-m,j-n] == (255,0,255) :
a[0] += 1
elif array3[i-m,j-n] == (255,255,0) :
a[1] += 1
elif array3[i-m,j-n] == (0,0,255) :
a[2] += 1
elif array3[i-m,j-n] == (0,255,0) :
a[3] += 1
elif array3[i-m,j-n] == (255,0,0) :
a[4] += 1
elif array3[i-m,j-n] == (0,255,255) :
a[5] += 1
elif array3[i-m,j-n] == (255,255,255) :
a[6] += 1
elif array3[i-m,j-n] == (0,0,0) :
a[7] += 1
Max = max(a)
dot_direction = a.index(Max)
if dot_direction == 0 :
array4[i,j] = (255,0,255)
elif dot_direction == 1 :
array4[i,j] = (255,255,0)
elif dot_direction == 2 :
array4[i,j] = (0,0,255)
elif dot_direction == 3 :
array4[i,j] = (0,255,0)
elif dot_direction == 4 :
array4[i,j] = (255,0,0)
elif dot_direction == 5 :
array4[i,j] = (0,255,255)
elif dot_direction == 6 :
array4[i,j] = (255,255,255)
elif dot_direction == 7 :
array4[i,j] = (0,0,0)
return (iDirection_Dot,iDirection_succession)
# *********************************************************** #
# 分割算法:包含三个阶段
# 1. 对原图按轮廓进行形态学处理,得到黑白二值的轮廓图
# 2. 对上一步的结果依据x方向梯度、y方向梯度进行横纵切,有效去掉一些杂乱噪声
# 3. 按照最后的轮廓进行还原
# 用于平滑图像的高斯算子,实现图像收敛处理
gauess_filter_operator = [0.0000,0.0001,0.0010,0.0016,0.0010,0.0001,0.0000,
0.0001,0.0027,0.0129,0.0214,0.0129,0.0027,0.0001,
0.0010,0.0129,0.0582,0.0960,0.0582,0.0129,0.0010,
0.0016,0.0214,0.0960,0.1586,0.0960,0.0214,0.0016,
0.0010,0.0129,0.0582,0.0960,0.0582,0.0129,0.0010,
0.0001,0.0027,0.0129,0.0214,0.0129,0.0027,0.0001,
0.0000,0.0001,0.0010,0.0016,0.0010,0.0001,0.0000]
# 用于进行形态学运算的结构矩阵,共有三个,第二个效果比较好
structure_data_one = [0 ,0 ,0 ,0 ,0 ,1 ,0 ,0 ,0 ,0 ,0 ,
0 ,0 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,0 ,0 ,
0 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,0 ,
0 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,0 ,
0 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,0 ,
1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,
0 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,0 ,
0 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,0 ,
0 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,0 ,
0 ,0 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,0 ,0 ,
0 ,0 ,0 ,0 ,0 ,1 ,0 ,0 ,0 ,0 ,0]
structure_data_two = [0,0,0,1,0,0,0,
0,0,1,1,1,0,0,
0,1,1,1,1,1,0,
0,1,1,1,1,1,0,
0,1,1,1,1,1,0,
0,0,1,1,1,0,0,
0,0,0,1,0,0,0]
structure_data_three = [1,1,1,1,1,
1,1,1,1,1,
1,1,1,1,1,
1,1,1,1,1,
1,1,1,1,1]
# 形态学腐蚀函数
def Corrode(image,structure_data,radius) :
structure_num = (2 * radius + 1) ** 2
flag = 0
valid_data = 0
width = image.size[0]
height = image.size[1]
iCorrode = Image.new("L",(width,height),255)
KH = range(radius) + range(height-radius,height)
KL = range(radius) + range(width-radius,width)
for k in range(structure_num) :
if structure_data[k] == 1 :
valid_data += 1
for y in range(height) :
for x in range(width) :
if y in KH or x in KL :
iCorrode.putpixel((x,y),0)
elif image.getpixel((x,y)) == 255 :
iCorrode.putpixel((x,y),255)
else :
flag = 0 # 要始终记得变量清零
for n in range(2 * radius + 1) :
for m in range(2 * radius + 1) :
if structure_data[(structure_num - 1) - (n*(2*radius+1) + m)] == 1 and image.getpixel((x-radius+m,y-radius+n)) == 0 :
flag += 1
else :
continue
if flag == valid_data :
iCorrode.putpixel((x,y),0)
else :
iCorrode.putpixel((x,y),255)
return iCorrode
# 形态学膨胀函数
def Expand(image,structure_data,radius) :
structure_num = (2 * radius + 1) ** 2
width = image.size[0]
height = image.size[1]
KH = range(radius) + range(height-radius,height)
KL = range(radius) + range(width-radius,width)
iExpand = Image.new("L",(width,height),255)
for y in range(height) :
for x in range(width) :
if y in KH or x in KL :
iExpand.putpixel((x,y),0)
else :
for n in range(2 * radius + 1) :
for m in range(2 * radius + 1) :
if -1 < x - radius + m < width and -1 < y - radius + n < height and structure_data[(structure_num - 1) - (n*(2*radius+1) + m)] == 1 and image.getpixel((x-radius+m,y-radius+n)) == 0 :
iExpand.putpixel((x,y),0)
break
break
return iExpand
# 形态学闭运算
def Close(image,structure_data,radius) :
iExpand = Expand(image,structure_data,radius)
iCorrode = Corrode(iExpand,structure_data,radius)
iClose = iCorrode
return iClose
# 形态学开运算
def Open(image,structure_data,radius) :
iCorrode = Corrode(image,structure_data,radius)
iExpand = Expand(iCorrode,structure_data,radius)
iOpen = iExpand
return iOpen
# 用于实现高斯收敛的卷积运算,也适用于其他进行卷积的地方 但是速度较慢 后期取消之后影响不大
def Convolution(image,operator,radius) :
operator_num = (2 * radius + 1) ** 2
width = image.size[0]
height = image.size[1]
iConvolution = Image.new("L",(width,height),255)
for y in range(height) :
for x in range(width) :
sum = 0.0
for n in range(2 * radius + 1) :
for m in range(2 * radius + 1) :
if -1 < x - radius + m < width and -1 < y - radius + n < height :
sum += operator[(operator_num - 1) - (n*(2*radius+1) + m)] * image.getpixel((x-radius+m,y-radius+n))
else :
continue
iConvolution.putpixel((x,y),int(sum))
return iConvolution
# 普通二值化函数,将灰度图像变为二值图像,仅用在分割函数内,后面还有智能二值化函数
def Binary(image) :
width = image.size[0]
height = image.size[1]
iBinary = Image.new("L",(width,height),255)
sum = 0
for y in range(height) :
for x in range(width) :
sum += image.getpixel((x,y))
Threshold = sum / (height * width)
for y in range(height) :
for x in range(width) :
if image.getpixel((x,y)) > Threshold :
iBinary.putpixel((x,y),255)
else :
iBinary.putpixel((x,y),0)
return iBinary
# 分割第一步 输入:原图,形态学结构算子,结构算子半径
def Segment_First(image,structure_data,radius1) :
(iMag,X_Grad,Y_Grad) = Magnitude(image)
# iConvolution = Convolution(iMagnitude,operator,radius2)
iBinary = Binary(iMag)
iClose = Close(iBinary,structure_data,radius1)
iOpen = Open(iClose,structure_data,radius1)
height = image.size[1]
width = image.size[0]
iRebuild = Image.new("L",(width,height),255)
for y in range(height) :
for x in range(width) :
if iOpen.getpixel((x,y)) == 0 :
iRebuild.putpixel((x,y),255)
else :
iRebuild.putpixel((x,y),0)
return iRebuild
# 分割第二步 输入: 原图,梯度总矢量图,梯度x分量图,梯度y分量图
def Segment_Second(image,image0,image1,image2) :
width = image.size[0]
height = image.size[1]
#iFirst = Image.new("L",(width,height),255)
#Draw = ImageDraw.Draw(iFirst)
iResult = Image.new("L",(width,height),255)
a = []
b = []
temp = 0
num = 0
for each in range(3) :
if each == 0 :
a = []
b = []
iTemp = image0.copy()
# 梯度垂直
for x in range(width) :
for y in range(height) :
temp += iTemp.getpixel((x,y))
a.append(temp)
temp = 0
MeanH = sum(a) / width
for one in range(len(a)) :
if a[one] < MeanH :
num += 1
temp += a[one]
Lower_MeanH = temp / num
temp = 0
num = 0
for i in range(len(a)) :
if a[i] > Lower_MeanH :
left0 = i
break
for i in range(len(a)) :
if a[len(a)-i-1] > Lower_MeanH :
right0 = len(a) - i - 1
break
# 梯度水平
for y in range(height) :
for x in range(width) :
temp += iTemp.getpixel((x,y))
b.append(temp)
temp = 0
MeanV = sum(b) / height
for one in range(len(b)) :
if b[one] < MeanV :
num += 1
temp += b[one]
Lower_MeanV = temp / num
temp = 0
num = 0
for j in range(len(b)) :
if b[j] > Lower_MeanV :
top0 = j
break
for j in range(len(b)) :
if b[len(b)-j-1] > Lower_MeanV :
bottom0 = len(b) - j - 1
break
elif each == 1 :
a = []
b = []
iTemp = image1.copy()
# 梯度x分量垂直
for x in range(width) :
for y in range(height) :
temp += iTemp.getpixel((x,y))
a.append(temp)
temp = 0
MeanH = sum(a) / width
for one in range(len(a)) :
if a[one] < MeanH :
num += 1
temp += a[one]
Lower_MeanH = temp / num
temp = 0
num = 0
for i in range(len(a)) :
if a[i] > Lower_MeanH :
left1 = i
break
for i in range(len(a)) :
if a[len(a)-i-1] > Lower_MeanH :
right1 = len(a) - i - 1
break
# 梯度x分量水平
for y in range(height) :
for x in range(width) :
temp += iTemp.getpixel((x,y))
b.append(temp)
temp = 0
MeanV = sum(b) / height
for one in range(len(b)) :
if b[one] < MeanV :
num += 1
temp += b[one]
Lower_MeanV = temp / num
temp = 0
num = 0
for j in range(len(b)) :
if b[j] > Lower_MeanV :
top1 = j
break
for j in range(len(b)) :
if b[len(b)-j-1] > Lower_MeanV :
bottom1 = len(b) - j - 1
break
elif each == 2 :
a = []
b = []
iTemp = image2.copy()
# 梯度y分量垂直
for x in range(width) :
for y in range(height) :
temp += iTemp.getpixel((x,y))
a.append(temp)
temp = 0
MeanH = sum(a) / width
for one in range(len(a)) :
if a[one] < MeanH :
num += 1
temp += a[one]
Lower_MeanH = temp / num
temp = 0
num = 0
for i in range(len(a)) :
if a[i] > Lower_MeanH :
left2 = i
break
for i in range(len(a)) :
if a[len(a)-i-1] > Lower_MeanH :
right2 = len(a) - i - 1
break
# 梯度y分量水平
for y in range(height) :
for x in range(width) :
temp += iTemp.getpixel((x,y))
b.append(temp)
temp = 0
MeanV = sum(b) / height
for one in range(len(b)) :
if b[one] < MeanV :
num += 1
temp += b[one]
Lower_MeanV = temp / num
temp = 0
num = 0
for j in range(len(b)) :
if b[j] > Lower_MeanV :
top2 = j
break
for j in range(len(b)) :
if b[len(b)-j-1] > Lower_MeanV :
bottom2 = len(b) - j - 1
break
'''
for k in range(width) :
a[k] = a[k]/height
x = (k,255)
y = (k,255-a[k])
Draw.line((x,y),fill = 20)
'''
left = [left0,left1,left2]
right = [right0,right1,right2]
top = [top0,top1,top2]
bottom = [bottom0,bottom1,bottom2]
final_left = max(left)
final_right = min(right)
final_top = max(top)
final_bottom = min(bottom)
print final_left,final_right,final_top,final_bottom
for y in range(height) :
for x in range(width) :
if y in range(final_top,final_bottom) and x in range(final_left,final_right) :
iResult.putpixel((x,y),0)
else :
iResult.putpixel((x,y),255)
return iResult
# 分割第三步 输入 :原图,分割第一步的返回值,分割第二步返回值 ,返回二值化的分割结果和正常分割结果
def Segment_Final(image,image1,image2) :
width = image1.size[0]
height = image1.size[1]
iFinal_Binary = Image.new("L",(width,height),255)
iFinal = Image.new("L",(width,height),255)
pix1 = image.load()
pix2 = iFinal.load()
for y in range(height) :
for x in range(width) :
temp1 = image1.getpixel((x,y))
temp2 = image2.getpixel((x,y))
if temp1 == temp2 and temp1 != 255 :
iFinal_Binary.putpixel((x,y),0)
pix2[x,y] = pix1[x,y]
elif temp1 != temp2 :
iFinal_Binary.putpixel((x,y),255)
pix2[x,y] = 255
return (iFinal_Binary,iFinal)
# 分割主程序 综合前三步骤,得到分割后的图像 输入:原图
def Segment(image) :
(iMag,x_Grad,y_Grad) = Magnitude(image)
iMag = iMag.convert("L")
x_Grad = x_Grad.convert("L")
y_Grad = y_Grad.convert("L")
iRebuild = Segment_First(image,structure_data_two,3)
iResult = Segment_Second(image,iMag,x_Grad,y_Grad)
iRebuild = iRebuild.convert("L")
iResult = iResult.convert("L")
(iFinal_Binary,iFinal) = Segment_Final(image,iRebuild,iResult)
return (iFinal_Binary,iFinal)
# ******************************************************************* #
# ******************************************************************* #
# 灰度均衡算法,使指纹图像对比更强烈 输入:分割后的正常图 有点问题
def Equalize(image) :
a = [0] * 256
width = image.size[0]
height = image.size[1]
iEqualize = Image.new("L",(width,height),255)
pix1 = image.load()
pix2 = iEqualize.load()
for y in range(height) :
for x in range(width) :
iGray = int(pix1[x,y])
a[iGray] = a[iGray] + 1
for m in range(1,256) :
a[m] = a[m] + a[m-1]
sum = max(a)
for k in range(256) :
a[k] = a[k] * 255 / sum
for y in range(height) :
for x in range(width) :
pix2[x,y] = a[int(pix1[x,y])]
return iEqualize
# *************************************************************** #
# 指纹平滑算法,运用一些平滑算子对图像进行平滑处理 这里用的是[1.0/9] * 9
def Smooth(image) :
width = image.size[0]
height = image.size[1]
iSmooth = Image.new("L",(width,height),255)
array = [1.0/9]*9
for i in range(height) :
for j in range(width) :
if i in [0,height-1] or j in [0,width-1] :
iSmooth.putpixel((j,i),image.getpixel((j,i)))
else :
a = [0] * 9
for k in range(3) :
for m in range(3) :
a[k*3+m] = image.getpixel((j-1+m,i-1+k))
sum = 0
for l in range(9) :
sum = sum + array[l]*a[l]
iSmooth.putpixel((j,i),int(sum))
return iSmooth
# ******************************************************************** #
# ******************************************************************** #
# 智能增强函数,第一步使用Gabor滤波器使纹线黑的更黑,白的更白。
# 但是需要一个预处理,对方向图进行编码输出
Direction_Block = [(-4,0),(-2,0),(-1,0),(0,0),(1,0),(2,0),(4,0),
(-4,-2),(-2,-1),(-1,-1),(0,0),(1,1),(2,1),(4,2),
(-4,-4),(-2,-2),(-1,-1),(0,0),(1,1),(2,2),(4,4),
(-2,-4),(-1,-2),(0,-1),(0,0),(0,1),(1,2),(2,4),
(0,-4),(0,-2),(0,-1),(0,0),(0,1),(0,2),(0,4),
(-4,2),(-2,1),(-1,1),(0,0),(1,-1),(2,-1),(4,-2),
(-4,4),(-2,2),(-1,1),(0,0),(1,-1),(2,-2),(4,-4),
(-2,4),(-1,2),(0,1),(0,0),(0,-1),(1,-2),(2,-4)]
Hw = [1,1,1,1,1,1,1] # Gabor 平行模板
Vw = [-3,-1,3,9,3,-1,-3] # Gabor 垂直模板
Wh = [2,2,3,4,3,2,3] # 智能二值化水平权值
Wv = [1,1,1,1,1,1,1] # 智能二值化垂直权值
def Direction_Index(tuple) :
if tuple == (255,0,255) :
return 0 # 0度
elif tuple == (255,255,0) :
return 1
elif tuple == (0,0,255) :
return 2
elif tuple == (0,255,0) :
return 3
elif tuple == (255,0,0) :
return 4
elif tuple == (0,255,255) :
return 5
elif tuple == (255,255,255) :
return 6
elif tuple == (0,0,0) :
return 7 # 157.5度
def Gabor_Enhance(image,image_Dir) :
width = image.size[0]
height = image.size[1]
iGabor = Image.new("L",(width,height),255)
iTemp = Image.new("L",(width,height),255)
Direction_Block_Width = 7
for y in range(height) :
for x in range(width) :
h_tuple = image_Dir.getpixel((x,y))
d = Direction_Index(h_tuple)
sum = 0
hsum = 0
for k in range(7) :
if (y + Direction_Block[d * Direction_Block_Width + k][1] < 0 or y + Direction_Block[d * Direction_Block_Width + k][1] >= height
or x + Direction_Block[d * Direction_Block_Width + k][0] < 0 or x + Direction_Block[d * Direction_Block_Width + k][0] >= width) :
continue
else :
sum += Hw[k] * image.getpixel((x + Direction_Block[d * Direction_Block_Width + k][0],y + Direction_Block[d * Direction_Block_Width + k][1]))
hsum += Hw[k]
if (hsum != 0) :
iTemp.putpixel((x,y),uint8(sum/hsum))
else :
iTemp.putpixel((x,y),255)
for y in range(height) :
for x in range(width) :
v_tuple = image_Dir.getpixel((x,y))
d = (Direction_Index(v_tuple) + 4) % 8
sum = 0
vsum = 0
for k in range(7) :
if(y + Direction_Block[d * Direction_Block_Width + k][1] < 0 or y + Direction_Block[d * Direction_Block_Width + k][1] >= height
or x + Direction_Block[d * Direction_Block_Width + k][0] < 0 or x + Direction_Block[d * Direction_Block_Width + k][0] >= width) :
continue
else :
sum += Vw[k] * iTemp.getpixel((x + Direction_Block[d * Direction_Block_Width + k][0],y + Direction_Block[d * Direction_Block_Width + k][1]))
vsum += Vw[k]
if vsum > 0 :
sum /= vsum
if sum >255 :
iGabor.putpixel((x,y),255)
elif sum < 0 :
iGabor.putpixel((x,y),0)
else :
iGabor.putpixel((x,y),uint8(sum))
else :
iGabor.putpixel((x,y),255)
return iGabor
# 第二步:智能二值化,噪声比较多,需要继续去噪,输入:Gabor滤波后的图片
def AI_Binary(image,image_Dir) :
width = image.size[0]
height = image.size[1]
iBinary = Image.new("L",(width,height),255)
Direction_Block_Width = 7
avrH = 0
avrV = 0
for y in range(height) :
for x in range(width) :
if image.getpixel((x,y)) < 4 :
iBinary.putpixel((x,y),0)
continue
h_tuple = image_Dir.getpixel((x,y))
d = Direction_Index(h_tuple)
sum = 0
hsum = 0
for k in range(7) :
if (y + Direction_Block[d * Direction_Block_Width + k][1] < 0 or y + Direction_Block[d * Direction_Block_Width + k][1] >= height
or x + Direction_Block[d * Direction_Block_Width + k][0] < 0 or x + Direction_Block[d * Direction_Block_Width + k][0] >= width) :
continue
else :
sum += Wh[k] * image.getpixel((x + Direction_Block[d * Direction_Block_Width + k][0],y + Direction_Block[d * Direction_Block_Width + k][1]))
hsum += Wh[k]
if hsum != 0 :
avrH = uint8(sum / hsum)
else :
avrH = 255
d = (d + 4) % 8
sum = 0
vsum = 0
for k in range(7) :
if (y + Direction_Block[d * Direction_Block_Width + k][1] < 0 or y + Direction_Block[d * Direction_Block_Width + k][1] >= height
or x + Direction_Block[d * Direction_Block_Width + k][0] < 0 or x + Direction_Block[d * Direction_Block_Width + k][0] >= width) :
continue
else :
sum += Wv[k] * image.getpixel((x + Direction_Block[d * Direction_Block_Width + k][0],y + Direction_Block[d * Direction_Block_Width + k][1]))
vsum += Wv[k]
if vsum != 0 :
avrV = sum / vsum
if avrV > 255 :
avrV = 255
elif avrV < 0 :
avrV = 0
else :
avrV = uint8(avrV)
else :
avrV = 255
if avrH < avrV :
iBinary.putpixel((x,y),0)
else :
iBinary.putpixel((x,y),255)
return iBinary
# 第三步:去噪 ,去掉二值化后的噪声 输入:上一步得到的智能二值化图片
def Denoising(image) :
width = image.size[0]
height = image.size[1]
a = [-1,0,1]
iDenoising = Image.new("L",(width,height),255)
for y in range(height - 1) :
for x in range(width - 1) :
if image.getpixel((x,y)) == 255 :
continue
else :
num = 0
for n in range(3) :
for m in range(3) :
if n != 0 and m != 0 :
if image.getpixel((x+a[m],y+a[n])) == image.getpixel((x,y)) :
num += 1
else :
continue
if num < 3 :
iDenoising.putpixel((x,y),255-image.getpixel((x,y)))
else :
iDenoising.putpixel((x,y),image.getpixel((x,y)))
return iDenoising
# 最后一个综合函数,实现增强以及后处理,输入:分割后的正常图,分割后的方向图
def Enhance(image,direction) :
width = image.size[0]
height = image.size[1]
iGabor = Image.new("L",(width,height),255)
iBinary = Image.new("L",(width,height),255)
iEnhance = Image.new("L",(width,height),255)
iGabor = Gabor_Enhance(image,direction)
iBinary = AI_Binary(iGabor,direction)
iEnhance = Denoising(iBinary)
return iEnhance
# ************************************************************************* #
# ************************************************************************* #
# 指纹细化。这是处理中的最后一步,细化完了同样需要去噪
Thin_Table = [
0,0,1,1,0,0,1,1, 1,1,0,1,1,1,0,1,
1,1,0,0,1,1,1,1, 0,0,0,0,0,0,0,1,
0,0,1,1,0,0,1,1, 1,1,0,1,1,1,0,1,
1,1,0,0,1,1,1,1, 0,0,0,0,0,0,0,1,
1,1,0,0,1,1,0,0, 0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
1,1,0,0,1,1,0,0, 1,1,0,1,1,1,0,1,
0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
0,0,1,1,0,0,1,1, 1,1,0,1,1,1,0,1,
1,1,0,0,1,1,1,1, 0,0,0,0,0,0,0,1,
0,0,1,1,0,0,1,1, 1,1,0,1,1,1,0,1,
1,1,0,0,1,1,1,1, 0,0,0,0,0,0,0,0,
1,1,0,0,1,1,0,0, 0,0,0,0,0,0,0,0,
1,1,0,0,1,1,1,1, 0,0,0,0,0,0,0,0,
1,1,0,0,1,1,0,0, 1,1,0,1,1,1,0,0,
1,1,0,0,1,1,1,0, 1,1,0,0,1,0,0,0]
# 上面是细化表,这里是细化算法,仅保留每条纹线的一个像素。 输入:Gabor增强后的图片
def Thin(image) :
height = image.size[1]
width = image.size[0]
iThin = Image.new("L",(width,height),255)
iThin = image.copy()
for y in range(height) :
for x in range(width) :
if image.getpixel((x,y)) == 0 :
a = [1] * 9
for j in range(3) :
for i in range(3) :
if -1 < y-1+j < height and -1 < x-1+i < width and iThin.getpixel((x-1+i,y-1+j)) == 0 :
a[j*3+i] = 0
sum = a[0] * 1 + a[1] * 2 + a[2] * 4 + a[3] * 8 + a[5] * 16 + a[6] * 32 + a[7] * 64 + a[8] * 128
iThin.putpixel((x,y),Thin_Table[sum] * 255)
return iThin
# 指纹细化后的去噪算法 !!! we are done 后续处理很好 加了一层新的循环 去掉前一次得到的孤立点
# 输入 : 细化后的图片,len是想要清除的短棒的长度,自定义
def Thin_Pro(image,len) :
width = image.size[0]
height = image.size[1]
iThin_Pro = image.copy()
for y in range(height) :
for x in range(width) :
if image.getpixel((x,y)) != 0 :
continue
else :
num = 0
a = []
for n in range(3) :
for m in range(3) :
if m == 1 and n == 1 :
continue
else :
if -1<y-1+n<height and -1<x-1+m<width and image.getpixel((x-1+m,y-1+n)) == 0 :
tuple = (x-1+m,y-1+n) # 记录可能的周边点坐标 为后来清除短棒做准备
a.append(tuple)
num += 1
if num == 0 :
iThin_Pro.putpixel((x,y),255)
continue
elif num == 1 :
for k in range(1,len,1) :
temp_1 = (k*(a[0][0]-x),k*(a[0][1]-y))
if image.getpixel((a[0][0]+temp_1[0],a[0][1]+temp_1[1])) == 0 :
n += 1
continue
else :
break
if n <= len :
for i in range(n) :
temp_2 = (i*(a[0][0]-x),i*(a[0][1]-y))
iThin_Pro.putpixel((a[0][0]+temp_2[0],a[0][1]+temp_2[1]),255)
for y in range(height) :
for x in range(width) :
if iThin_Pro.getpixel((x,y)) == 0 :
if (iThin_Pro.getpixel((x-1,y)) and iThin_Pro.getpixel((x+1,y)) and iThin_Pro.getpixel((x,y-1)) and iThin_Pro.getpixel((x,y+1)) and iThin_Pro.getpixel((x-1,y-1)) and iThin_Pro.getpixel((x+1,y-1)) and iThin_Pro.getpixel((x-1,y+1)) and iThin_Pro.getpixel((x+1,y+1))) == 0 :
continue
else : iThin_Pro.putpixel((x,y),255)
else :
continue
return iThin_Pro
# 同理,来一个综合函数,直接得到最终结果
def Thinning(image,len) :
width = image.size[0]
height = image.size[1]
iThin = Image.new("L",(width,height),255)
iThin = Thin(image)
iThin = Thin_Pro(iThin,len)
return iThin
# image1:细化图,image2:方向图 image3:前后背景图 mode: 检测模式(奇异点类型)
def IsSingular(image1,image2,image3,mode) :
width = image1.size[0]
height = image1.size[1]
copy = image1.copy()
Draw = ImageDraw.Draw(copy)
pix1 = image1.load()
pix2 = image2.load()
pix3 = image3.load()
singular = []
fg = False
for y in range(3,height-3) :
for x in range(3,width-3) :
if pix3[x,y] == 255 :
continue
fg = False
for i in range(24) :
if pix3[cell7[i][0]+x,cell7[i][1]+y] == 255 :
fg = True
break
if fg :
continue
sum1 = 0
for i in range(8) :
a1 = pix2[cell3[i][0]+x,cell3[i][1]+y]/24
a2 = pix2[cell3[(i+1)%8][0]+x,cell3[(i+1)%8][1]+y]/24
d = get_angle(a1,a2,mode)
if abs(d) > 5 :
break
sum1 += d
sum2 = 0
for i in range(12) :
a1 = pix2[cell5[i][0]+x,cell5[i][1]+y]/24
a2 = pix2[cell5[(i+1)%12][0]+x,cell5[(i+1)%12][1]+y]/24
d = get_angle(a1,a2,mode)
if abs(d) > 5 :
break
sum2 += d
if mode == -1 :
value = -10
elif mode == 1 :
value = 10
if sum2 == value and sum1 == value :
singular.append((x,y))
print singular
# 去除靠近边缘的奇异点
flag = 0
N_singular = deepcopy(singular)
for one in singular :
for n in range(-16,17) :
for m in range(-16,17) :
tempx = one[0] + m
tempy = one[1] + n
if (-1<tempx<width and -1<tempy<height and pix3[tempx,tempy] == 255) :
flag = 1
N_singular.remove(one)
break
elif (tempx<0 or tempx>=width or tempy<0 or tempy>=height) :
flag == 2
N_singular.remove(one)
else :
continue
if flag == 1 or flag == 2 :
flag = 0
break
else :
continue
for one in N_singular :
Draw.ellipse((one[0]-2,one[1]-2,one[0]+2,one[1]+2),fill = 255,outline = 0)
return copy,len(N_singular)
def ColorToGrey(image) :
width = image.size[0]
height = image.size[1]
grey = Image.new("L",(width,height),'white')
pix1 = image.load()
pix2 = grey.load()
for y in range(height) :
for x in range(width) :
if pix1[x,y] == (255,0,255) :
pix2[x,y] = 0
elif pix1[x,y] == (255,255,0) :
pix2[x,y] = 23
elif pix1[x,y] == (0,0,255) :
pix2[x,y] = 45
elif pix1[x,y] == (0,255,0) :
pix2[x,y] = 68
elif pix1[x,y] == (255,0,0) :
pix2[x,y] = 90
elif pix1[x,y] == (0,255,255) :
pix2[x,y] = 113
elif pix1[x,y] == (255,255,255) :
pix2[x,y] = 135
elif pix1[x,y] == (0,0,0) :
pix2[x,y] = 158
return grey
im = Image.open("D:\\Python2.7\\image\\sample2\\9.bmp").convert("L")
d = Image.open("D:\\Python2.7\\image\\sample2\\c.bmp")
divide = Image.open("D:\\Python2.7\\image\\sample2\\Divide.bmp").convert("L")
thin = Image.open("D:\\Python2.7\\image\\sample2\\Thin.bmp").convert("L")
IsSingular(thin,ColorToGrey(d),divide,1)
#iDirection_Dot,iDirection_succession = Direction(im,divide)
#iDirection_succession.show()
#g = Enhance(im,d)
#g.show()
'''
# ************************************************************************* #
# ************************************************************************* #
# 这里进入第二部分,识别特征点和奇异点。特征点主要包括:端点、叉点;奇异点包括:core,delta,whorl
# 该函数用于识别端点和叉点,并且去掉不符合条件的端点和叉点,端点已调好,叉点还没加上
# image1 : 细化图 image2 : 分割后的黑白图
def IsFeature(image1,image2) :
width = image1.size[0]
height = image1.size[1]
pix1 = image1.load()
pix2 = image2.load()
copy = image1.copy()
Draw = ImageDraw.Draw(copy)
dot = []
fork = []
for y in range(height) :
for x in range(width) :
if 0 < x < width-1 and 0 < y < height-1 and pix1[x,y] == 0 :
a = pix1[x-1,y+1]
b = pix1[x,y+1]
c = pix1[x+1,y+1]
d = pix1[x-1,y]
f = pix1[x+1,y]
g = pix1[x-1,y-1]
h = pix1[x,y-1]
i = pix1[x+1,y-1]
sum = abs(a-b) + abs(b-c) + abs(c-f) + abs(f-i) + abs(i-h) + abs(h-g) + abs(g-d) + abs(d-a)
if sum == 2 * 255 :
Draw.ellipse((x-1,y-1,x+1,y+1),fill = 255,outline = 255)
dot.append((x,y))
elif sum == 6 * 255 :
Draw.ellipse((x-1,y-1,x+1,y+1),fill = 255,outline = 255)
fork.append((x,y))
else :
continue
# 去除边缘端点 思路:根据是否靠近背景块来判断 使用了黑白分明的背景块图片1_rebuild.bmp
flag1 = 0
flag2 = 0
dot1 = deepcopy(dot)
for one in dot :
for n in range(32) :
for m in range(32) :
tempx = one[0] - 16 + m
tempy = one[1] - 16 + n
if (-1<tempx<width and -1<tempy<height and pix2[tempx,tempy] == 255) :
Draw.ellipse((one[0]-1,one[1]-1,one[0]+1,one[1]+1),fill = 255,outline = 255)