-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsegm_main.cc
463 lines (416 loc) · 12.6 KB
/
segm_main.cc
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
//Color Image Segmentation
//This is the implementation of the algorithm described in
//D. Comaniciu, P. Meer,
//Robust Analysis of Feature Spaces: Color Image Segmentation,
//http://www.caip.rutgers.edu/~meer/RIUL/PAPERS/feature.ps.gz
//appeared in Proceedings of CVPR'97, San Juan, Puerto Rico.
// ===========================================================================
// ===== Module: segm_main.cc
// ===== -------------------------------------------------------------- ======
// ===== Version 01 Date: 04/22/97
// ===== -------------------------------------------------------------- ======
// ===========================================================================
// ===== Written by Dorin Comaniciu
// ===== e-mail: comanici@caip.rutgers.edu
// ===========================================================================
// Permission to use, copy, or modify this software and its documentation
// for educational and research purposes only is hereby granted without
// fee, provided that this copyright notice appear on all copies and
// related documentation. For any other uses of this software, in original
// or modified form, including but not limited to distribution in whole
// or in part, specific prior permission must be obtained from
// the author(s).
//
// THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
// EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
// WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
//
// IN NO EVENT SHALL RUTGERS UNIVERSITY BE LIABLE FOR ANY SPECIAL,
// INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY
// DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
// WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY
// THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE
// OR PERFORMANCE OF THIS SOFTWARE.
// ===========================================================================
//
#include <stdlib.h>
#include <math.h>
#include <memory.h>
//#include <fstream.h>
//#include <strstream.h>
#include <fstream>
#include <iostream>
#include <strstream>
#include "segm.hh"
#include <string.h>
using namespace std;
extern int option;
static RasterIpChannels* read_PPM_file( istream* fp );
Boolean write_PPM_file( char* fname, RasterIpChannels* signal );
Boolean my_write_PGM_file( char* fname, Octet* signal,int _rows, int _colms );
/****************************************************************/
/* */
/* Run with: */
/* segm image_name.ppm */
/* */
/****************************************************************/
int main( int argc, char **argv )
{
int i;
SegmenterMS::sRectangle rects[Max_rects];
istream* fp;
ostrstream cmd;
char fname[1024];
cmd << "convert " << argv[1] << " " << argv[1] << ".ppm" << ends;
system( cmd.str() );
sprintf(fname, "%s.ppm", argv[1]);
fp = new ifstream( fname, ios::in );
if ( fp->fail() )
{
cerr << __FILE__ << '(' << __LINE__ << "): cannot open file " <<
argv[1] << endl;
cerr << "Usage: segm image_name.ppm"<< endl;
exit(1);
}
long selects = 0L;
selects |= Lightness; selects |= Ustar; selects |= Vstar;
int p_dim=3;
assert( p_dim <= p_max ); // must be since all array dimensions are such
Boolean block = false;
unsigned int seed = 29254088; //random # generator
int n_rect = 0;
Boolean done = false;
/*
cout << "Select:"<< endl
<<" w = undersegmentation inside a window"<< endl
<<" u = undersegmentation" << endl
<<" o = oversegmentation" << endl
<<" q = quantization" << endl;
char received;
*/
char received;
if(argc > 2) {
received = *argv[2];
} else {
received = 'u';
}
while(1)
{
// cin >> received;
if((received =='w') || (received=='W'))
{
option=2; break;
}
else if((received =='u') || (received=='U'))
{
option=2; done=true; break;
}
else if((received =='o') || (received=='O'))
{
option=1; done=true; break;
}
else if((received =='q') || (received=='Q'))
{
option=0; done=true; break;
}
else
{
option=2; done=true; break;
//cout << endl <<"Please type w, u, o or q !" << endl;
}
}
/*
#ifdef ORG_SIZE
cmd << "xv " << argv[1] << " &" << ends;
#else
cmd << "xv -geometry 120x120 " << argv[1] << " &" << ends;
#endif
system( cmd.str() );
*/
while ( !done )
{
cout << "*********** This is the " << n_rect+1 <<
(n_rect==0 ? "st" : (n_rect==1 ? "nd" : (n_rect==2 ? "rd" : "th")))
<< " Window! ************" << endl;
cout << "The Upper_Left Corner Coords:" << endl;
int x1, y1;
cin >> x1 >> y1;
cout << endl;
if((x1 < 0) || (y1 < 0))
{
cout << "********* " << n_rect << " Windows totally *******" << endl;
done = true;
break;
}
cout << "The Width and Height of window:" << endl;
int d1, d2;
cin >> d1 >> d2;
cout << endl;
if ( (d1 == 0) || (d2 == 0) )
{
cout << "********* " << n_rect << " Windows totally *******" << endl;
done = true;
break;
}
rects[n_rect].x = x1; rects[n_rect].y = y1;
rects[n_rect].width = d1; rects[n_rect].height = d2;
n_rect++;
}
RasterIpChannels* signal = read_PPM_file( fp );
((ifstream* )fp)->close();
SegmenterMS segmenter;
segmenter.ms_segment( signal, rects, n_rect, selects, seed, block);
RasterIpChannels* sig_result = segmenter.result_ras_;
write_PPM_file( "result.ppm", sig_result );
ostrstream cmd1;
ostrstream cmd2;
/*
#ifdef ORG_SIZE
cmd1 << "xv result.ppm &" << ends;
if(option && !n_rect)
cmd2 << "xv result.pgm &" << ends;
#else
cmd1 << "xv -geometry 120x120 result.ppm &" << ends;
if(option && !n_rect)
cmd2 << "xv -geometry 120x120 result.pgm &" << ends;
#endif
system( cmd1.str() );
if(option && !n_rect)
system( cmd2.str() );
*/
delete signal;
delete sig_result;
return( 0 );
}
static void skip( istream* fp )
{
char c1, c2;
if ( (c1 = fp->get() ) != '#' ) {
fp->putback( c1 );
return;
}
while ( c1 == '#' )
{
while ( (c2 = fp->get()) != '\n' )
;
c1 = fp->get();
if ( c1 != '#' )
fp->putback( c1 );
}
return;
}
static RasterIpChannels* read_PPM_file( istream* fp )
{
int c1 = fp->get();
int c2 = fp->get();
int c3 = fp->get();
// test point
if ( c1 == 'P' && c3 == '\n' )
{
Octet** datain = new Octet*[p_max];
int w, h, m;
switch ( c2 ) {
case '3':
{
skip( fp );
*fp >> w >> h;
int i;
for ( i = 0; i < p_max; i++ ) {
datain[i] = new Octet[w * h];
}
fp->get();
skip( fp );
*fp >> m;
//need a test for # comments !!!
for ( int j = 0, idx = 0; j < h; j++ ) {
for ( i = 0; i < w; i++, idx++ ) {
*fp >> c1 >> c2 >> c3; //ASCII decimal values
datain[0][idx] = c1;
datain[1][idx] = c2;
datain[2][idx] = c3;
}
}
}
break;
case '6':
{
skip( fp );
*fp >> w >> h;
for ( int i = 0; i < p_max; i++ ) {
datain[i] = new Octet[w * h];
}
fp->get();
skip( fp );
*fp >> m;
// test point
fp->get();
skip( fp );
cout << "Image Dim h: " << h << " w: " << w << endl;
cout << "Max Value m: " << m << endl;
Octet *temp_buf = new Octet[h*w*3];
//unsigned char *temp_buf = (unsigned char*)malloc(sizeof(unsigned char) * h*w*3);
fp->read((char*)temp_buf, h*w*3);
Octet *temp0 = datain[0];
Octet *temp1 = datain[1];
Octet *temp2 = datain[2];
for ( register int j = 0, idx = 0; j < h*w; j++) {
temp0[j] = temp_buf[idx++];
temp1[j] = temp_buf[idx++];
temp2[j] = temp_buf[idx++];
}
delete [] temp_buf;
}
break;
default:
cerr << "File is not the PPM format." << endl;
return NULL;
}
XfRaster::Info info;
info.rows = h;
info.columns = w;
info.origin_x = 0;
info.origin_y = 0;
return (new RasterIpChannels( info, p_max, eDATA_OCTET,
datain, true ) );
}
cerr << "File is not the PPM format." << endl;
return NULL;
}
Boolean my_write_PGM_file( char* fname, Octet* signal,int _rows, int _colms )
{
ofstream fp( fname, ios::out );
if ( fp.fail() )
return false;
fp << "P5\n" << _colms << ' ' << _rows<< "\n255" << endl;
// festr fp.write(signal,_rows*_colms);
fp.write((char*)signal,_rows*_colms);
fp.close( );
return true;
}
Boolean write_PPM_file( char* fname, RasterIpChannels* signal )
{
ofstream fp( fname, ios::out );
if ( fp.fail() )
return false;
fp << "P6\n" << signal->columns_ << ' ' << signal->rows_ << "\n255" << endl;
assert( signal->dtype_ == eDATA_OCTET );
Octet *temp_buf = new Octet[signal->rows_*signal->columns_*3];
Octet *temp0 = signal->chdata_[0];
Octet *temp1 = signal->chdata_[1];
Octet *temp2 = signal->chdata_[2];
for ( register int j = 0, idx = 0; j < signal->rows_*signal->columns_; j++) {
temp_buf[idx++]=temp0[j]; //red
temp_buf[idx++]=temp1[j]; //green
temp_buf[idx++]=temp2[j]; //blue
}
// festr fp.write(temp_buf,signal->rows_*signal->columns_*3);
fp.write((char*)temp_buf,signal->rows_*signal->columns_*3);
delete [] temp_buf;
fp.close( );
return true;
}
// Class constructor
// The `data' may be taken away from the caller in order to
// avoid time-consuming copying of the data. However,
// the caller has to give explicit permission for this.
RasterIpChannels::RasterIpChannels(
const XfRaster::Info& info, const int n_channels,
const DataType dtype, Octet* data[], const Boolean do_take
) {
rows_ = info.rows;
columns_ = info.columns;
dtype_ = dtype;
clipf_ = false;
n_channels_ = n_channels;
if (n_channels_ == 0) {
n_channels_ = 1;
}
size_t size = (size_t)(rows_ * columns_);
chdata_ = new Octet*[n_channels_];
for (int channel = 0; channel < n_channels_; channel++) {
if ( do_take == true ) { // take over the data from the caller
chdata_[channel] = (Octet* )data[channel];
data[channel] = nil;
} else {
if ( dtype_ == eDATA_FLOAT ) size *= sizeof(float);
chdata_[channel] = new Octet[size];
if (data != nil && data[channel] != nil) {
memmove( chdata_[channel], data[channel], size );
} else {
memset( chdata_[channel], 0, size );
}
}
}
delete [] data;
}
RasterIpChannels::~RasterIpChannels() {
for (int channel = 0; channel < n_channels_; channel++) {
if (chdata_[channel]) delete [] chdata_[channel];
chdata_[channel] = nil;
}
delete [] chdata_;
}
// RANGE forces `a' to be in the range [b..c] (inclusive)
inline void RANGE( int& a, const int b, const int c )
{
if ( a < b ) {
a = b;
} else if ( a > c ) {
a = c;
}
}
void RasterIpChannels::raster_info(Info& i) {
i.rows = rows_;
i.columns = columns_;
i.origin_x = 0;
i.origin_y = 0;
}
// Move floating point array to Octet array, i.e. to [0..255]
// The result is either scaled to the range [0..255] or
// clipped to this range, depending on the flag `clipf'.
// Note: Handles only 1-Octet per pixel pictures
// (i.e. mono/pseudo color pictures)
Octet** RasterIpChannels::raster_float_to_Octet(
RasterIpChannels& ras
) {
assert( ras.dtype() == eDATA_FLOAT );
float maxv = -1.0e38;
XfRaster::Info info;
ras.raster_info(info);
size_t size = (size_t)(info.rows * info.columns);
Octet** data = ras.chdata();
int channels = ras.n_channels();
Octet** picRes = new Octet*[channels];
int i;
for ( i = 0; i < channels; i++ )
picRes[i] = new Octet[ size ];
if ( ras.clipf() == true ) { // clip the values outside the range [0..255]
int p;
for ( i = 0; i < channels; i++ ) {
register float* ptr1 = (float* )data;
register Octet* ptr2 = picRes[i];
for ( register int off = 0; off < size; off++, ptr1++, ptr2++ ) {
p = int(*ptr1);
RANGE( p, 0, 255 );
*ptr2 = Octet(p);
}
}
} else { // scale the values to the range [0..255]
for ( i = 0; i < channels; i++ ) {
float minv = (float) 1.e38;
float maxv = (float) -1.e38;
register float* ptr1 = (float* ) data[i];
register Octet* ptr2 = picRes[i];
register int off;
for ( off = 0; off < size; off++, ptr1++ ) {
if ( *ptr1 < minv ) minv = *ptr1;
if ( *ptr1 > maxv ) maxv = *ptr1;
}
ptr1 = (float* ) data[i];
float ratio = (float) 255.0 / (maxv - minv);
for ( off = 0; off < size; off++, ptr1++, ptr2++ )
*ptr2 = Octet( (*ptr1 - minv) * ratio );
}
}
return ( picRes );
}