THRIS.C
Upload User: gzsenex
Upload Date: 2013-02-24
Package Size: 25984k
Code Size: 3k
Category:

Graph program

Development Platform:

C/C++

  1. /* Thresholding by using the iterative selection */
  2. #define MAX
  3. #include "lib.h"
  4. void thr_is (IMAGE im);
  5. void thr_is_fast (IMAGE im);
  6. void main (int argc, char *argv[])
  7. {
  8. IMAGE data;
  9. if (argc < 3)
  10. {
  11.   printf ("Usage: thris <input file> <output file>n");
  12.   exit (0);
  13. }
  14. data = Input_PBM (argv[1]);
  15. if (data == NULL)
  16. {
  17. printf ("Bad input file '%s'n", argv[1]);
  18. exit(1);
  19. }
  20. thr_is_fast (data);
  21. Output_PBM (data, argv[2]);
  22. }
  23. void thr_is (IMAGE image)
  24. {
  25. float tt, tb, to, t2;
  26. int   i, j, t;
  27. long  N, no, nb;
  28. N = (long)image->info->nc * (long)image->info->nr;
  29. tb = 0.0;       to = 0.0;       no = 0;
  30. for (i=0; i<image->info->nr; i++) 
  31.   for (j=0; j<image->info->nc; j++)
  32.     to = to+ (image->data[i][j]);
  33. tt = (to/(float)N);
  34. while (N) 
  35. {
  36.   no = 0; nb = 0; tb=0.0; to = 0.0;
  37.   for (i=0; i<image->info->nr; i++) 
  38.     for (j=0; j<image->info->nc; j++)
  39.     {
  40.       if ( (float)(image->data[i][j]) >= tt ) 
  41.       {
  42. to = to + (float)(image->data[i][j]);
  43. no++;
  44.       } else {
  45. tb = tb + (float)(image->data[i][j]);
  46. nb++;
  47.       }
  48.     }
  49.   if (no == 0) no = 1;
  50.   if (nb == 0) nb = 1;
  51.   t2 = (tb/(float)nb + to/(float)no )/2.0;
  52.   if (t2 == tt) N=0;
  53.   tt = t2;
  54. }
  55. t = (int) tt;
  56. printf("Threshold found is %dn", t);
  57. /* Threshold */
  58. for (i=0; i<image->info->nr; i++)
  59.   for (j=0; j<image->info->nc; j++)
  60.    if (image->data[i][j] < t)
  61.      image->data[i][j] = 0;
  62.    else
  63.      image->data[i][j] = 255;
  64. }
  65. void thr_is_fast (IMAGE im)
  66. {
  67. long i, j, told, tt, a, b, c, d;
  68. long N, *hist;
  69. hist = (long *) malloc(sizeof(long)*257);
  70. for (i=0; i<256; i++) hist[i] = 0;
  71. /* Compute the mean and the histogram */
  72. N = (long)im->info->nc * (long)im->info->nr;
  73. tt = 0;
  74. for (i=0; i<im->info->nr; i++)
  75.   for (j=0; j<im->info->nc; j++)
  76.   {
  77.     hist[im->data[i][j]] += 1;
  78.     tt = tt + (im->data[i][j]);
  79.   }
  80. tt = (tt/(float)N);
  81. do
  82. {
  83.   told = tt;
  84.   a = 0; b = 0;
  85.   for (i=0; i<=told; i++)
  86.   {
  87.     a += i*hist[i];
  88.     b += hist[i];
  89.   }
  90.   b += b;
  91.   c = 0; d = 0;
  92.   for (i=told+1; i<256; i++)
  93.   {
  94.     c += i*hist[i];
  95.     d += hist[i];
  96.   }
  97.   d += d;
  98.   if (b==0) b = 1;
  99.   if (d==0) d = 1;
  100.   tt = a/b + c/d;
  101. } while (tt != told);
  102. printf ("Fast threshold is %dn", tt);
  103. free (hist);
  104. /* Threshold */
  105.  for (i=0; i<im->info->nr; i++)
  106.    for (j=0; j<im->info->nc; j++)
  107.      if (im->data[i][j] < tt)
  108.        im->data[i][j] = 0;
  109.      else
  110.        im->data[i][j] = 255;
  111. }