-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcv_thresh.cpp
More file actions
196 lines (162 loc) · 4.65 KB
/
Copy pathcv_thresh.cpp
File metadata and controls
196 lines (162 loc) · 4.65 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <iostream>
#include <vector>
#include <opencv2/opencv.hpp>
using namespace cv;
// Function Declarations
void printUsage(char* arg0);
void threshold(int, void*);
void initLiveWindow();
void run(Mat img);
// Global Variables
Mat hue;
Mat val;
int minHue = 0;
int maxHue = 0;
int maxVal = 0;
int minVal = 0;
int minSat = 0;
int maxSat = 0;
std::string windowId = "CV LIVE THRESHOLDING DEMO";
int main(int argc, char* argv[])
{
Mat inputImg;
std::cout << "argc: " << argc << std::endl;
// Live Window Mode
if(argc == 3)
{
if(strcmp(argv[1], "-d") != 0)
{
printUsage(argv[0]);
return 1;
}
// This accepts numerical input, or argument like /dev/video0 (tested)
std::cout << "Initializing Camera @ Index " << argv[2] << std::endl;
VideoCapture cam = VideoCapture(atoi(argv[2]));
initLiveWindow();
while(true)
{
Mat inputImg;
cam.read(inputImg);
run(inputImg);
// Only breaks on Esc
if(waitKey(30) == 27)
break;
}
}
// File Mode
else if(argc == 2)
{
// Flag needs argument, can't be alone
if(strcmp(argv[1], "-d") == 0)
{
printUsage(argv[0]);
return 1;
}
// TODO: Exits immediately, fix plz
std::cout << "File Mode\n";
inputImg = imread(argv[1]);
run(inputImg);
return 0;
}
else
{
printUsage(argv[0]);
return 1;
}
return 0;
}
// Initialize the Window and the Trackbars
void initLiveWindow()
{
namedWindow(windowId, WINDOW_NORMAL);
createTrackbar("hueMin", windowId, &minHue, 255, threshold);
createTrackbar("hueMax", windowId, &maxHue, 255, threshold);
}
// Do all the thresholding everytime a slider value is updated
void threshold(int, void*)
{
using namespace cv;
Mat threshLow;
Mat threshHigh;
Mat hueResult;
Mat valResult;
Mat satResult;
//hue
threshold(hue, threshLow, minHue, 255, THRESH_BINARY);
threshold(hue, threshHigh, maxHue, 255, THRESH_BINARY_INV);
hueResult = threshLow & threshHigh;
//value
threshold(val, threshLow, minVal, 255, THRESH_BINARY);
threshold(val, threshHigh, maxVal, 255, THRESH_BINARY_INV);
valResult = threshLow & threshHigh;
//saturation
threshold(val, threshLow, minSat, 255, THRESH_BINARY);
threshold(val, threshHigh, maxSat, 255, THRESH_BINARY_INV);
satResult = threshLow & threshHigh;
Mat threshed = hueResult & valResult & satResult;
imshow(windowId, lastThreshResult);
}
// Processes the image and performs thresholding
void run(Mat img)
{
// Blur the image to smooth it out (especially with JPG's)
GaussianBlur(img, img, Size(3, 3), 1, 1);
// Convert to HSV
Mat cvted;
cvtColor(img, cvted, CV_BGR2HSV);
// Isolate the Hue Channel, and store in global variable
std::vector<Mat> separated(3);
split(cvted, separated);
hue = separated.at(0).clone();
val = separated.at(2).clone();
namedWindow(windowId, WINDOW_NORMAL);
createTrackbar("hueMin", windowId, &minHue, 255, threshold);
createTrackbar("hueMax", windowId, &maxHue, 255, threshold);
createTrackbar("valMin", windowId, &minVal, 255, threshold);
createTrackbar("valMax", windowId, &maxVal, 255, threshold);
createTrackbar("satMin", windowId, &minSat, 255, threshold);
createTrackbar("satMax", windowId, &maxSat, 255, threshold);
imshow(windowId, img);
// Do the image processing once initially (parameters have no significance)
threshold(1, NULL);
}
void printUsage(char* arg0)
{
bool error = false;
Mat img;
if(argc == 3) {
std::cout << "Initializing Camera @ Index " << argv[2];
VideoCapture cam = VideoCapture(atoi(argv[2]));
while(true) {
Mat img;
cam.read(img);
//imshow("Live", img);
run(img);
if (waitKey(30) >= 0){break;}
}
}
else if (argc == 2){
std::string arg1(argv[1]);
if (arg1 != "-d") {
std::cout << "Normal Init";
img = imread(argv[1]);
while(true) {
run(img);
if (waitKey(30) >= 0){break;}
}
//run(img);
}
else {
error = true;
}
}
else{
error = true;
}
if (error){
std::cout << std::endl;
std::cout << "Image Loading:\n";
std::cout << " Usage: " << arg0 << " <imgpath>\n\n";
std::cout << "Live Video Feed Loading:\n";
std::cout << " Usage: " << arg0 << " -d <videoDeviceID>\n\n";
}