1
+ #!/usr/bin/env python3
2
+
3
+ import matplotlib .pyplot as plt
4
+ import numpy as np
5
+ from optparse import OptionParser
6
+ import scipy .misc as misc
7
+ import scipy .ndimage as ndimage
8
+ from skimage import draw
9
+
10
+ import utils
11
+ from utils import showImage
12
+
13
+ parser = OptionParser (usage = "%prog [options] sourceimage [destinationimage]" )
14
+
15
+ parser .add_option ("-i" , dest = "images" , default = 0 , action = "count" ,
16
+ help = "Show intermediate images." )
17
+
18
+ parser .add_option ("-d" , "--dry-run" , dest = "dryrun" , default = False , action = "store_true" ,
19
+ help = "Do not save the result." )
20
+
21
+ parser .add_option ("-b" , "--no-binarization" , dest = "binarize" , default = True , action = "store_false" ,
22
+ help = "Use this option to disable the final binarization step" )
23
+
24
+ (options , args ) = parser .parse_args ()
25
+
26
+ if len (args ) == 0 or len (args ) > 2 :
27
+ parser .print_help ()
28
+ exit (1 )
29
+
30
+ sourceImage = args [0 ]
31
+ if len (args ) == 1 :
32
+ destinationImage = args [0 ]
33
+ else :
34
+ destinationImage = args [1 ]
35
+
36
+ def wahabKernel (size , angle ):
37
+ y = int (np .sin (angle ) * size )
38
+ x = int (np .cos (angle ) * size )
39
+
40
+ kernel = np .zeros ((np .abs (y ) + 1 , np .abs (x ) + 1 ))
41
+
42
+ if y < 0 :
43
+ rr , cc = draw .line (0 , 0 , y , x )
44
+ else :
45
+ rr , cc = draw .line (- y , 0 , 0 , x )
46
+
47
+ kernel [rr , cc ] = 1.0
48
+ return kernel
49
+
50
+
51
+ def wahabFilter (image , orientations , w = 8 ):
52
+ result = np .empty (image .shape )
53
+
54
+ height , width = image .shape
55
+ for y in range (0 , height - w , w ):
56
+ for x in range (0 , width - w , w ):
57
+ orientation = orientations [y + w // 2 , x + w // 2 ]
58
+ kernel = wahabKernel (16 , orientation )
59
+ result [y :y + w , x :x + w ] = utils .convolve (image , kernel , (y , x ), (w , w ))
60
+ result [y :y + w , x :x + w ] /= np .sum (kernel )
61
+
62
+ return result
63
+
64
+
65
+ if __name__ == '__main__' :
66
+ np .set_printoptions (
67
+ threshold = np .inf ,
68
+ precision = 4 ,
69
+ suppress = True )
70
+
71
+ print ("Reading image" )
72
+ image = ndimage .imread (sourceImage , mode = "L" ).astype ("float64" )
73
+ if options .images > 0 :
74
+ utils .showImage (image , "original" , vmax = 255.0 )
75
+
76
+ print ("Normalizing" )
77
+ image = utils .normalize (image )
78
+ if options .images > 1 :
79
+ utils .showImage (image , "normalized" )
80
+
81
+ print ("Finding mask" )
82
+ mask = utils .findMask (image )
83
+ if options .images > 1 :
84
+ utils .showImage (mask , "mask" )
85
+
86
+ print ("Applying local normalization" )
87
+ image = np .where (mask == 1.0 , utils .localNormalize (image ), image )
88
+ if options .images > 1 :
89
+ utils .showImage (image , "locally normalized" )
90
+
91
+ print ("Estimating orientations" )
92
+ orientations = np .where (mask == 1.0 , utils .estimateOrientations (image , interpolate = False ), - 1.0 )
93
+ if options .images > 0 :
94
+ utils .showOrientations (image , orientations , "orientations" , 8 )
95
+
96
+ print ("Filtering" )
97
+ image = np .where (mask == 1.0 , wahabFilter (image , orientations ), 1.0 )
98
+ if options .images > 0 :
99
+ utils .showImage (image , "filtered" )
100
+
101
+ if options .binarize :
102
+ print ("Binarizing" )
103
+ image = np .where (mask == 1.0 , utils .binarize (image ), 1.0 )
104
+ if options .images > 0 :
105
+ utils .showImage (image , "binarized" )
106
+
107
+ if options .images > 0 :
108
+ plt .show ()
109
+
110
+ if not options .dryrun :
111
+ misc .imsave (destinationImage , image )
0 commit comments