Python Computer Vision 3: blur, Smooth, de-noising

Source: Internet
Author: User

I am a beginner, if you find errors in the text, please leave a message to tell me, thank you

Image Blur and smoothing is the same level of meaning, the smooth process is a vague process.

The image denoising can be achieved by blurring and smoothing the image (image denoising and other methods)

So how can you blur a picture?

The image's fuzzy smoothing is the process of averaging the image matrix. Image smoothing is an integral process compared to image sharpening ( differential processing ).

The image smoothing process can be realized by using the original image and an integral operator for convolution.

The following is a description of two integration operators

All 1 operators

The simplest integral operator is the full 1 operator

The image can be blurred and smoothed by using all 1 operators, and there is a certain ability to de-noising.

The following is a Python instance

ImportNumPy as NP fromPILImportImageImportMatplotlib.pyplot as PltImportmatplotlib.cm as Cmsuanzi= Np.ones ((3,3))#Create a full 1 operator#Open the image and turn it into a grayscale imageImage = Image.open ("pika.jpg"). CONVERT ("L") Image_array=Np.array (image)#convolution of the original image and all 1 operatorsImage2 = Signal.convolve2d (image_array,suanzi,mode="same")#Convert result grayscale values to 0-255Image2 = (Image2/float (Image2.max ())) *255#Display ImagePlt.subplot (2,1,1) plt.imshow (Image_array,cmap=Cm.gray) Plt.axis ("off") Plt.subplot (2,1,2) plt.imshow (Image2,cmap=Cm.gray) Plt.axis ("if") plt.show ()

Operation results such as ( in order to see the effect, the image has been artificially amplified )

As the original image, the blurred image is processed

Comparing two graphs, we can see that all 1 operators have a certain fuzzy smoothing effect.

Gauss operator

The Gaussian blur that we often hear is the use of Gaussian operators for fuzzy processing .

The Gaussian distribution of the standard deviation σ is the following formula

We can generate Gaussian operators through the FromFunction () method of the NumPy module.

ImportNumPy as NP#multiplied by 100 is to make the number in the operator easy to observe#Sigma Specifies the standard deviation of the Gaussian operatordefFunc (x,y,sigma=1):    return100* (1/(2*np.pi*sigma)) *np.exp (-((x-2) **2+ (y-2) **2)/(2.0*sigma**2))#generation of 5*5 Gaussian operators with standard deviation of 2A = Np.fromfunction (func, (5,5), sigma=2)Print(a)#Results[[2.92749158 4.25947511 4.82661763 4.25947511 2.92749158] [ 4.25947511 6.19749972 7.02268722) 6.19749972 4.25947511] [ 4.82661763 7.02268722 7.95774715) 7.02268722 4.82661763] [ 4.25947511 6.19749972 7.02268722) 6.19749972 4.25947511] [ 2.92749158 4.25947511 4.82661763) 4.25947511 2.92749158]]

Rounding the above 5*5 gauss operator for each element, you can get the matrix below

See some places directly with the above matrix on the image Gaussian blur, in fact, is the standard deviation of 2 Gaussian approximation operator.

Using Gaussian operator to blur the image, the program is as follows

ImportNumPy as NP fromPILImportImageImportMatplotlib.pyplot as PltImportmatplotlib.cm as cmImportscipy.signal as Signal#functions for generating Gaussian operatorsdefFunc (x,y,sigma=1):    return100* (1/(2*np.pi*sigma)) *np.exp (-((x-2) **2+ (y-2) **2)/(2.0*sigma**2))#generation of 5*5 Gaussian operators with standard deviation of 2Suanzi = Np.fromfunction (func, (5,5), sigma=2)#Open the image and turn it into a grayscale imageImage = Image.open ("pika.jpg"). CONVERT ("L") Image_array=Np.array (image)#convolution of images and Gaussian operatorsImage2 = Signal.convolve2d (image_array,suanzi,mode="same")#results converted to 0-255Image2 = (Image2/float (Image2.max ())) *255#Display ImagePlt.subplot (2,1,1) plt.imshow (Image_array,cmap=Cm.gray) Plt.axis ("off") Plt.subplot (2,1,2) plt.imshow (Image2,cmap=Cm.gray) Plt.axis ("off") plt.show ()

Operation results such as ( in order to see the effect, the image has been artificially amplified )

As the original image, the image is processed by Gaussian Blur

Comparing the Gaussian operator with the full 1 operator, it can be seen that the ambiguity of the Gaussian operator seems to be better.

Furthermore, we can adjust the blur effect by changing the standard deviation and dimension of the Gaussian operator.

In general, the higher the standard deviation of the Gaussian operator, the larger the dimension, the more blurred the image .

Reference list

1. "Python Computer Vision Programming"

2. Mother, thank those who love to share knowledge of friends

Python Computer Vision 3: blur, Smooth, de-noising

Related Article

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.