OpenCV: Segmentation using Thresholding

In this article, a basic technique for object segmentation called Thresholding.
But before moving into anymore detail, below is a brief overview of OpenCV.

OpenCV (Open Source Computer Vision) is a cross platform, open-source library of programming functions, aimed at performing real-time computer vision tasks in a wide variety of fields, such as:

  • Facial recognition
  • Iris Recognition Systems
  • Gesture recognition
  • Human–computer interaction (HCI)
  • Mobile robotics
  • Object identification
  • Segmentation and recognition
  • Stereopsis stereo vision: depth perception from 2 cameras
  • Augmented reality

It also includes a robust statistical machine learning library, that contains a number of different classifiers used to support the above areas.

To use OpenCV, simply import or include the required libraries and start making use of the myriad of available functions.

Thresholding is a very popular segmentation technique, used for separating an object from its background. In the article below, I have described various techniques used to threshold grayscale images(8-bit).

The process of thresholding involves, comparing each pixel value of the image (pixel intensity) to a specified threshold. This divides all the pixels of the input image into 2 groups:

  1. Pixels having intensity value lower than threshold.
  2. Pixels having intensity value greater than threshold.

These 2 groups are now given different values, depending on various segmentation types.
OpenCV supports 5 different thresholding schemes on Grayscale(8-bit) images using the function :

Double threshold(InputArray src, OutputArray dst, double thresh, double maxval, int type)

Parameters:

  • InputArray src: Input Image (Mat, 8-bit or 32-bit)
  • OutputArray dst: Output Image ( same size as input)
  • double thresh: Set threshold value
  • double maxval: maxVal, used in type 1 and 2
  • int type* :Specifies the type of threshold to be use. (0-4)
  • *Below a list of thresholding types is given.

    Input Image

    orig

    The input RGB image is first converted to a grayscale image before thresholding is done.

    bw

    Thresholding types

    • Binary Threshold(int type=0)

      0_130
      Of the two groups obtained earlier, the group having members with pixel intensity, greater than the set threshold, are assignment “Max_Value”, or in case of a grayscale, a value of 255 (white).
      The members of the remaining group have their pixel intensities set to 0 (black).

      eq1

      If the pixel intensity value at (x, y) in source image, is greater than threshold, the value in final image is set to “maxVal”.

    • Inverted Binary Threshold(int type=1)

      1_130

      Inv. Binary threshold is the same as Binary threshold. The only essential difference being, in Inv.Binary thresholding, the group having pixel intensities greater than set threshold, gets assigned ‘0’, whereas the remaining pixels having intensities, less than the threshold, are set to “maxVal”.

      eq2

      If the pixel intensity value at (x, y) in source image, is greater than threshold, the value in final image is set to “0”, else it is set to “maxVal”.

    • Truncate Thresholding(int type=2)

      2_150

      The group having pixel intensities greater than the set threshold, is truncated to the set threshold or in other words, the pixel values are set to be same as the set threshold.
      All other values remain the same.

      eq3

      If the pixel intensity value at (x, y) in source image, is greater than threshold, the value in final image is set to “threshold”, else it is unchanged.

    • Threshold to Zero(int type=3)

      3

      A very simple thresholding technique, wherein we set the pixel intensity to ‘0’, for all the pixels of the group having pixel intensity value, less than the threshold.

      eq4

      If the pixel intensity value at (x, y) in source image, is greater than threshold, the value at (x, y) in the final image doesn’t change. All the remaining pixels are set to ‘0’.

    • Threshold to Zero, Inverted(int type=4)

      4

      Similar to the previous technique, here we set the pixel intensity to ‘0’, for all the pixels of the group having pixel intensity value, greater than the threshold.

      eq5

      If the pixel intensity value at (x, y) in source image, is greater than threshold, the value at (x, y) in the final image is set to ‘0’. All the remaining pixel value are unchanged.


    To compile OpenCV programs, you need to have OpenCV library installed on your system. I will be posting a simple tutorial for the same, in the coming days.
    If you have already installed OpenCV, run the below code with the input image of your choice.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // CPP program to demonstrate segmentation
    // thresholding.
    #include <opencv2/highgui/highgui.hpp>
    #include <opencv2/imgproc/imgproc.hpp>
    #include <stdio.h>
    #include <stdlib.h>
      
    using namespace cv;
      
    int main(int argc, char** argv)
    {
        if (argc != 2) 
        {
            cout << " Usage: <Sourceprogram>"
                    " <ImageToLoad>" << endl;
            return -1;
        }
      
        int threshold_value = 0;
      
        // Valid Values: 0, 1, 2, 3, 4
        int threshold_type = 2; 
      
        // maxVal, useful for threshold_type 1 and 2
        int maxVal = 255; 
      
        // Source image
        Mat src = imread(argv[1], 1);
      
        cvNamedWindow("Original", CV_WINDOW_NORMAL);
        imshow("Original", src);
      
        Mat src_gray, dst;
      
        // Convert the image to GrayScale
        cvtColor(src, src_gray, CV_BGR2GRAY);
      
        // Create a window to display results
        cvNamedWindow("Result", CV_WINDOW_NORMAL);
      
        createTrackbar("Threshold", "Result"
                      &threshold_value, 255);
        while (1) 
        {
            threshold(src_gray, dst, threshold_value, 
                             maxVal, threshold_type);
            imshow("Result", dst);
            waitKey(1);
        }
    }

    chevron_right

    
    



    My Personal Notes arrow_drop_up


    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




    Article Tags :
    Practice Tags :


    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.