• 中值滤波和均值滤波C++代码


    均值滤波和中值滤波代码  

    2008-11-24 16:07:36|  分类: 编程|举报|字号 订阅

     
     

    //------------------均值滤波器

    bool FilterAV(unsigned char *image,int height,int width)

    {

     int i,j;

     unsigned char *p=(unsigned char*)malloc(height*width);

     for(i=1;i<height-1;i++)

     {

      for(j=1;j<width-1;j++)

      {

       p[i*width+j]=(unsigned char)(((int)image[(i-1)*width+j-1]

          +(int)image[(i-1)*width+j]

          +(int)image[(i-1)*width+j+1]

          +(int)image[i*width+j-1]

          +(int)image[i*width+j]

          +(int)image[i*width+j+1]

          +(int)image[(i+1)*width+j-1]

          +(int)image[(i+1)*width+j]

          +(int)image[(i+1)*width+j+1])/9);

      }

     }

     for(i=1;i<height-1;i++)

     {

      for(j=1;j<width-1;j++)

      {

       image[i*width+j]=p[i*width+j];

      }

     }

     free(p);

     return true;

    }

    //----------------------------中值滤波器

    bool FilterMid(unsigned char *image,int height,int width)

    {

     int i,j,k,l;

     int pos;

     unsigned char temp;

     unsigned char psr[9];

     unsigned char *p=(unsigned char*)malloc(height*width);

      for(i=1;i<height-1;i++)

         {

      for(j=1;j<width-1;j++)

      {     //---3*3窗口矩阵

                  psr[0]=image[(i-1)*width+j-1];

                  psr[1]=image[(i-1)*width+j];

                  psr[2]=image[(i-1)*width+j+1];

                  psr[3]=image[i*width+j-1];

                  psr[4]=image[i*width+j];

                  psr[5]=image[i*width+j+1];

                  psr[6]=image[(i+1)*width+j-1];

                  psr[7]=image[(i+1)*width+j];

                  psr[8]=image[(i+1)*width+j+1];

                  //--------选择排序

                  for(k=0;k<9;k++)

                  {

                        pos=k;

                        for(l=k;l<9;l++)

                        {

                            if(psr[l]<psr[pos])

                                    pos=l;

                        }

                        temp=psr[k];

                        psr[k]=psr[pos];

                        psr[pos]=temp;

                  } 

                  //------取中值

                  p[i*width+j]=psr[4];

      }

     }

      for(i=1;i<height-1;i++)

         {

      for(j=1;j<width-1;j++)

      {

       image[i*width+j]=p[i*width+j];

      }

      }

      free(p);

      return true;

    }

  • 相关阅读:
    hdu 4825 Xor Sum (01 Trie)
    hdu 5877 Weak Pair (Treap)
    bzoj 1861: [Zjoi2006]Book 书架 (splay)
    bzoj 1503: [NOI2004]郁闷的出纳员 (splay)
    hihocoder#1333 : 平衡树·Splay2 (区间操作)
    「BZOJ1251」序列终结者 (splay 区间操作)
    二进制运算符的相关运算
    Bzoj 1085: [SCOI2005]骑士精神 (dfs)
    Bzoj 1083: [SCOI2005]繁忙的都市 (最小生成树)
    Bzoj 1088: [SCOI2005]扫雷Mine (DP)
  • 原文地址:https://www.cnblogs.com/ITcode/p/3968568.html
Copyright © 2020-2023  润新知