Home >Web Front-end >JS Tutorial >Javascript image processing—smoothing processing implementation principle_javascript skills
Foreword
In the last article, we explained the virtual edge of the image. This article starts with smoothing (that is, blurring) processing.
Basic Principles
Here is a direct reference to the relevant content of OpenCV 2.4 C smoothing processing and OpenCV 2.4 C edge gradient calculation:
Smoothing, also called blurring, is a simple and frequently used image processing method.
A filter
is required for smoothing. The most commonly used filter is the linear filter, the output pixel value of the linear filtering process (for example: ) is the weighted average of the input pixel value (for example: ):
is called the kernel
, which is just a weighting coefficient.
This involves an operation called "convolution", so what is convolution?
Convolution is an operation performed between each image block and a certain operator (kernel).
Nuclear? !
nbsp;The kernel is a fixed-size numerical array. The array has an anchor point
, which is usually located in the center of the array.
But how does this work?
If you want to get the convolution value at a specific position of the image, you can calculate it with the following method:
Place the anchor point of the kernel on the pixel at that specific position, and at the same time, other values in the kernel coincide with each pixel in the neighborhood of the pixel; multiply each value in the kernel by the corresponding pixel value, and add the products Add; place the result on the pixel corresponding to the anchor point; repeat the above process for all pixels in the image.
The above process is represented by the formula as follows:
What about convolution on the edge of the image?
Before calculating the convolution, you need to create virtual pixels by copying the boundary of the source image, so that there are enough pixels at the edge to calculate the convolution. This is why the previous article required a virtual edge function.
Mean Smoothing
Mean smoothing is actually a convolution operation in which the kernel elements are all 1, and then divided by the size of the kernel. The mathematical expression is:
Let’s implement the mean smoothing function blur:
Gaussian smoothing
The most useful filter (although not the fastest). Gaussian filtering is to convolve each pixel of the input array with a Gaussian kernel
and treat the convolution sum as the output pixel value.
Referring to the one-dimensional Gaussian function, we can see that it is a function that is large in the middle and small on both sides.
So the weighting number of the Gaussian filter is large in the middle and small around it.
The two-dimensional Gaussian function is:
where
is the mean (the position corresponding to the peak value), represents the standard deviation (variable and variables Each has a mean and each has a standard deviation).Here we refer to the implementation of OpenCV, but there should be room for optimization because the separation filter has not been used yet.
First we make a getGaussianKernel to return a one-dimensional array of Gaussian filters.
中值平滑
中值滤波将图像的每个像素用邻域 (以当前像素为中心的正方形区域)像素的
中值代替 。依然使用blur里面用到的循环,只要得到核中的所有值,再通过sort排序便可以得到中值,然后锚点由该值替代。