Image Filters with Python and OpenCV

Nattadet C.
Nattadet C.
Published in
4 min readDec 2, 2018

This article is about Image filters and just a little bit coding with openCV and googleColabs. I am just a student that interested in image processing.

Filters and Point operations

Filters generally use many pixels for computing each new pixel value but point operations can use one pixel to perform an image processing. The filters can use for blurred or fuzzy the local intensity of the image to make it smooth. The idea is to replace every pixel by the average of its neighbor pixels.

Type of filters

we can classify the filters into 2 types.

  • Linear filter
  • Non-linear filter

Linear filter

The convolution of matrix pixels and kernel matrix to reduce the intensity of the image, that is blurring the image.

This process of the linear filter can be done in 2 ways. First, filter the original image into an intermediate image and then copy it to the original image. Second, copy the original image into intermediate image and then filtering it and replace to original image. Both ways make the same result but in general, we using the second way.

There are 3 types of linear filter

  • Box filter
  • Gauss filter
  • Laplace filter or Mexican hat filter

Box filter and Gauss filter are smooth filters but Laplace filter is a difference filter. The difference of these filter is the weighted number of kernel matrix as the picture

Box filter

You can use a box filter by following this code.

kernel = np.ones((5,5),np.float32)/25
blur = cv2.filter2D(img,-1,kernel)

First, you have to create the kernel matrix. In this code, I using a 5x5 kernel matrix then convolution with function filter2D in OpenCV. This is the result.

The top is the original image. The bottom is the filtered image. You can see that the edge of pixels has gone and the picture looks more smooth.

Gauss filter

To compute Gauss filter can using this equation or using function from OpenCV as GaussianBlur

Gblur = cv2.GaussianBlur(img,(5,5),21)

The last number ‘21’ is the sigma of this Gaussian function determines the amount of smoothing. The higher number of sigma, it is the higher of smoothness too. these are the result.

Laplace filter

To compute Laplace filter, OpenCV has function called “Laplacian” to calculate the pixel convolution to weighted the kernel matrix.

LaplacePic = cv2.Laplacian(img, 24, (5,5))

Number ‘24’ is called “DDepth”, represent depth of the destination image. This is the result of Laplace filter.

You can see that this picture has a white line appear. These lines from the edge between white pixel and black pixel. This filter can use in an application for showing the cutting line in the picture such as in engineering or in medical technique.

Non-Linear Filter

Using some non-linear function from the source pixel value. The idea is to replace the target pixel value with its neighbor pixels value from some ordering mechanism or function.

There are many types of Non-Linear Filter but in this article, I will show you just 3 of them

  • Minimum Filter
  • Maximum Filter
  • Median Filter

Minimum Filter

This algorithm is to select the lowest pixel value from the neighbors' pixels around the target then replace it.

Maximum Filter

This algorithm also similar to minimum filter but pick the highest one.

a) and b) are the sample image with salts and peppers c) and d) are the example of minimum filter e) and f) are the example of the maximum filter

Median Filter

First, this filter selects the neighbor pixels then sort it from the lowest to highest then pick the middle one to replace at the targeted pixel.

Median algorithms

In OpenCV has the function for the median filter you picture which is medianBlur function. This is an example of using it.

MedianPic = cv2.medianBlur(img, 5)

Number “5” represents the area of Kernel. And this is the result!

for more clarity of the advantage of this filter, Here is the example that used in salt and pepper pictures.

a) and b) are the original picture with salts and peppers' noise. c) and d) are the result of Linear 3x3 filter (box filter). e) and f) are the result of the Median filter that effectively eliminates salts and peppers noise

That’s all from my class. Thanks for reading my article. Enjoy your image processing and don’t afraid to build an interesting project. See you next time ❤ :)

--

--

Nattadet C.
Nattadet C.

use technology to connect people. #creativeTechnologist #bioMedicalEngineer