-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathselector.cpp
More file actions
249 lines (191 loc) · 6.45 KB
/
selector.cpp
File metadata and controls
249 lines (191 loc) · 6.45 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include <iostream>
#include <math.h>
#include <string>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
cv::Mat frame, globalHist;
bool t = false;
int key = 0;
int h_max=180;
int h_min=0;
int s_min=0;
int s_max=256;
void printParams() {
std::cout << "S_MIN: " << s_min << '\n';
std::cout << "S_MAX: " << s_max << '\n';
std::cout << "H_MIN: " << h_min << '\n';
std::cout << "H_MAX: " << h_max << '\n';
}
cv::Mat convertToFullValuedHSV(const cv::Mat &image){
cv::Mat result;
cv::cvtColor(image, result, CV_BGR2HSV);
cv::Mat hsv[3];
cv::split(result, hsv);
// Remove Value
hsv[2].setTo(255);
cv::merge(hsv, 3, result);
return result;
}
void onClick(cv::Point start) {
// std::cout << "Clicked at: " << start << '\n';
}
void onDrag(cv::Point start, cv::Point current) {
auto clone = frame.clone();
// Draws a rectangle
cv::rectangle(clone, start, current, CV_RGB(255, 0, 0));
// std::cout << "Moved at: " << current << '\n';
cv::imshow("result", clone);
}
bool inHistogram(const cv::Mat & hist, const int histSize[], const float* ranges[], const cv::Vec3b & pixel) {
if ( hist.at<float>( ( pixel[0] * histSize[0] ) / ranges[0][1], ( pixel[1] * histSize[1] ) / ranges[1][1] ) != 0 )
return true;
return false;
}
cv::Mat getFilteredImage() {
auto clone = convertToFullValuedHSV(frame);
cv::Scalar min_color = cvScalar(h_min, s_min, 0);
cv::Scalar max_color = cvScalar(h_max, s_max, 256);
cv::inRange(clone, min_color, max_color, clone);
cv::cvtColor( clone, clone, CV_GRAY2RGB);
return clone;
}
bool matEq(const cv::Mat & lhs, const cv::Mat & rhs) {
cv::Mat cmp;
cv::compare(lhs, rhs, cmp, cv::CMP_NE);
cv::cvtColor(cmp, cmp, CV_BGRA2GRAY, 1);
return !cv::countNonZero(cmp);
}
void tuneParameter(int & param, int start, int end, const cv::Mat & pic) {
cv::Mat filt;
int change = -1;
int truechange;
int lowerbound = start, upperbound = end;
param = lowerbound;
while ( true ) {
if ( change == 0 || change == 1 ) break;
filt = getFilteredImage();
cv::Mat result;
cv::bitwise_and(filt, pic, result);
if ( matEq(pic, result) ) {
lowerbound = param;
truechange = (upperbound - param)/2;
param += (upperbound - param)/2;
}
else {
upperbound = param;
truechange = -(param - lowerbound)/2;
param -= (param - lowerbound)/2;
}
change = ( param - lowerbound );
std::cout << "Change = " << truechange << '\n';
}
}
void tryExpand(const cv::Mat & pic) {
std::cout << "### HMAX ###\n";
tuneParameter(h_max, 180, h_min, pic);
std::cout << "### HMIN ###\n";
tuneParameter(h_min, 0, 180, pic);
std::cout << "### SMAX ###\n";
tuneParameter(s_max, 256, s_min, pic);
std::cout << "### SMIN ###\n";
tuneParameter(s_min, 0, 256, pic);
printParams();
auto filt = getFilteredImage();
imshow("sel", pic);
imshow("result", filt);
}
void onRelease(cv::Point start, cv::Point current) {
if ( start.x > current.x ) std::swap(start.x, current.x);
if ( start.y > current.y ) std::swap(start.y, current.y);
// Perform actual work
auto clone = convertToFullValuedHSV(frame);
// Replace some value in selected square
auto roi = clone(cv::Range(start.y, current.y), cv::Range(start.x, current.x));
// Use histograms
int hbins = 30, sbins = 32;
int histSize[] = {hbins, sbins};
float hranges[] = { 0, 180 };
float sranges[] = { 0, 256 };
const float* ranges[] = { hranges, sranges };
int channels[] = {0, 1};
cv::Mat hist;
calcHist( &roi, 1, channels, cv::Mat(), // do not use mask
hist, 2, histSize, ranges,
true, // the histogram is uniform
false );
if ( t ) globalHist += hist;
else { globalHist = hist; t = true; }
for ( int i = 0; i < clone.rows; ++i )
for ( int j = 0; j < clone.cols; ++j )
if ( !inHistogram(globalHist, histSize, ranges, clone.at<cv::Vec3b>(i,j)) )
clone.at<cv::Vec3b>(i,j) = {0,0,0};
cv::cvtColor(clone, clone, CV_HSV2BGR);
cv::rectangle(clone, start, current, CV_RGB(255, 0, 0));
imshow("hist", globalHist);
tryExpand(clone);
//imshow("result", clone);
}
void mouseHandler(int event, int x, int y, int flags, void* param) {
static cv::Point start(0,0);
static bool drag = false;
cv::Point mouse(x,y);
/* user press left button */
if (event == CV_EVENT_LBUTTONDOWN && !drag) {
onClick(mouse);
start = mouse;
drag = true;
}
/* user drag the mouse */
else if (event == CV_EVENT_MOUSEMOVE && drag) {
onDrag(start, mouse);
}
/* user release left button */
else if (event == CV_EVENT_LBUTTONUP && drag) {
onRelease(start, mouse);
drag = false;
}
/* user click right button: reset all */
else if (event == CV_EVENT_RBUTTONUP) {
imshow("result", frame);
t = false;
h_max=180;
h_min=0;
s_min=0;
s_max=256;
drag = false;
}
}
void handleTrackbar(int, void*);
int main(int argc, char *argv[]) {
if ( argc < 2 ) {
std::cout << "Usage: " << argv[0] << " image_file_name\n";
return 0;
}
frame = cv::imread(argv[1]);
cv::Size kernel(8,8);
cv::blur(frame, frame, kernel);
/* create a window for the video */
std::string window = "result";
cv::namedWindow( window, CV_WINDOW_AUTOSIZE );
cv::namedWindow( "hist", CV_WINDOW_AUTOSIZE );
cv::namedWindow( "sel", CV_WINDOW_AUTOSIZE );
cv::createTrackbar( "hmin:", window, &h_min, 180, handleTrackbar );
cv::createTrackbar( "hmax:", window, &h_max, 180, handleTrackbar );
cv::createTrackbar( "smin:", window, &s_min, 256, handleTrackbar );
cv::createTrackbar( "smax:", window, &s_max, 256, handleTrackbar );
cv::setMouseCallback("result", mouseHandler, NULL);
cv::imshow(window, frame);
while( key != 'q' )
key = cv::waitKey( 1 );
cv::destroyWindow("result");
return 0;
}
void handleTrackbar(int, void*){
auto clone = convertToFullValuedHSV(frame);
cv::Mat imgResult;
cv::Scalar min_color = cvScalar(h_min, s_min, 0);
cv::Scalar max_color = cvScalar(h_max, s_max, 256);
cv::inRange(clone, min_color, max_color, imgResult);
cv::cvtColor( imgResult, imgResult, CV_GRAY2RGB);
imshow("result", imgResult);
}