OpenCV  3.0.0-rc1
Open Source Computer Vision
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Basic Thresholding Operations

Goal

In this tutorial you will learn how to:

Cool Theory

Note
The explanation below belongs to the book Learning OpenCV by Bradski and Kaehler. What is

Thresholding?

Types of Thresholding

Threshold Binary

Threshold Binary, Inverted

Truncate

Threshold to Zero

Threshold to Zero, Inverted

Code

The tutorial code's is shown lines below. You can also download it from here

1 
8 #include "opencv2/imgcodecs.hpp"
10 #include <stdlib.h>
11 #include <stdio.h>
12 
13 using namespace cv;
14 
16 
17 int threshold_value = 0;
18 int threshold_type = 3;
19 int const max_value = 255;
20 int const max_type = 4;
21 int const max_BINARY_value = 255;
22 
23 Mat src, src_gray, dst;
24 const char* window_name = "Threshold Demo";
25 
26 const char* trackbar_type = "Type: \n 0: Binary \n 1: Binary Inverted \n 2: Truncate \n 3: To Zero \n 4: To Zero Inverted";
27 const char* trackbar_value = "Value";
28 
30 void Threshold_Demo( int, void* );
31 
35 int main( int, char** argv )
36 {
38  src = imread( argv[1], 1 );
39 
41  cvtColor( src, src_gray, COLOR_RGB2GRAY );
42 
44  namedWindow( window_name, WINDOW_AUTOSIZE );
45 
47  createTrackbar( trackbar_type,
48  window_name, &threshold_type,
49  max_type, Threshold_Demo );
50 
51  createTrackbar( trackbar_value,
52  window_name, &threshold_value,
53  max_value, Threshold_Demo );
54 
56  Threshold_Demo( 0, 0 );
57 
59  for(;;)
60  {
61  int c;
62  c = waitKey( 20 );
63  if( (char)c == 27 )
64  { break; }
65  }
66 
67 }
68 
69 
73 void Threshold_Demo( int, void* )
74 {
75  /* 0: Binary
76  1: Binary Inverted
77  2: Threshold Truncated
78  3: Threshold to Zero
79  4: Threshold to Zero Inverted
80  */
81 
82  threshold( src_gray, dst, threshold_value, max_BINARY_value,threshold_type );
83 
84  imshow( window_name, dst );
85 }
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0)
Converts an image from one color space to another.
Mat imread(const String &filename, int flags=IMREAD_COLOR)
Loads an image from a file.
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
int createTrackbar(const String &trackbarname, const String &winname, int *value, int count, TrackbarCallback onChange=0, void *userdata=0)
Creates a trackbar and attaches it to the specified window.
Definition: imgproc.hpp:512
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
Creates a window.
double threshold(InputArray src, OutputArray dst, double thresh, double maxval, int type)
Applies a fixed-level threshold to each array element.
Definition: highgui.hpp:138
int main(int argc, const char *argv[])
Definition: facerec_demo.cpp:67
int waitKey(int delay=0)
Waits for a pressed key.

Explanation

  1. Let's check the general structure of the program:
    • Load an image. If it is RGB we convert it to Grayscale. For this, remember that we can use the function cv::cvtColor :
      src = imread( argv[1], 1 );
      cvtColor( src, src_gray, COLOR_RGB2GRAY );
    • Create a window to display the result
      namedWindow( window_name, WINDOW_AUTOSIZE );
    • Create \(2\) trackbars for the user to enter user input:
      • Type of thresholding: Binary, To Zero, etc...
      • Threshold value
        createTrackbar( trackbar_type,
        window_name, &threshold_type,
        max_type, Threshold_Demo );
        createTrackbar( trackbar_value,
        window_name, &threshold_value,
        max_value, Threshold_Demo );
    • Wait until the user enters the threshold value, the type of thresholding (or until the program exits)
    • Whenever the user changes the value of any of the Trackbars, the function Threshold_Demo is called:
      /*
      * @function Threshold_Demo
      */
      void Threshold_Demo( int, void* )
      {
      /* 0: Binary
      1: Binary Inverted
      2: Threshold Truncated
      3: Threshold to Zero
      4: Threshold to Zero Inverted
      */
      threshold( src_gray, dst, threshold_value, max_BINARY_value,threshold_type );
      imshow( window_name, dst );
      }
      As you can see, the function cv::threshold is invoked. We give \(5\) parameters:
      • src_gray: Our input image
      • dst: Destination (output) image
      • threshold_value: The \(thresh\) value with respect to which the thresholding operation is made
      • max_BINARY_value: The value used with the Binary thresholding operations (to set the chosen pixels)
      • threshold_type: One of the \(5\) thresholding operations. They are listed in the comment section of the function above.

Results

  1. After compiling this program, run it giving a path to an image as argument. For instance, for an input image as:

    Threshold_Tutorial_Original_Image.jpg
  2. First, we try to threshold our image with a binary threhold inverted. We expect that the pixels brighter than the \(thresh\) will turn dark, which is what actually happens, as we can see in the snapshot below (notice from the original image, that the doggie's tongue and eyes are particularly bright in comparison with the image, this is reflected in the output image).

    Threshold_Tutorial_Result_Binary_Inverted.jpg
  3. Now we try with the threshold to zero. With this, we expect that the darkest pixels (below the threshold) will become completely black, whereas the pixels with value greater than the threshold will keep its original value. This is verified by the following snapshot of the output image:

    Threshold_Tutorial_Result_Zero.jpg