2011-06-22 07:50:02 +08:00
|
|
|
/**
|
|
|
|
* @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"
|
2018-05-19 01:51:34 +08:00
|
|
|
#include <iostream>
|
2011-06-22 07:50:02 +08:00
|
|
|
|
|
|
|
using namespace cv;
|
2018-05-19 01:51:34 +08:00
|
|
|
using std::cout;
|
2011-06-22 07:50:02 +08:00
|
|
|
|
|
|
|
/// Global variables
|
|
|
|
|
|
|
|
int threshold_value = 0;
|
2014-01-07 06:38:41 +08:00
|
|
|
int threshold_type = 3;
|
2011-06-22 07:50:02 +08:00
|
|
|
int const max_value = 255;
|
|
|
|
int const max_type = 4;
|
2018-05-19 01:51:34 +08:00
|
|
|
int const max_binary_value = 255;
|
2011-06-22 07:50:02 +08:00
|
|
|
|
|
|
|
Mat src, src_gray, dst;
|
2012-11-07 22:21:20 +08:00
|
|
|
const char* window_name = "Threshold Demo";
|
2011-06-22 07:50:02 +08:00
|
|
|
|
2012-11-07 22:21:20 +08:00
|
|
|
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";
|
2011-06-22 07:50:02 +08:00
|
|
|
|
2018-05-19 01:51:34 +08:00
|
|
|
//![Threshold_Demo]
|
|
|
|
/**
|
|
|
|
* @function Threshold_Demo
|
|
|
|
*/
|
|
|
|
static 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 );
|
|
|
|
}
|
|
|
|
//![Threshold_Demo]
|
2011-06-22 07:50:02 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @function main
|
|
|
|
*/
|
2017-07-26 13:39:53 +08:00
|
|
|
int main( int argc, char** argv )
|
2011-06-22 07:50:02 +08:00
|
|
|
{
|
2018-05-19 01:51:34 +08:00
|
|
|
//! [load]
|
2019-07-28 17:09:17 +08:00
|
|
|
String imageName("stuff.jpg"); // by default
|
2018-05-19 01:51:34 +08:00
|
|
|
if (argc > 1)
|
|
|
|
{
|
|
|
|
imageName = argv[1];
|
|
|
|
}
|
2019-07-28 17:09:17 +08:00
|
|
|
src = imread( samples::findFile( imageName ), IMREAD_COLOR ); // Load an image
|
2011-06-22 07:50:02 +08:00
|
|
|
|
2018-05-19 01:51:34 +08:00
|
|
|
if (src.empty())
|
2011-06-22 07:50:02 +08:00
|
|
|
{
|
2019-07-28 17:09:17 +08:00
|
|
|
cout << "Cannot read the image: " << imageName << std::endl;
|
2018-05-19 01:51:34 +08:00
|
|
|
return -1;
|
2011-06-22 07:50:02 +08:00
|
|
|
}
|
|
|
|
|
2018-05-19 01:51:34 +08:00
|
|
|
cvtColor( src, src_gray, COLOR_BGR2GRAY ); // Convert the image to Gray
|
|
|
|
//! [load]
|
2011-06-22 07:50:02 +08:00
|
|
|
|
2018-05-19 01:51:34 +08:00
|
|
|
//! [window]
|
|
|
|
namedWindow( window_name, WINDOW_AUTOSIZE ); // Create a window to display results
|
|
|
|
//! [window]
|
|
|
|
|
|
|
|
//! [trackbar]
|
|
|
|
createTrackbar( trackbar_type,
|
|
|
|
window_name, &threshold_type,
|
2019-07-28 17:09:17 +08:00
|
|
|
max_type, Threshold_Demo ); // Create a Trackbar to choose type of Threshold
|
2011-06-22 07:50:02 +08:00
|
|
|
|
2018-05-19 01:51:34 +08:00
|
|
|
createTrackbar( trackbar_value,
|
|
|
|
window_name, &threshold_value,
|
2019-07-28 17:09:17 +08:00
|
|
|
max_value, Threshold_Demo ); // Create a Trackbar to choose Threshold value
|
2018-05-19 01:51:34 +08:00
|
|
|
//! [trackbar]
|
2011-06-22 07:50:02 +08:00
|
|
|
|
2018-05-19 01:51:34 +08:00
|
|
|
Threshold_Demo( 0, 0 ); // Call the function to initialize
|
|
|
|
|
2019-07-28 17:09:17 +08:00
|
|
|
/// Wait until the user finishes the program
|
2018-05-19 01:51:34 +08:00
|
|
|
waitKey();
|
|
|
|
return 0;
|
2011-06-22 07:50:02 +08:00
|
|
|
}
|