opencv/samples/cpp/tutorial_code/ImgProc/Threshold.cpp

89 lines
2.0 KiB
C++
Raw Normal View History

/**
* @file Threshold.cpp
* @brief Sample code that shows how to use the diverse threshold options offered by OpenCV
* @author OpenCV team
*/
2016-02-15 21:37:29 +08:00
#include "opencv2/imgproc.hpp"
2014-07-04 22:48:15 +08:00
#include "opencv2/imgcodecs.hpp"
2016-02-15 21:37:29 +08:00
#include "opencv2/highgui.hpp"
using namespace cv;
/// Global variables
int threshold_value = 0;
2014-01-07 06:38:41 +08:00
int threshold_type = 3;
int const max_value = 255;
int const max_type = 4;
int const max_BINARY_value = 255;
Mat src, src_gray, dst;
const char* window_name = "Threshold Demo";
const char* trackbar_type = "Type: \n 0: Binary \n 1: Binary Inverted \n 2: Truncate \n 3: To Zero \n 4: To Zero Inverted";
const char* trackbar_value = "Value";
/// Function headers
void Threshold_Demo( int, void* );
/**
* @function main
*/
int main( int, char** argv )
{
2016-07-18 21:32:05 +08:00
//! [load]
src = imread( argv[1], IMREAD_COLOR ); // Load an image
2016-02-15 21:37:29 +08:00
if( src.empty() )
{ return -1; }
2016-07-18 21:32:05 +08:00
cvtColor( src, src_gray, COLOR_BGR2GRAY ); // Convert the image to Gray
//! [load]
2016-07-18 21:32:05 +08:00
//! [window]
namedWindow( window_name, WINDOW_AUTOSIZE ); // Create a window to display results
//! [window]
2016-07-18 21:32:05 +08:00
//! [trackbar]
2012-10-17 07:18:30 +08:00
createTrackbar( trackbar_type,
window_name, &threshold_type,
2016-07-18 21:32:05 +08:00
max_type, Threshold_Demo ); // Create Trackbar to choose type of Threshold
createTrackbar( trackbar_value,
window_name, &threshold_value,
2016-07-18 21:32:05 +08:00
max_value, Threshold_Demo ); // Create Trackbar to choose Threshold value
//! [trackbar]
2016-07-18 21:32:05 +08:00
Threshold_Demo( 0, 0 ); // Call the function to initialize
/// Wait until user finishes program
for(;;)
{
int c;
c = waitKey( 20 );
if( (char)c == 27 )
2012-10-17 07:18:30 +08:00
{ break; }
}
}
2016-07-18 21:32:05 +08:00
//![Threshold_Demo]
/**
* @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 );
}
2016-07-18 21:32:05 +08:00
//![Threshold_Demo]