-
Notifications
You must be signed in to change notification settings - Fork 71
/
main.cpp
executable file
·401 lines (323 loc) · 16.4 KB
/
main.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/**
* Command line tool for the superpixel algorithm proposed in [1].
*
* [1] O. Veksler, Y. Boykov, P. Mehrani.
* Superpixels and supervoxels in an energy optimization framework.
* European Conference on Computer Vision, pages 211–224, 2010.
*
* **Unfortunately, the license of this superpixel algorithm prohibits us from
* redistributing the code, thereore we only provide the command line interface.**
*
* Installation intructions:
*
* - Got to http://www.csd.uwo.ca/faculty/olga/ and download the code.
* - Extract the archive into lib_cis to obtain the following folder structure:
*
* lib_cis
* |- vlib
* |- include
* |- utils
* |- CMakeLists.txt (provided by this library)
* |- README.txt
* |- maxflow.cpp
* |- ...
* |- CMakeLists.txt (provided by this library)
*
* - Comment out the `main` function in `superpixels.cpp`.
* - Either change the declaration of loadEdges in superpixels.cpp to
*
* void loadEdges(vector<Value> &weights,int num_pixels,int width,int height,
* Value lambda, char *name)
*
* or use -fpermissive (default).
* - Depending on the operating system, someminor changes within the
* downloaded code will be necessary. In particular, in energy.h change
* occurences of
*
* add_tweights(y, 0, C);
* add_edge(x, y, B+C, 0);
*
* to
*
* this->add_tweights(y, 0, C);
* this->add_edge(x, y, B+C, 0);
*
* The code was used for evaluation purposes in [2]:
*
* [2] D. Stutz, A. Hermans, B. Leibe.
* Superpixel Segmentation using Depth Information.
* Bachelor thesis, RWTH Aachen University, Aachen, Germany, 2014.
*
* [2] is available online at
*
* http://davidstutz.de/bachelor-thesis-superpixel-segmentation-using-depth-information/
*
* **How to use the command line tool?**
*
* $ ./bin/cis_cli --help
* Allowed options:
* --help produce help message
* --input arg folder containing the images to process
* --region-size arg (=10) maxmimum allowed region size (that is region size x
* region size patches)
* --type arg (=1) 0 for compact superpixels, 1 for constant intensity
* superpixels
* --iterations arg (=2) number of iterations
* --lambda arg (=50) lambda only influences constant intensity
* superpixels; larger lambda results in smoother
* boundaries
* --process show additional information while processing
* --time arg time the algorithm and save results to the given
* directory
* --csv save segmentation as CSV file
* --contour save contour image of segmentation
* --mean save mean colored image of segmentation
* --time save timings in BSD evaluation format in the given
* directory
* --output arg (=output) specify the output directory (default is ./output)
*
* The code (only concerning this command line tool, not the downloaded code
* from Olga Veksler!) is published under the BSD 3-Clause:
*
* Copyright (c) 2014, David Stutz
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "SeedsRevised.h"
#include "Tools.h"
#include "superpixels.h"
#include <opencv2/opencv.hpp>
#include <boost/filesystem.hpp>
#include <boost/program_options.hpp>
#include <boost/timer.hpp>
#if defined(WIN32) || defined(_WIN32)
#define DIRECTORY_SEPARATOR "\\"
#else
#define DIRECTORY_SEPARATOR "/"
#endif
int main (int argc, char ** argv) {
boost::program_options::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("input", boost::program_options::value<std::string>(), "folder containing the images to process")
("region-size", boost::program_options::value<int>()->default_value(10), "maxmimum allowed region size (that is region size x region size patches)")
("type", boost::program_options::value<int>()->default_value(1), "0 for compact superpixels, 1 for constant intensity superpixels")
("iterations", boost::program_options::value<int>()->default_value(2), "number of iterations")
("lambda", boost::program_options::value<int>()->default_value(50), "lambda only influences constant intensity superpixels; larger lambda results in smoother boundaries")
("process", "show additional information while processing")
("time", boost::program_options::value<std::string>(), "time the algorithm and save results to the given directory")
("csv", "save segmentation as CSV file")
("contour", "save contour image of segmentation")
("mean", "save mean colored image of segmentation")
("output", boost::program_options::value<std::string>()->default_value("output"), "specify the output directory (default is ./output)");
boost::program_options::positional_options_description positionals;
positionals.add("input", 1);
boost::program_options::variables_map parameters;
boost::program_options::store(boost::program_options::command_line_parser(argc, argv).options(desc).positional(positionals).run(), parameters);
boost::program_options::notify(parameters);
if (parameters.find("help") != parameters.end()) {
std::cout << desc << std::endl;
return 1;
}
boost::filesystem::path imageDir(parameters["input"].as<std::string>());
if (!boost::filesystem::is_directory(imageDir)) {
std::cout << "Image directory not found ..." << std::endl;
return 1;
}
boost::filesystem::path outputDir(parameters["output"].as<std::string>());
if (!boost::filesystem::is_directory(outputDir)) {
boost::filesystem::create_directory(outputDir);
}
bool process = false;
if (parameters.find("process") != parameters.end()) {
process = true;
}
std::vector<boost::filesystem::path> pathVector;
std::vector<boost::filesystem::path> images;
std::copy(boost::filesystem::directory_iterator(imageDir), boost::filesystem::directory_iterator(), std::back_inserter(pathVector));
std::sort(pathVector.begin(), pathVector.end());
int count = 0;
for (std::vector<boost::filesystem::path>::const_iterator iterator (pathVector.begin()); iterator != pathVector.end(); ++iterator) {
if (boost::filesystem::is_regular_file(*iterator)) {
std::string extension = iterator->extension().string();
std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
if (extension == ".png" || extension == ".jpg") {
images.push_back(*iterator);
if (process == true) {
std::cout << "Found " << iterator->string() << " ..." << std::endl;
}
++count;
}
}
}
std::cout << count << " images found ..." << std::endl;
double totalTime = 0;
int iterations = parameters["iterations"].as<int>();
int lambda = parameters["lambda"].as<int>();
int type = parameters["type"].as<int>();
if (type != 0 && type != 1) {
std::cout << "Type needs to be 0 for compact superpixels or 1 for constant intensity superpixels ...";
return 1;
}
int regionSize = parameters["region-size"].as<int>();
boost::timer timer;
cv::Mat time(images.size(), 2, cv::DataType<double>::type);
for(std::vector<boost::filesystem::path>::iterator iterator = images.begin(); iterator != images.end(); ++iterator) {
cv::Mat mat = cv::imread(iterator->string());
// Convert to PGM.
boost::filesystem::path extension = iterator->filename().extension();
int position = iterator->filename().string().find(extension.string());
cv::Mat matGrayScale;
cv::cvtColor(mat, matGrayScale, SEEDS_REVISED_OPENCV_BGR2GRAY);
std::string storeGray = outputDir.string() + DIRECTORY_SEPARATOR + iterator->filename().string().substr(0, position) + ".pgm";
cv::imwrite(storeGray, matGrayScale);
// Generate edge maps (that is, gradient maps in x and y direction)
cv::Mat gradX;
cv::Mat gradY;
cv::Mat absGradX;
cv::Mat absGradY;
cv::GaussianBlur(matGrayScale, matGrayScale, cv::Size(3,3), 0, 0, cv::BORDER_DEFAULT);
cv::Sobel(matGrayScale, gradX, CV_16S, 1, 0, 3, 1, 0, cv::BORDER_DEFAULT);
cv::convertScaleAbs(gradX, absGradX);
cv::Sobel(matGrayScale, gradY, CV_16S, 0, 1, 3, 1, 0, cv::BORDER_DEFAULT);
cv::convertScaleAbs(gradY, absGradY);
std::string storeX = outputDir.string() + DIRECTORY_SEPARATOR + iterator->filename().string().substr(0, position) + "_x.pgm";
std::string storeY = outputDir.string() + DIRECTORY_SEPARATOR + iterator->filename().string().substr(0, position) + "_y.pgm";
cv::imwrite(storeX, absGradX);
cv::imwrite(storeY, absGradY);
image<unsigned char> *I = loadPGM(storeGray.c_str());
assert(I != 0);
int width = I->width();
int height = I->height();
int num_pixels = width*height;
timer.restart();
int index = std::distance(images.begin(), iterator);
float variance = computeImageVariance(I, width, height);
// Initialize and place seeds
std::vector<int> Seeds(num_pixels);
int numSeeds = 0;
PlaceSeeds(I, width, height, num_pixels, Seeds, &numSeeds, regionSize);
MoveSeedsFromEdges(I, width, height, num_pixels, Seeds, numSeeds, regionSize);
std::vector<int> horizWeights(num_pixels,lambda);
std::vector<int> vertWeights(num_pixels,lambda);
std::vector<int> diag1Weights(num_pixels,lambda);
std::vector<int> diag2Weights(num_pixels,lambda);
loadEdges(horizWeights, num_pixels, width, height, lambda, storeX.c_str());
loadEdges(vertWeights, num_pixels, width, height, lambda, storeY.c_str());
// loadEdges(diag1Weights, num_pixels, width, height, lambda, edges[2].c_str());
// loadEdges(diag2Weights, num_pixels, width, height, lambda, edges[3].c_str());
computeWeights(horizWeights, num_pixels, width,height, lambda, variance, -1, 0, I, type);
computeWeights(vertWeights, num_pixels, width,height, lambda, variance, 0, -1, I, type);
computeWeights(diag1Weights, num_pixels, width,height, lambda, variance, -1, -1, I, type);
computeWeights(diag2Weights, num_pixels, width,height, lambda, variance, 1, -1, I, type);
vector<int> labeling(num_pixels);
initializeLabeling(labeling, width, height, Seeds, numSeeds, regionSize);
int oldEnergy, newEnergy;
std::vector<int> changeMask(num_pixels, 1);
std::vector<int> changeMaskNew(num_pixels, 0);
std::vector<int> order(numSeeds);
for (int i = 0; i < numSeeds; i++) {
order[i] = i;
}
int j = 0;
//purturbSeeds(order,numSeeds);
while (true) {
newEnergy = computeEnergy(labeling, width, height, num_pixels, horizWeights, vertWeights, diag1Weights, diag2Weights, Seeds, I, type);
if (j == 0) {
oldEnergy = newEnergy + 1;
}
if (newEnergy == oldEnergy || j >= iterations) {
break;
}
oldEnergy = newEnergy;
for (int i = 0; i < numSeeds; i++) {
expandOnLabel(order[i], width, height, num_pixels, Seeds, numSeeds, labeling, horizWeights,
vertWeights, lambda, diag1Weights, diag2Weights, regionSize, changeMask,
changeMaskNew, I, type, variance);
}
for (int i = 0; i < num_pixels; i++) {
changeMask[i] = changeMaskNew[i];
changeMaskNew[i] = 0;
}
//purturbSeeds(order,numSeeds);
j++;
}
time.at<double>(index, 1) = timer.elapsed();
time.at<double>(index, 0) = index + 1;
totalTime += time.at<double>(index, 1);
delete I;
int** labels = new int*[height];
for (int i = 0; i < height; ++i) {
labels[i] = new int[width];
for (int j = 0; j < width; ++j) {
labels[i][j] = labeling[j + i*width];
}
}
if (parameters.find("csv") != parameters.end()) {
boost::filesystem::path csvFile(outputDir.string() + DIRECTORY_SEPARATOR + iterator->filename().string().substr(0, position) + ".csv");
Export::CSV(labels, height, width, csvFile);
if (process == true) {
std::cout << "Labels for image " << iterator->string() << " saved in " << csvFile.string() << " ..." << std::endl;
}
}
if (parameters.find("contour") != parameters.end()) {
std::string store = outputDir.string() + DIRECTORY_SEPARATOR + iterator->filename().string().substr(0, position) + "_contours.png";
int bgr[] = {0, 0, 204};
cv::Mat contourImage = Draw::contourImage(labels, mat, bgr);
cv::imwrite(store, contourImage);
if (process == true) {
std::cout << "Image " << iterator->string() << " with contours saved to " << store << " ..." << std::endl;
}
}
if (parameters.find("mean") != parameters.end()) {
std::string store = outputDir.string() + DIRECTORY_SEPARATOR + iterator->filename().string().substr(0, position) + "_mean.png";
cv::Mat meanImage = Draw::meanImage(labels, mat);
cv::imwrite(store, meanImage);
if (process == true) {
std::cout << "Image " << iterator->string() << " with mean colors saved to " << store << " ..." << std::endl;
}
}
for (int i = 0; i < height; ++i) {
delete[] labels[i];
}
delete[] labels;
}
if (parameters.find("time") != parameters.end()) {
boost::filesystem::path timeDir(parameters["time"].as<std::string>());
if (!boost::filesystem::is_directory(timeDir)) {
boost::filesystem::create_directories(timeDir);
}
boost::filesystem::path timeImgFile(timeDir.string() + DIRECTORY_SEPARATOR + "eval_time_img.txt");
boost::filesystem::path timeFile(timeDir.string() + DIRECTORY_SEPARATOR + "eval_time.txt");
Export::BSDEvaluationFile<double>(time, 4, timeImgFile);
cv::Mat avgTime(1, 1, cv::DataType<double>::type);
avgTime.at<double>(0, 0) = totalTime/((double) images.size());
Export::BSDEvaluationFile<double>(avgTime, 6, timeFile);
}
std::cout << "On average, " << totalTime/images.size() << " seconds needed ..." << std::endl;
return 0;
}