In part-1 of image processing using OpenCV, we learned basic terminology used in computer vision, read-write operations, histogram creation, and edge detection techniques. In part-2 of image pre-processing using OpenCV, we are going to learn a few advanced techniques of image processing that helps in image augmentation as well so that we can increase our dataset size for any deep learning implementation.

Table of content:

  1. Image Thresholding
  2. Image Smoothing
  3. Rescaling
  4. Denoising

1. Image Thresholding

Image thresholding is used to extract the desired information or pixels from the image.

There are 3 types of thresholding that can be applied using OpenCV

  • Simple Thresholding
    • Binary threshold
    • Inverted binary threshold
    • Truncated threshold
    • threshold set to zero
    • Inverted threshold set to zero
  • Adaptive Thresholding
    • Mean
    • Gaussian
  • Otsu Thresholding

Simple thresholding

The constant threshold is applied to the whole image

Binary threshold

If pixel value > Threshold: Pixel value == 255

If pixel value < Threshold: Pixel value == 0

Inverted binary threshold

It’s just opposite to binary threshold

If pixel value > Threshold: Pixel value == 0

If pixel value < Threshold: Pixel value ==255

Truncated threshold

If pixel value > Threshold: pixel value == Threshold

If pixel value < threshold: pixel value == No change

Threshold set to zero

If pixel value > Threshold: pixel value == No change

If pixel value < threshold: pixel value == 0

Inverted threshold set to zero

If pixel value > Threshold: pixel value == 0

If pixel value < threshold: pixel value == No change

Adaptive threshold

Previously, in simple thresholding, the threshold value was constant for all pixels. That is not very effective if the image has variations in light, contrast.

So in that case, adaptive thresholding is used. There are two ways to implement adaptive thresholding.

  1. based on mean: In this case we take mean of pixels under kernel area and subtract it from a constant value (manually defined). Through this approach, threshold will be varied according to neighborhood pixels.
  2. Based on Gaussian weighted sum: In this case we take weighted sum of pixels under kernel area and then subtract it from a constant value.

which method will be suitable for the application, that purely depends on what we want to extract from the image, image details.

Mean adaptive thresholding

Gaussian Adaptive thresholding

If the batch size is chosen an even no, then an error will be thrown as shown. So batch size is always chosen odd.

Ostu threshold

In otsu thresholding, the value of the threshold is decided automatically.

How does it work?

It assumes that every image contains two kinds of pixel intensities, one belongs to the background class and one to the foreground class. So it involves an iterating process through all pixels, segregating pixels by keeping them into two classes. The goal is to find a threshold such that the resulting background and foreground distributions are maximally

2. Image Blurring/smoothing

Image smoothing can be done in 4 ways:

  • Averaging
  • Median blur
  • Gaussian blur
  • Bilateral filtering


It replaces the pixel value with the average of all pixels under the kernel area.

Median Blur

It’s a non-linear filter. It replaces the central element pixel values with the median value of pixels present in the kernel area. How kernel area pixels are taken into account, depends on kernel size e.g. (3×3),(5×5)

Gaussian Blur

It replaces pixel values with a weighted average of neighborhood pixels. It follows Gaussian distribution, that’s why more weight is given to the pixels which are nearer to the central pixel of the kernel. It’s a linear filter. It does not preserve the edges because while taking a weighted average, it does not check intensity or edges.

Bilateral Filtering

Bilateral filtering considers two Gaussian filters for blurring. One is used for a weighted average, same as in Gaussian blurring while another Gaussian filter is a function of intensity difference that makes sure that it will consider only pixels in the kernel which are almost similar in intensity. So that edges can be preserved.

3. Rescaling

Image resizing is known as Image rescaling. It helps to reduce or increase the number of pixels in an image. An increase in the number of pixels is called zooming of the image. Reduction in size is helpful in many ways in image processing as by reducing the number of pixels, less computation is required for neural networks or any other machine learning model.

Reduction in the number of pixels may affect the image quality i.e. fine details or features.

To make the image size larger:


Through image blurring, we filtered out noise on the basis of the nearest pixels. So blurring is a local process to reduce noise.

If we take a small patch in the image, find out similar windows in the same image, take their average and then replace the pixel of the window with the average obtained, the process will be non-local that denoising.

There are two functions available in OpenCV to denoise the image.

  1. cv2.fastNlMeansDenoising() – works with a single grayscale image
  1. cv2.fastNlMeansDenoisingColored() – works with a color image.

Here is the link for GitHub code:

If you want to learn image pre-processing basic terms and operations, please check part-1 here: