marrh.c
上传用户:zkm8103
上传日期:2011-09-16
资源大小:1k
文件大小:5k
源码类别:

图形图象

开发平台:

C/C++

  1. #include <stdio.h>                  /*  Marr-Hildreth.c  (or marrh.c) */
  2. #include <math.h>
  3. #define  PICSIZE 256
  4. #define  MAXMASK 100
  5.          int    pic[PICSIZE][PICSIZE];
  6.          double outpic1[PICSIZE][PICSIZE];
  7.          double outpic2[PICSIZE][PICSIZE];
  8.          int    edgeflag[PICSIZE][PICSIZE];
  9.          double mask[MAXMASK][MAXMASK];
  10.          double conv[PICSIZE][PICSIZE];
  11. main(argc,argv)
  12. int argc;
  13. char **argv;
  14. {
  15.         int     i,j,p,q,s,t,mr,centx,centy;
  16.         double  maskval,sum,sig,maxival,minival,maxval,ZEROTOL;
  17.         FILE    *fo1, *fo2,*fp1, *fopen();
  18.         char    *foobar;
  19.         argc--; argv++;
  20.         foobar = *argv;
  21.         fp1=fopen(foobar,"r");
  22.         argc--; argv++;
  23.         foobar = *argv;
  24.         fo1=fopen(foobar,"w");
  25.         argc--; argv++;
  26.         foobar = *argv;
  27.         fo2=fopen(foobar,"w");
  28.         argc--; argv++;
  29.         foobar = *argv;
  30.         sig = atof(foobar);
  31.         argc--; argv++;
  32.         foobar = *argv;
  33.         ZEROTOL = atof(foobar);
  34.         mr = (int)(sig * 3);
  35.         centx = (MAXMASK / 2);
  36.         centy = (MAXMASK / 2);
  37.         for (i=0;i<256;i++)
  38.         { for (j=0;j<256;j++)
  39.                 {
  40.                   pic[i][j]  =  getc (fp1);
  41.                   pic[i][j]  &= 0377;
  42.                 }
  43.         }
  44.         for (p=-mr;p<=mr;p++)
  45.         {  for (q=-mr;q<=mr;q++)
  46.            {
  47.               maskval = ((2-(((p*p)+(q*q))/(sig*sig)))*
  48.                       (exp(-1*(((p*p)+(q*q))/(2*(sig*sig))))));
  49.               (mask[p+centy][q+centx]) = maskval;
  50.            }
  51.         }
  52.         for (i=mr;i<=255-mr;i++)
  53.         { for (j=mr;j<=255-mr;j++)
  54.           {
  55.              sum = 0;
  56.              for (p=-mr;p<=mr;p++)
  57.              {
  58.                 for (q=-mr;q<=mr;q++)
  59.                 {
  60.                    sum += pic[i+p][j+q] * mask[p+centy][q+centx];
  61.                 }
  62.              }
  63.              outpic1[i][j] = sum;
  64.              conv[i][j] = sum;
  65.           }
  66.         }
  67.         maxval  = 0;
  68.         maxival = 0;
  69.         minival = 255;
  70.         for (i=mr;i<256-mr;i++)
  71.         { for (j=mr;j<256-mr;j++)
  72.           {
  73.              if (outpic1[i][j] > maxival)
  74.                 maxival = outpic1[i][j];
  75.              if (outpic1[i][j] < minival)
  76.                 minival = outpic1[i][j];
  77.            }
  78.         }
  79.         if (fabs(maxival) > fabs(minival))
  80.            maxval = fabs(maxival);
  81.         else
  82.            maxval = fabs(minival);
  83.         for (i=0;i<256;i++)
  84.         { for (j=0;j<256;j++)
  85.           {
  86.              outpic1[i][j] = ((((outpic1[i][j]) / maxval) + 1) * 127);
  87.              fprintf(fo1,"%c",(char)((int)(outpic1[i][j])));
  88.           }
  89.         }
  90.         for (i=mr;i<=255-mr;i++)
  91.         {  for (j=mr;j<=255-mr;j++)
  92.            {
  93.                          outpic2[i][j] = 0;
  94.              if (conv[i][j] > ZEROTOL)
  95.              {
  96.                for (p=-1;p<=1;p++)
  97.                {
  98.                  for (q=-1;q<=1;q++)
  99.                  {
  100.                    if (conv[i+p][j+q] < -ZEROTOL)
  101.                    {
  102.                      outpic2[i][j] = 255;
  103.                    }
  104.                  }
  105.                }
  106.              }
  107.              else if ((fabs)(conv[i][j]) < ZEROTOL)
  108.              {
  109.                      if (((conv[i+1][j] > ZEROTOL) &&
  110.                           (conv[i-1][j] < -ZEROTOL))   ||
  111.                          ((conv[i+1][j] < -ZEROTOL) &&
  112.                           (conv[i-1][j] > ZEROTOL)))
  113.                      {
  114.                        outpic2[i][j] = 255;
  115.                      }
  116.                      else if (((conv[i][j+1] > ZEROTOL) &&
  117.                                (conv[i][j-1] < -ZEROTOL))   ||
  118.                               ((conv[i][j+1] < -ZEROTOL) &&
  119.                                (conv[i][j-1] > ZEROTOL)))
  120.                      {
  121.                        outpic2[i][j] = 255;
  122.                      }
  123.                      else if (((conv[i+1][j+1] > ZEROTOL) &&
  124.                                (conv[i-1][j-1] < -ZEROTOL))   ||
  125.                               ((conv[i+1][j+1] < -ZEROTOL) &&
  126.                                (conv[i-1][j-1] > ZEROTOL)))
  127.                      {
  128.                        outpic2[i][j] = 255;
  129.                      }
  130.                      else if (((conv[i+1][j-1] > ZEROTOL) &&
  131.                                (conv[i-1][j+1] < -ZEROTOL))   ||
  132.                               ((conv[i+1][j-1] < -ZEROTOL) &&
  133.                                (conv[i-1][j+1] > ZEROTOL)))
  134.                      {
  135.                        outpic2[i][j] = 255;
  136.                      }
  137.              }
  138.            }
  139.         }
  140.         for (i=0;i<256;i++)
  141.         { for (j=0;j<256;j++)
  142.           {  if (outpic2[i][j] == 255) outpic2[i][j]=0;
  143.              else outpic2[i][j]=255;
  144.              fprintf(fo2,"%c",(char)((int)(outpic2[i][j])));
  145.           }
  146.         }
  147. }