Python OpenCV using notes (v) (image smoothing and filtering)

Source: Internet
Author: User
Tags image filter

For the smoothing and filtering of graphs, but from the angle of filtering, the main purpose is to realize the elimination of image noise and enhance the effect of image.
Firstly, the two-dimensional convolution operation is introduced, and the image filtering can be regarded as the convolution operation of the filter template and the corresponding part of the original image. For a convolution operation, find a few related blogs:

Image processing: foundation (template, convolution)
Image Processing-template, convolution finishing

For 2D images can be low-pass or high-pass filtering operation, low-pass filter (LPF) is conducive to de-noising, blurred image, high-pass filter (HPF) is conducive to finding image boundaries.

(a) unified 2D filter cv2.filter2d

OPENCV provides a general 2D filter function for the cv2.filter2d (), the use of the filter function requires a kernel template, the image of the filter operation process is: and the template is placed in the image of a pixel A, the corresponding image of each pixel on the sum, the nucleus is different, the results are different, And the use of the core filter is also for the use of the kernel template, it is important to note that the filter function is a single-channel operation, that is, the color image of the filter, the color image needs to be extracted from each channel, the individual channels are filtered respectively.
Here is a similar situation with MATLAB, MATLAB also has a similar filter function IMFilter, the application of the filter function is not only in the filter, for many images of the overall processing, in fact, can be combined with the filter function to achieve faster results, Related to the room below this blog:

Application and extension of image filter function IMFilter function

ImportCv2ImportNumPy asNpImportMatplotlib.pyplot asPltimg = Cv2.imread (' flower.jpg ',0)#直接读为灰度图像IMG1 = Np.float32 (IMG)#转化数值类型Kernel = Np.ones ((5,5), Np.float32)/ -DST = cv2.filter2d (img1,-1, kernel)#cv2. filter2d (src,dst,kernel,auchor= ( -1,-1)) function:#输出图像与输入图像大小相同#中间的数为-1, output numeric format of the same plt.figure ()Plt.subplot (1,2,1), Plt.imshow (IMG1,' Gray ')#默认彩色, another color BGRPlt.subplot (1,2,2), Plt.imshow (DST,' Gray ')


Some of the filtering sections described below may be the function cv2.filter2d () materialized, re-specify a name just, paste a good blog reference:

Image Smoothing (normalized block filtering, Gaussian filtering, median filtering, bilateral filtering)

(b) Mean value filter

The 5*5 kernel template generated above is actually a mean-value filter. The OPENCV has a dedicated average filter template for use – normalized convolution template, all filter templates are the values of the center pixel that the resulting value of all pixel points in the convolution box overlay is multiplied by the template. OpenCV the mean value template can be used Cv2.blur and cv2.boxfilter, such as a 3*3 template can be represented as follows:
M = 1 9 ? ? ? 1 1 1 1 1 1 1 1 1 < Span style= "Display:inline-block; position:relative; Width:0.003em; height:0px; " > ? ? ?
The template size is m*n and can be set. If you do not want the previous 1/9, you can use the non-normalized template cv2.boxfilter. An example is as follows:

import cv2importas pltimg = cv2.imread(‘flower.jpg‘,0#直接读为灰度图像blur = cv2.blur(img,(3,5))#模板大小3*5plt.subplot(1,2,1),plt.imshow(img,‘gray‘)#默认彩色,另一种彩色bgrplt.subplot(1,2,2),plt.imshow(blur,‘gray‘)

(c) Gaussian blur template

Now change the value of the convolution template, not all 1, replaced by a set of values in accordance with the Gaussian distribution in the template, such as the middle of the value of the largest, toward both sides of the smaller, and construct a small Gaussian packet. The implemented function is CV2. Gaussianblur (). For the Gaussian template, we need to make the Gaussian kernel high and wide (odd), along the X and y direction of the standard deviation (if only give x,y=x, if all give 0, then the function will calculate itself). Gaussian core can effectively go out of the image of the Gaussian noise. Of course, you can also construct the Gaussian kernel, related functions: Cv2. Gaussiankernel ().

ImportCv2ImportNumPy asNpImportMatplotlib.pyplot asPltimg = Cv2.imread (' flower.jpg ',0)#直接读为灰度图像 forIinchRange -):#添加点噪声temp_x = Np.random.randint (0, img.shape[0]) temp_y = Np.random.randint (0, img.shape[1]) img[temp_x][temp_y] =255Blur = Cv2. Gaussianblur (IMG, (5,5),0) Plt.subplot (1,2,1), Plt.imshow (IMG,' Gray ')#默认彩色, another color BGRPlt.subplot (1,2,2), Plt.imshow (Blur,' Gray ')

(d) Median filter template

The median filter template is to use the median value of the pixel in the convolution box instead of the center value to achieve the purpose of de-noising. This template is typically used to remove salt and pepper noise. The previous filter replaces the value of the center pixel with a new value computed, and the median filter replaces it with the value of the center pixel (which can also make him himself), and the size of the convolution core is an odd number.

ImportCv2ImportNumPy asNpImportMatplotlib.pyplot asPltimg = Cv2.imread (' flower.jpg ',0)#直接读为灰度图像 forIinchRange -):#添加点噪声temp_x = Np.random.randint (0, img.shape[0]) temp_y = Np.random.randint (0, img.shape[1]) img[temp_x][temp_y] =255Blur = Cv2.medianblur (IMG,5) Plt.subplot (1,2,1), Plt.imshow (IMG,' Gray ')#默认彩色, another color BGRPlt.subplot (1,2,2), Plt.imshow (Blur,' Gray ')


You can see that the median filter is very good for the removal of these white-point noises.

(v) Bilateral filtering

The bilateral filter function is Cv2.bilateralfilter (). The filter can effectively remove noise when the boundary is clear. Its structure is more complicated, that is, considering the spatial relation of image and the gray relation of image. Both spatial Gaussian weights and gray similarity Gaussian weights are used in bilateral filtering to ensure that the boundary is not blurred out. There is a blog that introduces bilateral filtering:

The principle and realization of bilateral filter

Cv2.bilateralfilter (img,d, ' P1 ', ' P2 ') functions have four parameters required, D is the diameter of the field, and the latter two parameters are the standard deviation of the spatial Gaussian function standard deviation and the gray value similarity Gaussian function. An example is as follows:

ImportCv2ImportNumPy asNpImportMatplotlib.pyplot asPltimg = Cv2.imread (' flower.jpg ',0)#直接读为灰度图像 forIinchRange -):#添加点噪声temp_x = Np.random.randint (0, img.shape[0]) temp_y = Np.random.randint (0, img.shape[1]) img[temp_x][temp_y] =255#9---filter field diameter#后面两个数字: Standard deviation of spatial Gaussian function and standard deviation of gray value similarityBlur = Cv2.bilateralfilter (IMG,9, the, the) Plt.subplot (1,2,1), Plt.imshow (IMG,' Gray ')#默认彩色, another color BGRPlt.subplot (1,2,2), Plt.imshow (Blur,' Gray ')

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Python OpenCV using notes (v) (image smoothing and filtering)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.