-
Notifications
You must be signed in to change notification settings - Fork 11
/
wahab
executable file
·109 lines (81 loc) · 3.04 KB
/
wahab
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
#!/usr/bin/env python3
import matplotlib.pyplot as plt
import numpy as np
from optparse import OptionParser
from skimage import draw
import imageio
import utils
parser = OptionParser(usage="%prog [options] sourceimage [destinationimage]")
parser.add_option("-i", dest="images", default=0, action="count",
help="Show intermediate images.")
parser.add_option("-d", "--dry-run", dest="dryrun", default=False, action="store_true",
help="Do not save the result.")
parser.add_option("-b", "--no-binarization", dest="binarize", default=True, action="store_false",
help="Use this option to disable the final binarization step")
(options, args) = parser.parse_args()
if len(args) == 0 or len(args) > 2:
parser.print_help()
exit(1)
sourceImage = args[0]
if len(args) == 1:
destinationImage = args[0]
else:
destinationImage = args[1]
def wahabKernel(size, angle):
y = int(np.sin(angle) * size)
x = int(np.cos(angle) * size)
kernel = np.zeros((np.abs(y) + 1, np.abs(x) + 1))
if y < 0:
rr, cc = draw.line(0, 0, y, x)
else:
rr, cc = draw.line(-y, 0, 0, x)
kernel[rr, cc] = 1.0
return kernel
def wahabFilter(image, orientations, w=8):
result = np.empty(image.shape)
height, width = image.shape
for y in range(0, height - w, w):
for x in range(0, width - w, w):
orientation = orientations[y+w//2, x+w//2]
kernel = wahabKernel(16, orientation)
result[y:y+w, x:x+w] = utils.convolve(image, kernel, (y, x), (w, w))
result[y:y+w, x:x+w] /= np.sum(kernel)
return result
if __name__ == '__main__':
np.set_printoptions(
threshold=np.inf,
precision=4,
suppress=True)
print("Reading image")
image = imageio.v2.imread(sourceImage).astype("float64")
if options.images > 0:
utils.showImage(image, "original", vmax=255.0)
print("Normalizing")
image = utils.normalize(image)
if options.images > 1:
utils.showImage(image, "normalized")
print("Finding mask")
mask = utils.findMask(image)
if options.images > 1:
utils.showImage(mask, "mask")
print("Applying local normalization")
image = np.where(mask == 1.0, utils.localNormalize(image), image)
if options.images > 1:
utils.showImage(image, "locally normalized")
print("Estimating orientations")
orientations = np.where(mask == 1.0, utils.estimateOrientations(image, interpolate=False), -1.0)
if options.images > 0:
utils.showOrientations(image, orientations, "orientations", 8)
print("Filtering")
image = np.where(mask == 1.0, wahabFilter(image, orientations), 1.0)
if options.images > 0:
utils.showImage(image, "filtered")
if options.binarize:
print("Binarizing")
image = np.where(mask == 1.0, utils.binarize(image), 1.0)
if options.images > 0:
utils.showImage(image, "binarized")
if options.images > 0:
plt.show()
if not options.dryrun:
imageio.v2.imwrite(destinationImage, image)