-
Notifications
You must be signed in to change notification settings - Fork 903
/
example_10-03.cpp
58 lines (51 loc) · 1.35 KB
/
example_10-03.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Example 10-3. Threshold versus adaptive threshold
#include <opencv2/opencv.hpp>
#include <cstdlib>
#include <iostream>
using namespace std;
int main( int argc, char** argv )
{
if(argc != 7) { cout << "\nExample 10-3. Threshold versus adaptive threshold\n"
"Usage:\n" <<argv[0] <<" fixed_threshold invert(0=off|1=on) "
"adaptive_type(0=mean|1=gaussian) block_size offset image\n"
"Example:\n" <<argv[0] <<" 100 1 0 15 10 ../faces.png\n"; return -1; }
// Process command line arguments
//
double fixed_threshold = (double)atof(argv[1]);
int threshold_type = atoi(argv[2]) ? cv::THRESH_BINARY : cv::THRESH_BINARY_INV;
int adaptive_method = atoi(argv[3]) ? cv::ADAPTIVE_THRESH_MEAN_C
: cv::ADAPTIVE_THRESH_GAUSSIAN_C;
int block_size = atoi(argv[4]);
double offset = (double)atof(argv[5]);
cv::Mat Igray = cv::imread(argv[6], cv::IMREAD_GRAYSCALE);
// Read in gray image.
//
if( Igray.empty() ){ cout << "Can not load " << argv[6] << endl; return -1; }
// Declare the output images.
//
cv::Mat It, Iat;
// Thresholds.
//
cv::threshold(
Igray,
It,
fixed_threshold,
255,
threshold_type);
cv::adaptiveThreshold(
Igray,
Iat,
255,
adaptive_method,
threshold_type,
block_size,
offset
);
// Show the results.
//
cv::imshow("Raw",Igray);
cv::imshow("Threshold",It);
cv::imshow("Adaptive Threshold",Iat);
cv::waitKey(0);
return 0;
}