home *** CD-ROM | disk | FTP | other *** search
/ Practical Algorithms for Image Analysis / Practical Algorithms for Image Analysis.iso / TARFILE.GZ / tarfile / ch_3.5 / bcd / edge / bcd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-09-11  |  2.1 KB  |  108 lines

  1. /*
  2.  * (c) Copyright 1988 by
  3.  * Robotics Principles Research Department, ATT Bell Laboratories.
  4.  * All rights reserved.
  5.  * Last modified 2/8/88 Ingemar J. Cox
  6.  * Revised 4/22/88 Ingemar J. Cox
  7.  * Revised 11/11/88 Ingemar J. Cox
  8.  */
  9. #include <stdio.h>
  10. #include "edge_finder.h"
  11.  
  12. #define UBYTE_HD 0x4500
  13.  
  14. main(argc, argv)
  15. int argc;
  16. char *argv[];
  17. {
  18.     short header[5];
  19.     int nx, ny;
  20.     static unsigned char *pic;
  21.     float sigma;
  22.     int threshold;
  23.     int hysteresis = 1;
  24.     int thinning = 1;
  25.     int cnt, i;
  26.     
  27.     unsigned char *edge_map;
  28.     unsigned char *image_find_edges();
  29.     unsigned char *image_hysteresis();
  30.     unsigned char *image_thin();
  31.     float atof();
  32.     
  33.     if(argc<2)
  34.     {
  35.         fprintf(stderr, "\n***error: wrong number of arguments***\n");
  36.         fprintf(stderr, 
  37.             "\n%s -h -t sigma opt_thld < input > output\n", argv[0]);
  38.         exit(1);
  39.     }
  40.     cnt = 1;
  41.     while(argv[cnt][0]=='-')
  42.     {
  43.         switch(argv[cnt][1])
  44.         {
  45.             case 'h':
  46.                 hysteresis = 0;
  47.                 cnt++;
  48.                 break;
  49.             case 't':
  50.                 thinning = 0;
  51.                 cnt++;
  52.                 break;
  53.             default:
  54.                 fprintf(stderr, "%s: syntax error\n", argv[0]);
  55.                 fprintf(stderr, 
  56.                     "%s -h -t sigma opt_thld < input > output\n", argv[0]);
  57.                 exit(1);
  58.         }
  59.     }
  60.     sigma=atof(argv[cnt++]);
  61.     if(argc>cnt)
  62.         threshold=atoi(argv[cnt]);
  63.     else
  64.         threshold=0;
  65. /*
  66.  * now read in image
  67.  */
  68.     read(0, (char*)header, 10);
  69.     if(header[0] != UBYTE_HD)
  70.     {
  71.         fprintf(stderr, "%s: ERROR image not of unsigned char\n", argv[0]);
  72.         exit(1);
  73.     }
  74.     nx = header[1];
  75.     ny = header[2];
  76.     fprintf(stderr, "\nimage size is %d x %d\n", nx, ny);
  77.     
  78.     if((pic = (unsigned char *)malloc(nx*ny*sizeof(unsigned char)))==0)
  79.     {
  80.         fprintf(stderr, 
  81.             "%s: ERROR cannot allocate image array\n", argv[0]);
  82.         exit(1);
  83.     }
  84.  
  85.     for(i=0; i<ny; i++)
  86.     {
  87.         read(0, (char*)pic+i*nx, nx);
  88.     }
  89.     image_init(pic, nx, ny);
  90.  
  91.     edge_map = image_find_edges(sigma, &threshold);
  92.     if(hysteresis)
  93.     {
  94.         fprintf(stderr, "\napplying hysteresis\n");
  95.         edge_map = image_hysteresis(threshold/3);
  96.     }
  97.     if(thinning)
  98.     {
  99.         fprintf(stderr, "\nthinning image\n");
  100.         edge_map = image_thin();
  101.     }
  102.     write(1, (char*)header, 10);
  103.     write(1, (char*)edge_map, nx*ny);
  104.  
  105.     image_cleanup();
  106. }
  107.  
  108.