-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
341 lines (271 loc) · 9.26 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
/* -- Serge Bobbia : serge.bobbia@u-bourgogne.fr -- Le2i 2018
* This work is distributed for non commercial use only,
* it implements the IBIS method as described in the CVPM 2018 paper.
* Read the ibis.h file for options and benchmark instructions
*
* This file show how to instanciate the IBIS class
* You can either provide a file, or a directory, path to segment images
*/
#include <iostream>
#include "ibis.h"
#include <opencv2/opencv.hpp>
#include <unistd.h>
#include <cmath>
#include <fstream>
#include <highgui.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
using namespace std;
//=================================================================================
/// DrawContoursAroundSegments
///
/// Internal contour drawing option exists. One only needs to comment the if
/// statement inside the loop that looks at neighbourhood.
//=================================================================================
void DrawContoursAroundSegments(
unsigned int*& ubuff,
int*& labels,
const int& width,
const int& height,
const unsigned int& color )
{
const int dx8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int dy8[8] = { 0, -1, -1, -1, 0, 1, 1, 1};
int sz = width*height;
vector<bool> istaken(sz, false);
vector<int> contourx(sz);
vector<int> contoury(sz);
int mainindex(0);int cind(0);
for( int j = 0; j < height; j++ )
{
for( int k = 0; k < width; k++ )
{
int np(0);
for( int i = 0; i < 8; i++ )
{
int x = k + dx8[i];
int y = j + dy8[i];
if( (x >= 0 && x < width) && (y >= 0 && y < height) )
{
int index = y*width + x;
//if( false == istaken[index] )//comment this to obtain internal contours
{
if( labels[mainindex] != labels[index] ) np++;
}
}
}
if( np > 1 )
{
contourx[cind] = k;
contoury[cind] = j;
istaken[mainindex] = true;
//img[mainindex] = color;
cind++;
}
mainindex++;
}
}
int numboundpix = cind;//int(contourx.size());
for( int j = 0; j < numboundpix; j++ )
{
int ii = contoury[j]*width + contourx[j];
ubuff[ii] = 0xffffff;
for( int n = 0; n < 8; n++ )
{
int x = contourx[j] + dx8[n];
int y = contoury[j] + dy8[n];
if( (x >= 0 && x < width) && (y >= 0 && y < height) )
{
int ind = y*width + x;
if(!istaken[ind])
ubuff[ind] = 0;
}
}
}
}
void write_labels(IplImage* input, const std::string& output_labels)
{
std::ofstream file;
file.open(output_labels.c_str());
unsigned char* data = (unsigned char*)input->imageData;
for (int y=0 ; y<input->height ; y++)
{
for (int x=0 ; x<input->width-1 ; x++)
{
file << (int) data[y*input->widthStep + x*input->nChannels] << " ";
}
file << (int) data[y*input->widthStep + (input->width -1)*input->nChannels] << std::endl;
}
file.close();
}
std::string get_name(const std::string& path_with_ext)
{
int deb = path_with_ext.find_last_of("/");
int fin = path_with_ext.find_last_of(".");
return path_with_ext.substr(deb+1, fin-deb-1);
}
void execute_IBIS( int K, int compa, IBIS* Super_Pixel, cv::Mat* img, std::string output_basename ) {
// process IBIS
Super_Pixel->process( img );
// convert int* labels to Mat* labels in gray scale
int* labels = Super_Pixel->getLabels();
const int width = img->cols;
const int height = img->rows;
const int color = 0xFFFFFFFF;
std::string output_labels = get_name(output_basename);
output_labels = std::string("results/") + output_labels + std::string(".seg");
ofstream outfile;
outfile.open(output_labels.c_str());
for (int y=0 ; y<height ; y++)
{
for (int x=0 ; x<width-1 ; x++)
{
outfile << labels[y*width + x] << " ";
}
outfile << labels[y*width + width-1] << std::endl;
}
outfile.close();
IplImage* output_bounds_alpha = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 4);
cvSet(output_bounds_alpha, cvScalar(0,0,0,0));
IplImage* output_bounds = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3);
unsigned int* ubuff = (unsigned int*)output_bounds_alpha->imageData;
DrawContoursAroundSegments(ubuff, labels, width, height, color);
cvCvtColor(output_bounds_alpha, output_bounds, CV_RGBA2RGB);
std::string output_boundaries_name = get_name(output_basename);
output_boundaries_name = std::string("results/") + output_boundaries_name + std::string("_boundary.png");
cvSaveImage(output_boundaries_name.c_str(), output_bounds);
cvReleaseImage(&output_bounds_alpha);
cvReleaseImage(&output_bounds);
}
int filter( const struct dirent *name ) {
std::string file_name = std::string( name->d_name );
std::size_t found = file_name.find(".png");
if (found!=std::string::npos) {
return 1;
}
found = file_name.find(".jpg");
if (found!=std::string::npos) {
return 1;
}
found = file_name.find(".ppm");
if (found!=std::string::npos) {
return 1;
}
found = file_name.find(".bmp");
if (found!=std::string::npos) {
return 1;
}
found = file_name.find(".tiff");
if (found!=std::string::npos) {
return 1;
}
return 0;
}
int main( int argc, char* argv[] )
{
printf(" - Iterative Boundaries implicit Identification for Segmentation - \n\n");
int K;
int compa;
if( argc != 4 ) {
printf("--> usage ./ibis SP_number Compacity File_path\n");
printf(" |-> SP_number: user fixed number of superpixels, > 0\n");
printf(" |-> Compacity: factor of caompacity, set to 20 for benchmark, > 0\n");
printf(" |-> File_path: path to the file to compute\n");
printf(" |-> if file_path is a directory, all the image within file_path/ are processed\n");
printf("format: .png, .jpg, .ppm, .bmp, .tiff\n");
printf("\n");
printf("--> output file are saved in a \"./results\" directory\n");
exit(EXIT_SUCCESS);
}
else {
K = atoi( argv[ 1 ] );
compa = atoi( argv[ 2 ] );
if( K < 0 || compa < 0 ) {
printf("--> usage ./ibis SP_number Compacity File_path\n");
printf(" |-> SP_number: user fixed number of superpixels, > 0\n");
printf(" |-> Compacity: factor of caompacity, set to 20 for benchmark, > 0\n");
printf(" |-> File_path: path to the file to compute\n");
printf(" |-> if file_path is a directory, all the image within file_path/ are processed\n");
printf("format: .png, .jpg, .ppm\n");
printf("\n");
printf("--> output file are saved in a \"./results\" directory\n");
exit(EXIT_SUCCESS);
}
}
// determine mode : file or path
struct stat sb;
if (stat(argv[3], &sb) == -1) {
perror("stat");
exit(EXIT_SUCCESS);
}
int type;
switch (sb.st_mode & S_IFMT) {
case S_IFDIR:
printf("directory processing\n");
type=0;
break;
case S_IFREG:
printf("single file processing\n");
type=1;
break;
default:
type=-1;
break;
}
if( type == -1 )
exit(EXIT_SUCCESS);
else if( type == 1 ) {
// IBIS
IBIS Super_Pixel( K, compa );
// get picture
cv::Mat img = cv::imread( argv[ 3 ] );
// execute IBIS
execute_IBIS( K, compa, &Super_Pixel, &img, argv[ 3 ] );
}
else if( type == 0 ) {
// get file list
struct dirent **namelist;
int n = scandir(argv[3], &namelist, &filter, alphasort);
if (n == -1) {
perror("scandir");
exit(EXIT_FAILURE);
}
printf(" %i image(s) found\n", n);
if( n == 0 )
exit(EXIT_SUCCESS);
// process file list
int width = 0;
int height = 0;
IBIS* Super_Pixel;
char* image_name = (char*)malloc(255);
while (n--) {
printf("processing %s\n", namelist[n]->d_name);
// get picture
sprintf(image_name, "%s/%s", argv[3], namelist[n]->d_name );
cv::Mat img = cv::imread( image_name );
// execute IBIS
if( width == 0 ) {
width = img.cols;
height = img.rows;
// IBIS
Super_Pixel = new IBIS( K, compa );
}
else {
if( width != img.cols ) {
delete Super_Pixel;
Super_Pixel = new IBIS( K, compa );
width = img.cols;
height = img.rows;
}
}
execute_IBIS( K, compa, Super_Pixel, &img, image_name );
free(namelist[n]);
printf("\n");
}
free( image_name );
delete Super_Pixel;
free( namelist );
}
exit(EXIT_SUCCESS);
}