-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.cpp
377 lines (337 loc) · 9.78 KB
/
Utils.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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include "Utils.hpp"
void loadROIs(std::string name, std::vector<cv::Rect>& ROIs)
{
std::ifstream input;
input.open(name);
int x, y, w, h;
while (input >> x >> y >> w >> h)
{
ROIs.push_back(cv::Rect(x,y,w,h));
}
input.close();
}
void selectROIs(cv::String name, cv::Mat& img, std::vector<cv::Rect>& ROIs)
{
bool loop = true;
// Loop until the user presses enter (or space) without selecting any ROIs
// (or until they press c as indicated in opencv's docs)
while(loop)
{
cv::Rect ROI = cv::selectROI(name, img, false);
// stop looping if no ROI is selected
if(ROI.x == 0 and ROI.y == 0 and ROI.width == 0 and ROI.height == 0)
{
loop = false;
}
// otherwise draw the ROI just selected in red over the image, add the ROI to the output vector
// and loop again to select the next ROI
else
{
cv::rectangle(img,ROI,cv::Scalar(0,0,255));
ROIs.push_back(ROI);
}
}
}
void saveROIs(cv::String baseName, std::vector<cv::Rect>& ROIs)
{
std::ofstream output;
output.open(baseName+".txt");
for(const auto& ROI: ROIs)
{
output<<ROI.x<<" "<<ROI.y<<" "<<ROI.width<<" "<<ROI.height<<"\n";
}
output.close();
}
bool filenamesMatch(const cv::String& f1, const cv::String& f2)
{
return f1.size() == f2.size() and
f1.substr(0,f1.size()-4) == f2.substr(0,f1.size()-4);
}
bool maskFilenamesMatch(const cv::String& mask, const cv::String& base, bool boat)
{
if (boat){
return base.substr(base.size()-(4+5)) == "_maskb.png" or
(
mask.size() == (base.size()+ 6) and // 6 = "_maskb".size()
mask.substr(0,mask.size()-(4+6)) == base.substr(0,base.size()-4)
);
} else {
return base.substr(base.size()-(4+5)) == "_mask.png" or
(
mask.size() == (base.size()+ 5) and // 5 = "_mask".size()
mask.substr(0,mask.size()-(4+5)) == base.substr(0,base.size()-4)
);
}
}
void drawROIs(cv::Mat& img, std::vector<cv::Rect>& ROIs)
{
for(const auto& ROI: ROIs)
{
cv::rectangle(img,ROI,cv::Scalar(0,0,255));
}
}
void bgr_to_gray_HE(cv::Mat& image, cv::Mat& out)
{
// Created by Anna Zuccante on 20/06/2021.
cv::Mat gray_img;
cv::cvtColor(image, gray_img, cv::COLOR_BGR2GRAY);
cv::equalizeHist(gray_img, out);
};
void sharpen(cv::Mat& input, cv::Mat& output, int laplacianWeight)
{
// by Ivano
cv::Mat imgLaplacian;
//cv::Laplacian(input, imgLaplacian, CV_32F);
cv::GaussianBlur(input, output, cv::Size(5,5),0);
cv::Laplacian(output, imgLaplacian, CV_32F);
output.convertTo(output, CV_32F);
output = output - laplacianWeight * imgLaplacian;
// convert back to 8bits gray scale
output.convertTo(output, CV_8U);
}
double median(cv::Mat& img)
{
// https://gist.github.com/heisters/9cd68181397fbd35031b
double m = (img.rows*img.cols) / 2;
int bin = 0;
double med = -1.0;
int histSize = 256;
float range[] = { 0, 256 };
const float* histRange = { range };
bool uniform = true;
bool accumulate = false;
cv::Mat hist;
cv::calcHist( &img, 1, 0, cv::Mat(), hist, 1, &histSize, &histRange, uniform, accumulate );
for ( int i = 0; i < histSize && med < 0.0; ++i )
{
bin += cvRound( hist.at< float >( i ) );
if ( bin > m && med < 0.0 )
med = i;
}
return med;
}
typedef struct {
cv::Mat* mask;
cv::Mat* img;
cv::String* name;
unsigned int brushSize = 20;
unsigned int count = 0;
cv::Point prevPt;
bool boat;
} SeaCallbackData;
void updateSeaImage(SeaCallbackData& data)
{
showMask(*data.name, *data.img, *data.mask, data.boat);
}
static void selectSeaCallback( int event, int x, int y, int flags, void* userdata)
{
SeaCallbackData* data = (SeaCallbackData*)userdata;
const double brush_size = data->brushSize;
if( x < 0 || x >= data->img->cols || y < 0 || y >= data->img->rows )
return;
if( event == cv::EVENT_MOUSEMOVE && (flags & cv::EVENT_FLAG_LBUTTON) )
{
cv::Point pt(x, y);
if (data->prevPt.x == -1 && data->prevPt.y == -1) {
data->prevPt = pt;
}
cv::line(*data->mask, data->prevPt, pt, cv::Scalar(255,255,255), data->brushSize,8,0);
data->prevPt=pt;
//cv::circle(*data->mask, pt, data->brushSize, cv::Scalar(255,255,255),-1,8,0);
data->count += 1;
if(data->count>10)
{
updateSeaImage(*data);
data->count = 0;
}
}
if( event == cv::EVENT_MOUSEMOVE && (flags & cv::EVENT_FLAG_RBUTTON) )
{
cv::Point pt(x, y);
if (data->prevPt.x == -1 && data->prevPt.y == -1) {
data->prevPt = pt;
}
cv::line(*data->mask, data->prevPt, pt, cv::Scalar(0,0,0), data->brushSize,8,0);
data->prevPt=pt;
data->count += 1;
if(data->count>10)
{
updateSeaImage(*data);
data->count = 0;
}
}
if( event == cv::EVENT_LBUTTONUP )
{
updateSeaImage(*data);
data->prevPt = cv::Point(-1,-1);
} else if( event == cv::EVENT_RBUTTONUP )
{
updateSeaImage(*data);
data->prevPt = cv::Point(-1,-1);
}else if( event == cv::EVENT_LBUTTONDOWN ){
data->prevPt = cv::Point(x,y);
} else if( event == cv::EVENT_RBUTTONDOWN ){
data->prevPt = cv::Point(x,y);
}else if (!(flags & cv::EVENT_FLAG_LBUTTON)) {
updateSeaImage(*data);
}
}
unsigned int selectSea(cv::String name, cv::Mat& img, cv::Mat& mask, unsigned int brushSize, bool boat)
{
SeaCallbackData data;
data.img = &img;
data.mask = &mask;
data.name = &name;
data.prevPt = cv::Point(-1,-1);
data.brushSize = brushSize;
data.boat = boat;
updateSeaImage(data);
cv::setMouseCallback(name, selectSeaCallback, &data);
while(true)
{
int pressed = cv::waitKey(0);
if(pressed == 'q')
{
data.brushSize *= 2;
data.brushSize = data.brushSize>100?100:data.brushSize;
}
else if(pressed == 'a')
{
data.brushSize /= 2;
data.brushSize = data.brushSize<5?5:data.brushSize;
}
else break;
}
return data.brushSize;
}
void selectKeypointsRectangle(cv::String name, cv::Mat& img, std::vector<cv::KeyPoint>& keypoints, std::vector<cv::KeyPoint>& selected)
{
cv::Mat out;
bool loop = true;
// Loop until the user presses enter (or space) without selecting any ROIs
// (or until they press c as indicated in opencv's docs)
while(loop)
{
cv::drawKeypoints(img, keypoints, out, cv::Scalar(0,0,255));
cv::drawKeypoints(out, selected, out, cv::Scalar(0,255,0));
cv::Rect ROI = cv::selectROI(name, out, false);
// stop looping if no ROI is selected
if(ROI.x == 0 and ROI.y == 0 and ROI.width == 0 and ROI.height == 0)
{
loop = false;
}
// otherwise select the keypoints inside the ROI
// and loop again to select the next ROI
else
{
for(const auto& kp: keypoints)
{
if(ROI.contains(kp.pt))
{
selected.push_back(kp);
}
}
keypoints.erase( std::remove_if
(
keypoints.begin(), keypoints.end(),
[ROI](const cv::KeyPoint& kp)
{
return ROI.contains(kp.pt);
}
), keypoints.end());
}
}
}
void findAllKeypoints(cv::Mat& img, std::vector<cv::KeyPoint>& keypoints, bool shouldSharpen)
{
cv::Ptr<cv::Feature2D> sift = cv::SIFT::create();
cv::Mat equalized;
bgr_to_gray_HE(img, equalized);
if(shouldSharpen)
{
sharpen(equalized, equalized);
}
sift->detect(equalized, keypoints);
}
void createMask(cv::Mat& img, std::vector<cv::KeyPoint>& background, std::vector<cv::KeyPoint>& foreground, cv::Mat& mask)
{
bgr_to_gray_HE(img, mask);
cv::blur(mask, mask, cv::Size(3,3));
double med = median(mask);
double lower = 0.67*med;
double upper = 1.33*med;
lower = lower<0?0:lower;
upper = upper>255?255:upper;
cv::Canny(mask, mask, lower, upper);
cv::bitwise_not(mask, mask);
cv::distanceTransform(mask, mask, cv::DIST_L2, 3);
cv::normalize(mask, mask, 0, 1.0, cv::NORM_MINMAX);
mask.convertTo(mask, CV_8UC3, 255, 0);
cv::subtract(cv::Scalar::all(255),mask,mask);
// Create the marker image for the watershed algorithm
cv::Mat markers = cv::Mat::zeros(mask.size(), CV_32SC1);
// Draw the foreground markers
for(const auto& kp: foreground)
{
cv::circle(markers, kp.pt, 3, cv::Scalar(255), -1);
}
// Draw the background marker
for(const auto& kp: background)
{
cv::circle(markers, kp.pt, 3, cv::Scalar(1), -1);
}
cv::cvtColor(mask,mask,cv::COLOR_GRAY2BGR);
cv::watershed(mask, markers);
markers.convertTo(mask, CV_8U);
}
void showMask(cv::String name, cv::Mat& img, cv::Mat& mask, bool boat)
{
cv::Mat foreground;
cv::Mat background;
if (mask.channels() == 1) {
cv::cvtColor(mask,foreground,cv::COLOR_GRAY2BGR);
} else {
foreground = mask.clone();
}
cv::Mat channels[3];
cv::split(foreground, channels);
if(boat) {
channels[0] *= 0;
channels[2] *= 0;
} else {
channels[0] *= 0;
channels[1] *= 0;
}
cv::merge(&channels[0], 3, foreground);
foreground.convertTo(foreground, CV_32FC3, 0.85/255.0);
img.convertTo(background, CV_32FC3, 0.85/255.0);
cv::multiply(foreground, foreground, foreground);
cv::multiply(cv::Scalar::all(1.0)-foreground, background, background);
cv::add(foreground, background, background);
imshow(name,background);
}
void saveMask(cv::String baseName, cv::Mat& mask, bool boat)
{
if(boat)
cv::imwrite(baseName+"_maskb.png", mask);
else
cv::imwrite(baseName+"_mask.png", mask);
}
std::string type2str(int type) {
std::string r;
uchar depth = type & CV_MAT_DEPTH_MASK;
uchar chans = 1 + (type >> CV_CN_SHIFT);
switch ( depth ) {
case CV_8U: r = "8U"; break;
case CV_8S: r = "8S"; break;
case CV_16U: r = "16U"; break;
case CV_16S: r = "16S"; break;
case CV_32S: r = "32S"; break;
case CV_32F: r = "32F"; break;
case CV_64F: r = "64F"; break;
default: r = "User"; break;
}
r += "C";
r += (chans+'0');
return r;
}