正文
55 - How to read proprietary microscope images into Python
(给使用显微镜的人看的,略)
56 - What are features in machine learning
(听不懂,寄)
57 - How to generate features in Python for machine learning
1 2 3 4 5 import matplotlib.pyplot as pltimport cv2 img = cv2.imread('images/scratch.jpg' , 0 ) plt.imshow(img, cmap='gray' )
<matplotlib.image.AxesImage at 0x181098b9220>
这个图像很难使用传统的直方图方法来分割
使用熵过滤器来分割,像素点越白,表示熵越高
1 2 3 4 5 from skimage.filters.rank import entropyfrom skimage.morphology import disk entropy_img = entropy(img, disk(1 )) plt.imshow(entropy_img, cmap='gray' )
<matplotlib.image.AxesImage at 0x1810981b2e0>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 import matplotlib.pyplot as pltimport cv2from skimage.filters.rank import entropyfrom skimage.morphology import disk img = cv2.imread('images/Yeast_Cells.png' , 0 ) entropy_img = entropy(img, disk(1 )) fig = plt.figure(figsize=(10 , 10 )) ax1 = fig.add_subplot(121 ) ax1.imshow(img, cmap='gray' ) ax1.title.set_text('img' ) ax2 = fig.add_subplot(122 ) ax2.imshow(entropy_img, cmap='gray' ) ax2.title.set_text('entropy_img' ) plt.show()
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 import matplotlib.pyplot as pltimport cv2from skimage.filters.rank import entropyfrom skimage.morphology import disk img = cv2.imread('images/Yeast_Cells.png' , 0 ) entropy_img = entropy(img, disk(1 ))from scipy import ndimage as nd gaussian_img = nd.gaussian_filter(img, sigma=3 )from skimage.filters import sobel sobel_img = sobel(img) fig = plt.figure(figsize=(10 , 10 )) ax1 = fig.add_subplot(131 ) ax1.imshow(img, cmap='gray' ) ax1.title.set_text('img' ) ax2 = fig.add_subplot(132 ) ax2.imshow(gaussian_img, cmap='gray' ) ax2.title.set_text('gaussian_img' ) ax3 = fig.add_subplot(133 ) ax3.imshow(sobel_img, cmap='gray' ) ax3.title.set_text('sobel_img' ) plt.show()
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 import matplotlib.pyplot as pltimport cv2from skimage.filters.rank import entropyfrom skimage.morphology import diskfrom scipy import ndimage as ndfrom skimage.filters import sobelimport pandas as pd img = cv2.imread('images/Yeast_Cells.png' , 0 ) img2 = img.reshape(-1 ) df = pd.DataFrame() df['Original Pixel Values' ] = img2 entropy_img = entropy(img, disk(1 )) entropy1 = entropy_img.reshape(-1 ) df['Entropy' ] = entropy1 gaussian_img = nd.gaussian_filter(img, sigma=3 ) gaussian1 = gaussian_img.reshape(-1 ) df['Gaussian' ] = gaussian1 sobel_img = sobel(img) sobel1 = sobel_img.reshape(-1 ) df['Sobel' ] = sobel1 df
Original Pixel Values
Entropy
Gaussian
Sobel
0
123
1.584963
116
0.027311
1
122
1.500000
113
0.025565
2
116
2.000000
109
0.032590
3
114
2.000000
103
0.063612
4
99
2.000000
97
0.098479
...
...
...
...
...
1048571
111
2.000000
107
0.022570
1048572
118
2.000000
107
0.007466
1048573
106
2.000000
108
0.021702
1048574
109
1.500000
108
0.012783
1048575
112
1.584963
108
0.005280
1048576 rows × 4 columns
58 - What are Gabor filters
g(x,y;\lambda,\theta,\psi,\sigma,\gamma)=\exp \left(-\frac{x'^2+\gamma^2y'^2}{2\sigma^2}\right)\cdot\exp\left[i\left(2\pi\frac{x'}{\lambda}+\psi\right)\right]
e i x = cos x + i sin x e^{ix}=\cos x + i \sin x
e i x = cos x + i sin x
g(x,y;\lambda,\theta,\psi,\sigma,\gamma)=\exp \left(-\frac{x'^2+\gamma^2y'^2}{2\sigma^2}\right)\cdot\cos\left(2\pi\frac{x'}{\lambda}+\psi\right)
g(x,y;\lambda,\theta,\psi,\sigma,\gamma)=\exp \left(-\frac{x'^2+\gamma^2y'^2}{2\sigma^2}\right)\cdot\sin\left(2\pi\frac{x'}{\lambda}+\psi\right)
其中 x ′ = x cos θ + y sin θ x'=x\cos\theta + y\sin \theta x ′ = x cos θ + y sin θ , y ′ = − x sin θ + y cos θ y'=-x\sin\theta + y\cos \theta y ′ = − x sin θ + y cos θ
λ \lambda λ represents the wavelength of the sinusoidal factor.
θ \theta θ represents the orientation of the normal to the parallel stripes of a Gabor function.
θ \theta θ 表示Gabor函数平行条纹的法线方向。
ψ \psi ψ is the phase offset.
σ \sigma σ is the sigma/standard deviation of the Gaussian envelope.
γ \gamma γ is the spatial aspect ratio, and specifies the ellipticity of the support of the Gabor function.
γ \gamma γ 为空间纵横比,表示Gabor函数支持度的椭圆度。越接近 1,越像圆;越接近 0,越像椭圆。
1 2 3 4 5 6 7 8 9 10 11 12 13 import numpy as npimport cv2import matplotlib.pyplot as plt ksize = 5 sigma = 3 theta = 1 * np.pi / 4 lamda = 1 * np.pi / 4 gamma = 0.5 phi = 0 kernel = cv2.getGaborKernel((ksize, ksize), sigma, theta, lamda, gamma, phi, ktype=cv2.CV_32F) plt.imshow(kernel, cmap='gray' )
<matplotlib.image.AxesImage at 0x2021c8e8d90>
1 2 img = cv2.imread('images/synthetic.jpg' , 0 ) plt.imshow(img, cmap='gray' )
<matplotlib.image.AxesImage at 0x2021c966fd0>
1 2 fimg = cv2.filter2D(img, cv2.CV_8UC3, kernel) plt.imshow(fimg, cmap='gray' )
<matplotlib.image.AxesImage at 0x2021c9bec40>
通过修改 θ \theta θ 的值来使 Gabor 过滤器识别右下斜线。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 import numpy as npimport cv2import matplotlib.pyplot as plt ksize = 5 sigma = 3 theta = 3 * np.pi / 4 lamda = 1 * np.pi / 4 gamma = 0.5 phi = 0 kernel = cv2.getGaborKernel((ksize, ksize), sigma, theta, lamda, gamma, phi, ktype=cv2.CV_32F) fimg = cv2.filter2D(img, cv2.CV_8UC3, kernel) plt.imshow(fimg, cmap='gray' )
<matplotlib.image.AxesImage at 0x2021c697b20>