-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommonfunctions.py
105 lines (79 loc) · 3.04 KB
/
commonfunctions.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
import skimage.io as io
import matplotlib.pyplot as plt
import numpy as np
from skimage.exposure import histogram
from matplotlib.pyplot import bar
from skimage.color import rgb2gray,rgb2hsv
# Convolution:
from scipy.signal import convolve2d
from scipy import fftpack
import math
from skimage.util import random_noise
from skimage.filters import median
from skimage.feature import canny
from mpl_toolkits.mplot3d import Axes3D # noqa: F401 unused import
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np
# Edges
from skimage.filters import sobel_h, sobel, sobel_v,roberts, prewitt
from skimage.filters import threshold_otsu
# Show the figures / plots inside the notebook
def show_images(images,titles=None):
#This function is used to show image(s) with titles by sending an array of images and an array of associated titles.
# images[0] will be drawn with the title titles[0] if exists
# You aren't required to understand this function, use it as-is.
n_ims = len(images)
if titles is None: titles = ['(%d)' % i for i in range(1,n_ims + 1)]
fig = plt.figure()
n = 1
for image,title in zip(images,titles):
a = fig.add_subplot(1,n_ims,n)
if image.ndim == 2:
plt.gray()
plt.imshow(image)
a.set_title(title)
plt.axis('off')
n += 1
fig.set_size_inches(np.array(fig.get_size_inches()) * n_ims)
plt.show()
def show_3d_image(img, title):
fig = plt.figure()
fig.set_size_inches((12,8))
ax = fig.gca(projection='3d')
# Make data.
X = np.arange(0, img.shape[0], 1)
Y = np.arange(0, img.shape[1], 1)
X, Y = np.meshgrid(X, Y)
Z = img[X,Y]
# Plot the surface.
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
# Customize the z axis.
ax.set_zlim(0, 8)
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.5, aspect=5)
ax.set_title(title)
plt.show()
def show_3d_image_filtering_in_freq(img, f):
img_in_freq = fftpack.fft2(img)
filter_in_freq = fftpack.fft2(f, img.shape)
filtered_img_in_freq = np.multiply(img_in_freq, filter_in_freq)
img_in_freq = fftpack.fftshift(np.log(np.abs(img_in_freq)+1))
filtered_img_in_freq = fftpack.fftshift(np.log(np.abs(filtered_img_in_freq)+1))
show_3d_image(img_in_freq, 'Original Image')
show_3d_image(filtered_img_in_freq, 'Filtered Image')
def showHist(img):
# An "interface" to matplotlib.axes.Axes.hist() method
plt.figure()
imgHist = histogram(img, nbins=256)
bar(imgHist[1].astype(np.uint8), imgHist[0], width=0.8, align='center')
def binarize(img,thresh=None):
img_cp = img.copy()
threshold = threshold_otsu(img_cp) if thresh is None else thresh
img_cp[img_cp < threshold] = 0
img_cp[img_cp >= threshold] = 255
return img_cp