Digital Image Processing: Blur Image from Time Domain
If there is a lot of noise in your image, or the image looks sharp so that it may look not like nature, what should we do?
You can use some filters to blur the image.
Co-Author: Y. S. Huang, a master’s student studying AIVC, likes open-source.
If you are interested, go to check my Github!
There are three different filters.
Before we start to introduce these filters, we have to know the effect of using the filter for the data. In the picture down below you can imagine that is a sectional view of the image and is also a sequence of data, the idea of the blur is using neighboring values to make the distribution more smooth.
And in the implementation stage, we have to know how filter2D()
works in OpenCV.
Mean Filter
The concept of the mean filter is very simple, it replaces each pixel value with a mean value of its neighbors, including itself.
Because it averages every single pixel in the image by a custom kernel, it retouches your image and removes noise in it.
dst = cv.filter2D(src, ddepth, kernel[, dst[, anchor[, delta[, borderType]]]])
src
: represents the input image, which image we want to convert.
ddepth
: desire depth, usually set to-1
, here’s the correspondence table:
kernel & anchor
: kernel is a numpy.ndarray martix, we can create it by using numpy, anchor decide where the new data is in the dst.
delta
: plus delta after finishing the convolution, turn it into a linear operation.
borderType
: Specifies image boundaries while the kernel is applied on image borders.
We can change the kernel size to get a more severe smoothing image.
Mean filter can make your image more smooth and remove noise in the image, but it will lose the texture information.
2. Gaussian filter
Rather than the mean filter, the Gaussian filter is a better way to reserve the detail of texture.
Using filter2D to make the image present the Gaussian distribution,
dst = cv2.GaussianBlur(src, ksize, sigmaX[, dst[, sigmaY[, borderType=BORDER_DEFAULT]]]
src
: represents the input image, which image we want to convert.
ksize
: Gaussian Kernel Size. [height width]. height and width should be odd and can have different values.
sigmaX & Y
: Kernel standard deviation along X-axis (horizontal direction) and Y-axis (vertical direction). If sigmaY=0, then sigmaX value is taken for sigmaY.
borderType
: Specifies image boundaries while kernel is applied on image borders.
3. Medium filter
The medium filter is an extremely powerful filter to remove the noise like pepper & salt.
We can see the second image full fill with dot noise(pepper & salt), and the median filter can easily remove this type of noise.
At first, we have to decide the kernel size, changing the kernel size can get a more severe smoothing image, and increase the effect of removing noise.
Second, according to the kernel, we sort the elements in the matrix to get the middle value.
Last, change the center value into the middle value.
cv2.medianBlur(src, ksize[, dst])
src
: represents the input image, which image we want to convert.
dst
: destination array of the same size and type assrc
.
ksize
: aperture linear size; it must be odd and greater than 1.
We introduce three different filters in this story, each of them can be used in different types of situations. They are very useful tools!
Figure out the feature of your image and choose the correct filter!