PYTHON+OPENCV image Processing (vii)--filtering and fuzzy operation

Source: Internet
Author: User
Tags function prototype

Filtering is the basic task in signal and image processing. The purpose is to selectively extract some of the information that is considered important in the image, depending on the application environment. Filtering removes noise from images, extracts visual features of interest, allows image resampling, and so on. The frequency domain analysis divides the image into different parts from low frequency to high frequency. The low frequency corresponds to the region where the intensity of the image changes little, and the high frequency is the area where the intensity varies greatly. In the framework of frequency analysis, a filter is an operation to enhance a band or frequency in an image and block (or lower) other frequency bands. The low-pass filter is to eliminate the high-frequency portion of the image, but retains the low frequency. High-pass filters eliminate low-frequency parts. Reference Blog: 9155893

  The individual thinks that the fuzzy operation is to filter out some special noises in the image.

Specific fuzzy and filtering relationships such as: reference to the Great God: https://www.zhihu.com/question/54918332/answer/142137732

  

First, mean value blur, median blur, user-defined Blur

The code is as follows:

#Mean blur, median blur, and custom fuzzy blur are a representation of convolutionImportCv2 as CVImportNumPy as NPdefBlur_demo (image):#mean fuzzy de-random noise has a good effect on drynessDST = Cv.blur (image, (1, 15))#(1, 15) is the vertical direction blur, (15, 1) also horizontal direction blurCv.namedwindow ('Blur_demo', CV. Window_normal) Cv.imshow ("Blur_demo", DST)defMedian_blur_demo (image):#median ambiguity has a good effect on salt and pepper noise.DST = Cv.medianblur (image, 5) Cv.namedwindow ('Median_blur_demo', CV. Window_normal) Cv.imshow ("Median_blur_demo", DST)defCustom_blur_demo (image):#user-definable Blurkernel = Np.ones ([5, 5], Np.float32)/25#除以25是防止数值溢出DST= cv.filter2d (image, 1, Kernel) Cv.namedwindow ('Custom_blur_demo', CV. Window_normal) Cv.imshow ("Custom_blur_demo", DST) src= Cv.imread ('E:\imageload\lenanoise.jpg') Cv.namedwindow ('Input_image', CV. Window_normal) Cv.imshow ('Input_image', SRC) blur_demo (src) median_blur_demo (src) custom_blur_demo (src) cv.waitkey (0) cv.destroyallwindows ()

Operation Result:

Attention:

1. Mean filter is a typical linear filtering algorithm, which refers to the image of the target pixel to a template, the template includes its surrounding neighboring pixels (with the target pixel centered around 8 pixels, forming a filter template, that is, remove the target pixel itself), and then use the average of all the pixels in the template to replace the original pixel value.

