1 /**
2 * @file Threshold.cpp
3 * @brief Sample code that shows how to use the diverse threshold options offered by OpenCV
4 * @author OpenCV team
5 */
6
7 #include "opencv2/imgproc/imgproc.hpp"
8 #include "opencv2/imgcodecs.hpp"
9 #include "opencv2/highgui/highgui.hpp"
10 #include <stdlib.h>
11 #include <stdio.h>
12
13 using namespace cv;
14
15 /// Global variables
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
29 /// Function headers
30 void Threshold_Demo( int, void* );
31
32 /**
33 * @function main
34 */
main(int,char ** argv)35 int main( int, char** argv )
36 {
37 /// Load an image
38 src = imread( argv[1], 1 );
39
40 /// Convert the image to Gray
41 cvtColor( src, src_gray, COLOR_RGB2GRAY );
42
43 /// Create a window to display results
44 namedWindow( window_name, WINDOW_AUTOSIZE );
45
46 /// Create Trackbar to choose type of Threshold
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
55 /// Call the function to initialize
56 Threshold_Demo( 0, 0 );
57
58 /// Wait until user finishes program
59 for(;;)
60 {
61 int c;
62 c = waitKey( 20 );
63 if( (char)c == 27 )
64 { break; }
65 }
66
67 }
68
69
70 /**
71 * @function Threshold_Demo
72 */
Threshold_Demo(int,void *)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 }
86