Page 195 -
P. 195

Chapter 4 ■ Grey-Level Segmentation    169



                                 #define MAX
                                 #include “lib.h“
                                 void thrdd (IMAGE im);

                                 void main (int argc, char *argv[])
                                 {
                                         IMAGE data;

                                         if (argc < 3)
                                         {
                                           printf (“Usage: thrdd <input file> <output file>\n“);
                                           exit (0);
                                         }
                                         data = Input_PBM (argv[1]);
                                         if (data == NULL)
                                         {
                                                printf (“Bad input file '%s’\n“, argv[1]);
                                                exit(1);
                                         }
                                         thrdd (data);
                                         Output_PBM (data, argv[2]);
                                 }

                                 void thrdd (IMAGE im)
                                  {
                                          int NC, row, col, inc;
                                          float mean, s, sum;
                                          unsigned char *p;
                                          long N, i;

                                          N = (long)im->info->nc * (long)im->info->nr;
                                          NC = im->info->nc; s = (int)(float)(NC/Navg);
                                          sum = 127*s; row = col = 0; inc = 1;
                                          p = &(im->data[0][0]);

                                          for (i=0; i<N-1; i++) {
                                            if (col >= NC) {
                                              col = NC-1; row++; inc = -1;
                                              p = &(im->data[row][col]);
                                            } else if (col < 0)
                                            {
                                              col = 0; row++; inc = 1;
                                              p = &(im->data[row][col]);
                                            }
                                            sum = sum - sum/s + *p;
                                            mean = sum/s;
                                            if (*p < mean*(100-pct)/100.0) *p = 0; else *p = 255;
                                            p += inc; col += inc;
                                          }
                                  }

                               Figure 4.15: Source code for the program thrdd.c: adaptive thresholding using a moving
                               average.
   190   191   192   193   194   195   196   197   198   199   200