Low pass filter (mean Blur) function prototype: Blur (SRC, ksize[, dst[, anchor[, Bordertype]]), DST

The SRC parameter represents the input image to be processed.

The Ksize parameter represents the fuzzy kernel size. For example (1,15) indicates that the generated fuzzy kernel is a 1*15 matrix.

The DST parameter represents an image of the same size and type as the SRC output.

Anchor parameter, Bordertype parameter can be ignored

2. Median filtering is a nonlinear smoothing technique that sets the grayscale value of each pixel point to the median of all pixel grayscale values in a neighborhood window of that point. Specific principles See blog: 72627543

Median filter (median blur) function prototype: Medianblur (SRC, ksize[, DST]), DST

The SRC parameter represents the input image to be processed.

The Ksize parameter represents the filter window size, which must be odd and greater than 1. For example, here is 5, the median filter is calculated using the 5x5 range, that is, the central value of the pixel and its 5x5 neighborhood consists of a set of values, processing calculations, the current pixel is replaced by its median value .

The DST parameter represents an image of the same size and type as the SRC output.

3. User-definable Blur

Function used: filter2d ()

Function prototypes: filter2d (SRC, ddepth, kernel[, dst[, anchor[, delta[, Bordertype]]), DST

The SRC parameter represents the input image to be processed.

The ddepth parameter represents the target image depth, and the target image is consistent with the original image depth when the input value is-1

Kernel: convolutional cores (or related cores), a single-channel floating-point matrix . Modify the kernel matrix to achieve different blur

Second, Gaussian Blur

The code is as follows:

#Gaussian blur Contour is still in, preserving the main features of the image Gaussian blur ratio mean fuzzy denoising effect is goodImportCv2 as CVImportNumPy as NPdefClamp (PV):ifPV > 255:        return255ifPV <0:return0Else:        returnPVdefGaussian_noise (image):#Heightened noiseH, W, C =Image.shape forRowinchRange (h): forColinchRange (W): s= Np.random.normal (0, 20, 3) b= Image[row, col, 0]#Blueg = Image[row, col, 1]#Greenr = Image[row, col, 2]#RedImage[row, col, 0] = Clamp (b +S[0]) Image[row, col,1] = clamp (g + s[1]) Image[row, col,2] = Clamp (r + s[2]) Cv.namedwindow ("Noise Image", CV. Window_normal) Cv.imshow ("Noise Image", image) DST= CV. Gaussianblur (Image, (15, 15), 0)#Gaussian BlurCv.namedwindow ("Gaussian", CV. Window_normal) Cv.imshow ("Gaussian", DST) src= Cv.imread ('E:\imageload\lena.jpg') Cv.namedwindow ("Input_image", CV. Window_normal) Cv.imshow ('Input_image', SRC) gaussian_noise (src) DST= CV. Gaussianblur (SRC, (15,15), 0)#Gaussian BlurCv.namedwindow ("Gaussian Blur", CV. Window_normal) Cv.imshow ("Gaussian Blur", DST) cv.waitkey (0) cv.destroyallwindows ()

Operation Result:

Attention:

1. Gaussian blur is essentially a mean fuzzy, but Gaussian blur is weighted average, the closer the point weight, the farther away the point weight is lower. In layman's words, Gaussian filtering is the process of weighted averaging of an entire image, and the value of each pixel is obtained by weighted averaging of its own and other pixel values within the neighborhood.

2. The two-dimensional principle of Gaussian distribution is as follows:

Compensation: Standard deviation of Gaussian distributionσ. The standard deviation represents the degree of dispersion of the data if & #x03C3; " >σ smaller, the resulting template has a large central coefficient, and the surrounding coefficients are small, This smoothing effect on the image is not obvious; conversely, & #x03C3; " >σ Gaussian blur specific principles see blog: 51023768

3. Gaussian Blur Gaussianblur function prototype: Gaussianblur (SRC, ksize, sigmax[, dst[, sigmay[, Bordertype]]), DST

The SRC parameter represents the input image to be processed.

The Ksize parameter represents the Gaussian filter template size. Ksize.width and ksize.height can be different, but they must both be positive and odd . Alternatively, they can be zero, i.e. (0, 0) and then calculated from Σ.

The Sigmax parameter represents the Gaussian core standard deviation in the x direction.

The Sigmay parameter represents the Gaussian core standard deviation in the y direction. If Sigmay is zero, it is set to equal to Sigmax, and if two Sigma is zero, it is calculated from Ksize.width and ksize.height, respectively.

Complement: If Ksize is not (0, 0), then according to Ksize calculation, the latter Sigmax no meaning. If the ksize is (0, 0), the ksize is calculated according to the following Sigmax

The random module in the 4.numpy package is used to generate the stochastic number, and the normal function in the random module represents the generated Gaussian random number.

Normal function Default prototype: Normal (loc=0.0, scale=1.0, Size=none).

The LOC parameter represents the center point of the Gaussian distribution.

The scale parameter represents the standard deviation σ of the Gaussian distribution.

The size parameter indicates the number of random numbers that are generated. The size value can be (m,n,k), which indicates that a sample is drawn m*n*k.

Third, Edge retention filter EPF

There are two methods for edge-preserving filtering: Gaussian-bilateral filtering and mean-shift filtering.

The code is as follows:

#Edge-Preserving filter (EPF) Gaussian-bilateral, mean-value migrationImportCv2 as CVImportNumPy as NPdefBi_demo (image):#Bilateral filteringDST = cv.bilateralfilter (image, 0, 100, 15) Cv.namedwindow ("Bi_demo", CV. Window_normal) Cv.imshow ("Bi_demo", DST)defShift_demo (image):#mean MigrationDST = cv.pyrmeanshiftfiltering (image, 10, 50) Cv.namedwindow ("Shift_demo", CV. Window_normal) Cv.imshow ("Shift_demo", DST) src= Cv.imread ('E:/imageload/example.png') Cv.namedwindow ('Input_image', CV. Window_normal) Cv.imshow ('Input_image', SRC) bi_demo (src) shift_demo (src) cv.waitkey (0) cv.destroyallwindows ()

Operation Result:

Attention:

1. Bilateral filtering (bilateral filter) is a nonlinear filtering method, which combines the spatial proximity of the image with the similarity of the pixel value, taking into account the spatial information and the similarity of gray level to achieve the purpose of edge-preserving denoising. The double-sided filter, as the name implies, is a Gaussian variance sigma-d, which is based on the spatial distribution of the Gaussian filter function, so near the edge, the farther away pixels will not affect the pixel value on the edge, thus guaranteeing the preservation of the pixel values near the edge. However, due to the preservation of excessive high-frequency information, for the high-frequency noise in the color image, the bilateral filter can not be filtered cleanly, only for low-frequency information to better filter

2. Bilateral filter function prototypes: Bilateralfilter (SRC, D, Sigmacolor, sigmaspace[, dst[, Bordertype]), DST

The SRC parameter represents the input image to be processed.

The d parameter represents the diameter of each pixel neighborhood used during filtering. If input d is not 0, then Sigmaspace is calculated by D, and if Sigmacolor is not entered, the Sigmacolor is calculated by Sigmaspace.

The Sigmacolor parameter represents the standard variance of the color space, which is generally as large as possible. Larger parameter values mean that the darker colors in the pixel neighborhood are blended together, resulting in a larger area of semi-equal color.

The Sigmaspace parameter represents the standard variance (pixel units) of the coordinate space, which is generally as small as possible. Larger parameter values mean that as long as their color is close enough, the farther the pixels will affect each other. When d > 0 o'clock, it specifies the neighborhood size without regard to sigmaspace. Otherwise, d is proportional to the sigmaspace.

The principle of bilateral filtering:

78837988,77482794, https://www.cnblogs.com/qiqibaby/p/5296681.html Anyway, I don't know how to read O (╥﹏╥) o

3. Mean shift pyrmeanshiftfiltering function prototype: pyrmeanshiftfiltering (SRC, SP, sr[, dst[, maxlevel[, Termcrit]]), DST

The SRC parameter represents the input image, 8-bit, three-channel image.

The SP parameter indicates the size of the drift physical space radius.

The SR parameter indicates the size of the drift color space radius.

The Maxlevel parameter represents the maximum number of layers for the pyramid.

The Termcrit parameter represents the drift iteration termination condition.

Mean drift principle:

52705087

51804574

30258833

PYTHON+OPENCV image Processing (vii)--filtering and fuzzy operation

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.