2011-06-22 01:50:02 +02:00
|
|
|
/**
|
|
|
|
* @file Threshold.cpp
|
|
|
|
* @brief Sample code that shows how to use the diverse threshold options offered by OpenCV
|
|
|
|
* @author OpenCV team
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "opencv2/imgproc/imgproc.hpp"
|
2014-07-04 16:48:15 +02:00
|
|
|
#include "opencv2/imgcodecs.hpp"
|
2011-06-22 01:50:02 +02:00
|
|
|
#include "opencv2/highgui/highgui.hpp"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
using namespace cv;
|
|
|
|
|
|
|
|
/// Global variables
|
|
|
|
|
|
|
|
int threshold_value = 0;
|
2014-01-06 23:38:41 +01:00
|
|
|
int threshold_type = 3;
|
2011-06-22 01:50:02 +02:00
|
|
|
int const max_value = 255;
|
|
|
|
int const max_type = 4;
|
|
|
|
int const max_BINARY_value = 255;
|
|
|
|
|
|
|
|
Mat src, src_gray, dst;
|
2012-11-07 15:21:20 +01:00
|
|
|
const char* window_name = "Threshold Demo";
|
2011-06-22 01:50:02 +02:00
|
|
|
|
2012-11-07 15:21:20 +01: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 01:50:02 +02:00
|
|
|
|
|
|
|
/// Function headers
|
|
|
|
void Threshold_Demo( int, void* );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function main
|
|
|
|
*/
|
2012-11-07 15:21:20 +01:00
|
|
|
int main( int, char** argv )
|
2011-06-22 01:50:02 +02:00
|
|
|
{
|
|
|
|
/// Load an image
|
|
|
|
src = imread( argv[1], 1 );
|
|
|
|
|
|
|
|
/// Convert the image to Gray
|
2013-10-30 13:34:27 +01:00
|
|
|
cvtColor( src, src_gray, COLOR_RGB2GRAY );
|
2011-06-22 01:50:02 +02:00
|
|
|
|
|
|
|
/// Create a window to display results
|
2013-10-30 13:34:27 +01:00
|
|
|
namedWindow( window_name, WINDOW_AUTOSIZE );
|
2011-06-22 01:50:02 +02:00
|
|
|
|
|
|
|
/// Create Trackbar to choose type of Threshold
|
2012-10-17 01:18:30 +02:00
|
|
|
createTrackbar( trackbar_type,
|
2013-10-30 13:34:27 +01:00
|
|
|
window_name, &threshold_type,
|
|
|
|
max_type, Threshold_Demo );
|
2011-06-22 01:50:02 +02:00
|
|
|
|
|
|
|
createTrackbar( trackbar_value,
|
2013-10-30 13:34:27 +01:00
|
|
|
window_name, &threshold_value,
|
|
|
|
max_value, Threshold_Demo );
|
2011-06-22 01:50:02 +02:00
|
|
|
|
|
|
|
/// Call the function to initialize
|
|
|
|
Threshold_Demo( 0, 0 );
|
|
|
|
|
|
|
|
/// Wait until user finishes program
|
2012-11-07 15:21:20 +01:00
|
|
|
for(;;)
|
2011-06-22 01:50:02 +02:00
|
|
|
{
|
|
|
|
int c;
|
|
|
|
c = waitKey( 20 );
|
|
|
|
if( (char)c == 27 )
|
2012-10-17 01:18:30 +02:00
|
|
|
{ break; }
|
2011-06-22 01:50:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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 );
|
|
|
|
}
|