This article mainly introduces “python How to realize simple image filtering ” Knowledge about , Xiaobian shows you the operation process through practical cases , The operation method is simple and fast , Practical , Hope this article “python How to realize simple image filtering ” The article can help you solve problems .
Filter the image , It can have two effects : One is smooth filtering , Used to suppress noise ; The other is differential operator , It can be used for edge detection and feature extraction .
skimage Pass through the library filters The module performs filtering operation .
sobel Operators can be used to detect edges
The function format is :skimage.filters.sobel(image, mask=None)
from skimage import data,filtersimport matplotlib.pyplot as pltimg = data.camera()edges = filters.sobel(img)plt.imshow(edges,plt.cm.gray)
roberts Operator and sobel Like operators , Used to detect edges
The call format is the same :
edges = filters.roberts(img)
Same function sobel, Invocation format :
edges = filters.scharr(img)
Same function sobel, Invocation format :
edges = filters.prewitt(img)
canny Operators are also used to extract edge features , But it's not on filters modular , It's on the feature modular
Function format :skimage.feature.canny(image,sigma=1.0)
You can modify sigma To adjust the effect
from skimage import data,filters,featureimport matplotlib.pyplot as pltimg = data.camera()edges1 = feature.canny(img) #sigma=1edges2 = feature.canny(img,sigma=3) #sigma=3plt.figure('canny',figsize=(8,8))plt.subplot(121)plt.imshow(edges1,plt.cm.gray) plt.subplot(122)plt.imshow(edges2,plt.cm.gray)plt.show()
It can be seen from the results ,sigma The smaller it is , The smaller the edge lines .
gabor Filtering can be used for edge detection and texture feature extraction .
Function call format :skimage.filters.gabor_filter(image, frequency)
By modifying the frequency Value to adjust the filtering effect , Returns a pair of edge results , One is to use the filtering result of the real filtering kernel , One is the filtering result of the imaginary filtering kernel .
from skimage import data,filtersimport matplotlib.pyplot as pltimg = data.camera()filt_real, filt_imag = filters.gabor_filter(img,frequency=0.6) plt.figure('gabor',figsize=(8,8))plt.subplot(121)plt.title('filt_real')plt.imshow(filt_real,plt.cm.gray) plt.subplot(122)plt.title('filt-imag')plt.imshow(filt_imag,plt.cm.gray)plt.show()
The above is frequency=0.6 Results of .
The above is frequency=0.1 Results of
Multidimensional filter , It is a kind of smoothing filter , Can eliminate Gaussian noise .
The call function is :skimage.filters.gaussian_filter(image, sigma)
By adjusting the sigma To adjust the filtering effect
from skimage import data,filtersimport matplotlib.pyplot as pltimg = data.astronaut()edges1 = filters.gaussian_filter(img,sigma=0.4) #sigma=0.4edges2 = filters.gaussian_filter(img,sigma=5) #sigma=5plt.figure('gaussian',figsize=(8,8))plt.subplot(121)plt.imshow(edges1,plt.cm.gray) plt.subplot(122)plt.imshow(edges2,plt.cm.gray)plt.show()
so sigma The bigger it is , The more blurred the filtered image
median filtering , A smoothing filter , Noise can be eliminated .
Need to use skimage.morphology Module to set the shape of the filter .
from skimage import data,filtersimport matplotlib.pyplot as pltfrom skimage.morphology import diskimg = data.camera()edges1 = filters.median(img,disk(5))edges2= filters.median(img,disk(9))plt.figure('median',figsize=(8,8))plt.subplot(121)plt.imshow(edges1,plt.cm.gray) plt.subplot(122)plt.imshow(edges2,plt.cm.gray)plt.show()
It can be seen from the results , The larger the filter , The more blurred the image .
The above examples are all edge detection , Sometimes we just need to detect horizontal edges , Or vertical edge , You can use the following method .
Horizontal edge detection :sobel_h, prewitt_h, scharr_h
Vertical edge detection : sobel_v, prewitt_v, scharr_v
from skimage import data,filtersimport matplotlib.pyplot as pltimg = data.camera()edges1 = filters.sobel_h(img) edges2 = filters.sobel_v(img) plt.figure('sobel_v_h',figsize=(8,8))plt.subplot(121)plt.imshow(edges1,plt.cm.gray) plt.subplot(122)plt.imshow(edges2,plt.cm.gray)plt.show()
The upper left figure shows the detected horizontal edge , The right figure shows the detected vertical edge .
You can use Roberts The cross core of , To achieve the purpose of detecting the cross edge . These cross edges are actually a component of the gradient in one direction .
One of the nuclei :
0 1
-1 0
Corresponding function :
roberts_neg_diag(image)
example :
from skimage import data,filtersimport matplotlib.pyplot as pltimg =data.camera()dst =filters.roberts_neg_diag(img) plt.figure('filters',figsize=(8,8))plt.subplot(121)plt.title('origin image')plt.imshow(img,plt.cm.gray)plt.subplot(122)plt.title('filted image')plt.imshow(dst,plt.cm.gray)
Another core :
1 0
0 -1
The corresponding function is :
roberts_pos_diag(image)
from skimage import data,filtersimport matplotlib.pyplot as pltimg =data.camera()dst =filters.roberts_pos_diag(img) plt.figure('filters',figsize=(8,8))plt.subplot(121)plt.title('origin image')plt.imshow(img,plt.cm.gray)plt.subplot(122)plt.title('filted image')plt.imshow(dst,plt.cm.gray)
About “python How to realize simple image filtering ” That's all for , Thanks for reading . If you want to know more about the industry , You can pay attention to the Yisu cloud industry information channel , Xiaobian will update different knowledge points for you every day .