forked from oreillymedia/Learning-OpenCV-3_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_14-01.cpp
49 lines (39 loc) · 1.06 KB
/
example_14-01.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
// Example 14-1. Finding contours based on a trackbar’s location; the contours are
// updated whenever the trackbar is moved
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
cv::Mat g_gray, g_binary;
int g_thresh = 100;
void on_trackbar( int, void* ) {
cv::threshold( g_gray, g_binary, g_thresh, 255, cv::THRESH_BINARY );
vector< vector< cv::Point> > contours;
cv::findContours(
g_binary,
contours,
cv::noArray(),
cv::RETR_LIST,
cv::CHAIN_APPROX_SIMPLE
);
g_binary = cv::Scalar::all(0);
cv::drawContours( g_binary, contours, -1, cv::Scalar::all(255));
cv::imshow( "Contours", g_binary );
}
int main( int argc, char** argv ) {
if( argc != 2 || ( g_gray = cv::imread(argv[1], 0)).empty() ) {
cout << "\nExample 14-1: Find threshold dependent contours\nUsage:\n" <<argv[0]
<<" ../fruits.jpg\n" << endl;
return -1;
}
cv::namedWindow( "Contours", 1 );
cv::createTrackbar(
"Threshold",
"Contours",
&g_thresh,
255,
on_trackbar
);
on_trackbar(0, 0);
cv::waitKey();
return 0;
}