segmentation
All Data Structures Namespaces Files Functions Variables Modules Pages
BgGlobalFc.cpp
1 // Name: BgGlobalFc.cpp
3 // Purpose: global functions
4 // Author: Bogdan Georgescu
5 // Modified by:
6 // Created: 06/22/2000
7 // Copyright: (c) Bogdan Georgescu
8 // Version: v0.1
10 
11 #include <math.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include "edge/BgDefaults.h"
15 // !!! only MSV
16 #include <sys/timeb.h>
17 #include <time.h>
18 
19 double bgSign(double x)
20 {
21  if (x>=0)
22  return 1.0;
23  return -1.0;
24 }
25 
26 double factorial(double num)
27 {
28  if (num==0 || num==1)
29  return 1;
30  return (num * factorial(num - 1));
31 }
32 
33 // return 0 - error
34 // return 1 - one real solution
35 // return 3 - three real solutions
36 int bgSolveCubic(double a, double b, double c, double d, double& s1, double& s2, double& s3)
37 {
38  double p, q;
39  double r, s, t, z;
40 
41  // convert to canonical form
42  r = b/a;
43  s = c/a;
44  t = d/a;
45  p = s-(r*r)/3.0;
46  q = (2*r*r*r)/27.0-(r*s)/3.0+t;
47 
48  double D, phi, R;
49  R = bgSign(q)*sqrt(fabs(p)/3.0);
50  D = pow(p/3.0,3)+pow(q/2,2);
51 
52  if (p<0)
53  {
54  if (D<=0)
55  {
56  phi = acos(q/(2*R*R*R));
57  s1 = -2*R*cos(phi/3)-r/3;
58  s2 = -2*R*cos(phi/3+2*PI/3)-r/3;
59  s3 = -2*R*cos(phi/3+4*PI/3)-r/3;
60  return 3;
61  }
62  else
63  {
64  z = q/(2*R*R*R);
65  phi = log(z+sqrt(z*z-1));
66  s1 = -2*R*cosh(phi/3)-r/3;
67  return 1;
68  }
69  }
70  else
71  {
72  z = q/(2*R*R*R);
73  phi = log(z+sqrt(z*z+1));
74  s1 = -2*R*sinh(phi/3)-r/3;
75  return 1;
76  }
77 
78  return 0;
79 }
80 
81 
82 
83 inline int bgRound(double in_x)
84 {
85  return int(floor(in_x + 0.5));
86 }
87 
88 inline int bgRoundSign(double in_x)
89 {
90  if (in_x>=0)
91  {
92  return ((int) (in_x + 0.5));
93  }
94  else
95  {
96  return ((int) (in_x - 0.5));
97  }
98 }
99 
100 void bgSort(double* ra, int nVec)
101 {
102  unsigned long n, l, ir, i, j;
103  n = nVec;
104  double rra;
105 
106  if (n<2)
107  return;
108 
109  l = (n>>1)+1;
110  ir = n;
111  for (;;)
112  {
113  if (l>1)
114  {
115  rra = ra[(--l)-1];
116  }
117  else
118  {
119  rra = ra[ir-1];
120  ra[ir-1] = ra[1-1];
121  if (--ir==1)
122  {
123  ra[1-1] = rra;
124  break;
125  }
126  }
127  i = l;
128  j = l+l;
129  while (j<=ir)
130  {
131  if (j<ir && ra[j-1]<ra[j+1-1])
132  j++;
133  if (rra<ra[j-1])
134  {
135  ra[i-1] = ra[j-1];
136  i = j;
137  j <<= 1;
138  }
139  else
140  j = ir+1;
141  }
142  ra[i-1] = rra;
143  }
144 }
145 
146 // rank in 0-1 range, 0 min, 1 max
147 // inplace sort vec
148 double bgMedian(double* vec, int nVec, double rank)
149 {
150  bgSort(vec, nVec);
151  int krank = int(floor(rank*nVec));
152  if (rank == 1)
153  krank = nVec-1;
154  return vec[krank];
155 }
156 
157 double bgMedianToSigmaGaussian(double med)
158 {
159  return med * 1.482602219;
160 }
161 
162 int write_pgm_image(const char *outfilename, unsigned char *image, int rows,
163  int cols, char *comment, int maxval)
164 {
165  FILE *fp;
166 
167  /***************************************************************************
168  * Open the output image file for writing if a filename was given. If no
169  * filename was provided, set fp to write to standard output.
170  ***************************************************************************/
171 
172  if(outfilename == NULL) fp = stdout;
173  else{
174  if((fp = fopen(outfilename, "wb")) == NULL){
175  fprintf(stderr, "Error writing the file %s in write_pgm_image().\n",
176  outfilename);
177  return(0);
178  }
179  }
180 
181  /***************************************************************************
182  * Write the header information to the PGM file.
183  ***************************************************************************/
184 
185  fprintf(fp, "P5\n%d %d\n", cols, rows);
186  if(comment != NULL)
187  if(strlen(comment) <= 70) fprintf(fp, "# %s\n", comment);
188  fprintf(fp, "%d\n", maxval);
189 
190  /***************************************************************************
191  * Write the image data to the file.
192  ***************************************************************************/
193 
194  if(rows != fwrite(image, cols, rows, fp)){
195  fprintf(stderr, "Error writing the image data in write_pgm_image().\n");
196  if(fp != stdout) fclose(fp);
197  return(0);
198  }
199 
200  if(fp != stdout) fclose(fp);
201  return(1);
202 }
203 
204 
205 void write_MATLAB_ASCII(char *filename, float *data, int rows, int cols)
206 {
207  FILE *fp = fopen(filename, "wb");
208  int i,j;
209  for(i = 0; i < rows; i++)
210  {
211  for(j = 0; j < cols-1; j++)
212  {
213  fprintf(fp, "%10.6f ", data[i*rows+j]);
214  }
215  fprintf(fp, "%10.6f\n", data[i*rows+cols-1]);
216  }
217  fclose(fp);
218 }
219 
220 /*
221 struct _timeb timestart;
222 struct _timeb timeend;
223 void timer_start()
224 {
225  _ftime( &timestart );
226  bgLog("timer start...\n");
227 }
228 void timer_stop()
229 {
230  _ftime( &timeend );
231 
232  unsigned long seconds;
233  unsigned long milliseconds;
234  seconds = timeend.time - timestart.time;
235  long msdif;
236  msdif = timeend.millitm - timestart.millitm;
237  if (msdif > 0)
238  milliseconds = msdif;
239  else
240  {
241  seconds--;
242  milliseconds = (timeend.millitm + 1000) - timestart.millitm;
243  }
244 
245  bgLog("timer stop, elapsed %d.%d seconds.\n", seconds, milliseconds);
246 }
247 */
248 time_t timestart;
249 time_t timeend;
250 void timer_start()
251 {
252  timestart = clock();
253  bgLog("timer start...\n");
254 }
255 void timer_stop()
256 {
257  timeend = clock();
258  unsigned long seconds, milliseconds;
259  seconds = (timeend-timestart)/CLOCKS_PER_SEC;
260  milliseconds = ((100*(timeend-timestart))/CLOCKS_PER_SEC) - 100*seconds;
261  bgLog("timer stop, elapsed %d.%d seconds.\n", seconds, milliseconds);
262 }
263 
264 
265 /********************************************/
266 /* Image Sampling */
267 /********************************************/
268 
269 //zooms into an image
270 //pre :
271 // - dest is the pre-allocated destination color image
272 // - src is the (w x h) source color image
273 // - zconst is an integer zoom constant from that is >= 1
274 // - interpolate is a flag that determines if interpolation
275 // should occur
276 //post: the src image has been zoomed by (zconst)x and stored into dest
277 void bgZoomIn(unsigned char **dest, unsigned char *src, int w, int h, int zconst, bool interpolate)
278 {
279 
280  //if dest or src is NULL or zconst is < 1 exit
281  if((!(*dest))||(!src)||(zconst < 1))
282  return;
283 
284  //if zconst = 1 then copy the image contents and return
285  if(zconst == 1)
286  {
287  memcpy(*dest, src, 3*w*h*sizeof(unsigned char));
288  return;
289  }
290 
291  //calculate new image dimension
292  w *= zconst;
293  h *= zconst;
294 
295  //copy image data from source image to destination image
296 
297  //******************************************************//
298 
299  unsigned char *dptr = (*dest);
300 
301  int i, j, x, y, dp, offset, factor, index;
302  index = 0;
303  //zconst does not divide evenly into image width
304  if(factor = w%zconst)
305  {
306  for(j=0; j<(h-zconst); j+=zconst)
307  {
308  for(i=0; i<(w-zconst); i+=zconst)
309  {
310  dp = 3*(j*w+i);
311  for(y=0; y<zconst; y++)
312  {
313  for(x=0; x<zconst; x++)
314  {
315  offset = 3*(y*w+x);
316  dptr[dp+offset ] = src[index ];
317  dptr[dp+offset+1] = src[index+1];
318  dptr[dp+offset+2] = src[index+2];
319  }
320  }
321 
322  //next data point
323  index += 3;
324  }
325 
326  dp = 3*(j*w+i);
327  for(y=0; y<(zconst-factor-1); y++)
328  {
329  for(x=0; x<(zconst-factor-1); x++)
330  {
331  offset = 3*(y*w+x);
332  dptr[dp+offset ] = src[index ];
333  dptr[dp+offset+1] = src[index+1];
334  dptr[dp+offset+2] = src[index+2];
335  }
336  }
337 
338  //next data point
339  index += 3;
340  }
341  }
342  //zconst does divide evenly into image width
343  else
344  {
345  for(j=0; j<(h-zconst); j+=zconst)
346  {
347  for(i=0; i<(w); i+=zconst)
348  {
349  dp = 3*(j*w+i);
350  for(y=0; y<zconst; y++)
351  {
352  for(x=0; x<zconst; x++)
353  {
354  offset = 3*(y*w+x);
355  dptr[dp+offset ] = src[index ];
356  dptr[dp+offset+1] = src[index+1];
357  dptr[dp+offset+2] = src[index+2];
358  }
359  }
360 
361  //next data point
362  index += 3;
363  }
364  }
365  }
366 
367  //consider last row of image
368  factor = h%zconst;
369  dp = 3*(h-zconst)*(w);
370  for(i=0; i<(w); i+=zconst)
371  {
372  for(y=0; y<(zconst-factor); y++)
373  {
374  for(x=0; x<(zconst-factor); x++)
375  {
376  offset = 3*(y*w+x);
377  dptr[dp+offset ] = src[index ];
378  dptr[dp+offset+1] = src[index+1];
379  dptr[dp+offset+2] = src[index+2];
380  }
381  }
382  //next data point
383  dp += zconst*3;
384  index += 3;
385  }
386 
387  //******************************************************//
388 
389  //done.
390  return;
391 
392 }
393 
394 //zooms out of an image
395 //pre :
396 // - dest is the pre-allocated destination color image
397 // - src is the (w x h) source color image
398 // - zconst is an integer zoom constant from that is >= 1
399 // - interpolate is a flag that determines if interpolation
400 // should occur
401 //post: the src image has been zoomed by (1/zconst)x and stored into dest
402 void bgZoomOut(unsigned char **dest, unsigned char *src, int w, int h, int zconst, bool interpolate)
403 {
404 
405  //if dest or src is NULL or zconst is <= 1 exit
406  if((!(*dest))||(!src)||(zconst <= 1))
407  return;
408 
409  //copy image data from source image to destination image
410 
411  //******************************************************//
412 
413  unsigned char *dptr = (*dest);
414 
415  int i, j, dp, index;
416  index = 0;
417  for(j=0; j<h; j+=zconst)
418  {
419  for(i=0; i<w; i+=zconst)
420  {
421  dp = 3*(j*w+i);
422  dptr[index ] = src[dp ];
423  dptr[index+1] = src[dp+1];
424  dptr[index+2] = src[dp+2];
425 
426  //next data point
427  index += 3;
428  }
429  }
430 
431  //******************************************************//
432 
433  //done.
434  return;
435 
436 }
437 
438 /************************************/
439 /* Filename Manipulation */
440 /************************************/
441 
442 //adds an extension (label) to a filename
443 void BgAddExtension(char **filename, char *label)
444 {
445  //allocate memory for new filename
446  char *new_filename = new char [strlen(*filename) + strlen(label) + 1], ext[5];
447 
448  //copy filename
449  strcpy(new_filename, *filename);
450 
451  //get extension of filename (e.g. '.txt')
452  char *pdest = strchr(new_filename, '.');
453  strcpy(ext, pdest);
454 
455  //place filename label at the end of the filename
456  //followed by extension...
457  strcpy(pdest, label);
458  strcat(new_filename, ext);
459 
460  //delete old filename and replace it with new one...
461  delete *filename;
462  (*filename) = new_filename;
463 
464  //done.
465  return;
466 }
467