-
Notifications
You must be signed in to change notification settings - Fork 676
/
Skimage Filters
175 lines (138 loc) · 4.25 KB
/
Skimage Filters
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
import cv2
img = cv2.imread('/Volumes/16 DOS/Python/StreetCars.png')
gray0 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
import numpy as np
import matplotlib.pyplot as plt
from scipy.ndimage import gaussian_filter
from skimage.morphology import reconstruction
image = gaussian_filter(gray, 1)
seed = np.copy(image)
seed[1:-1, 1:-1] = image.min()
mask = image
dilated = reconstruction(seed, mask, method='dilation')
plt.imshow(dilated)
from skimage.filters import sobel
elevation_map = sobel(image)
plt.imshow(elevation_map)
markers = np.zeros_like(image)
markers[image < 100] = 1
markers[image > 150] = 2
### TREES
from skimage.morphology import watershed
segmentation = watershed(elevation_map, markers)
plt.imshow(segmentation)
####### FOURIER
'''http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_transforms/py_fourier_transform/py_fourier_transform.html'''
f = np.fft.fft2(im2)
fshift = np.fft.fftshift(f)
magnitude_spectrum = 20*np.log(np.abs(fshift))
plt.subplot(121),plt.imshow(im2, cmap = 'gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(magnitude_spectrum, cmap = 'gray')
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.show()
###
rows, cols = im.shape
crow,ccol = int(rows/2) , int(cols/2)
fshift[crow-30:crow+30, ccol-30:ccol+30] = 0
f_ishift = np.fft.ifftshift(fshift)
img_back = np.fft.ifft2(f_ishift)
img_back = np.abs(img_back)
plt.subplot(121),plt.imshow(im, cmap = 'gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(121),plt.imshow(img_back, cmap = 'gray')
plt.title('Image after HPF'), plt.xticks([]), plt.yticks([])
plt.subplot(121),plt.imshow(img_back)
plt.title('Result in JET'), plt.xticks([]), plt.yticks([])
plt.show()
# Calculate gradient
gx = cv2.Sobel(im, cv2.CV_32F, 1, 0, ksize=1)
gy = cv2.Sobel(im, cv2.CV_32F, 0, 1, ksize=1)
plt.imshow(gy)
mag, angle = cv2.cartToPolar(gx, gy, angleInDegrees=True)
plt.imshow(angle)
##
from skimage.filter import threshold_otsu, threshold_adaptive
global_thresh = threshold_otsu(gray0.astype(np.float32))
binary_global = gray0.astype(np.float32) > global_thresh
plt.imshow(binary_global)
block_size = 45
binary_adaptive = threshold_adaptive(gray0, block_size, offset=5)
plt.imshow(binary_adaptive)
from skimage.filters import gabor
from skimage import data, io
from matplotlib import pyplot as plt
#### ESSE E FODA !!!!!
filt_real, filt_imag = gabor(gray0, frequency=2.1)
plt.figure()
io.imshow(filt_real)
io.show()
plt.figure()
io.imshow(filt_imag)
io.show()
##
from skimage.filters import gaussian, gaussian_filter, laplace,prewitt
from skimage.filters import prewitt_v,prewitt_h,scharr, wiener
gauss=gaussian(gray0, sigma=5, multichannel=True)
plt.imshow(gauss)
gauss2=gaussian_filter(gray0, sigma=5, multichannel=True)
plt.imshow(gauss2)
lap=laplace(gray0,ksize=100)
plt.imshow(lap)
pre=prewitt(gray0, mask=None)
plt.imshow(pre)
pre_v=prewitt_v(gray0, mask=None)
plt.imshow(pre_v)
from skimage import filters
edges2 = filters.roberts(gray0)
plt.imshow(edges2)
plt.imshow(scharr(gray0))
plt.imshow(threshold_mean(gray0))
plt.imshow(wiener(gray0))
#######################################
plt.imshow(img)
plt.imshow(gray0)
plt.imshow(image)
### TREES
plt.imshow(segmentation)
### CONTOURS
plt.imshow(img_back, cmap = 'gray')
### STREET
plt.imshow(gy)
plt.imshow(angle)
plt.imshow(binary_adaptive)
plt.imshow(binary_global)
#### STREET CALÇADA TREES BEST
io.imshow(filt_real)
plt.imshow(gauss)
plt.imshow(lap)
plt.imshow(pre)
plt.imshow(pre_v)
#### CARS CONTOURS
plt.imshow(edges2)
plt.imshow(scharr(gray0))
plt.figure(figsize=(12,12))
ax = plt.subplot(1, 2, 1)
plt.imshow(img)
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.title('ORIGINAL IMAGE')
ax = plt.subplot(1, 2, 2)
plt.imshow(gy)
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.title('SOBEL FILTER')
plt.show()
plt.figure(figsize=(10,10))
ax = plt.subplot(2, 2, 1)
io.imshow(filt_real)
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.title('GABOR FILTER')
ax = plt.subplot(2, 2, 2)
plt.imshow(edges2)
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.title('ROBERTS FILTER')
plt.show()