3*3中值滤波程序

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lyc_daniel/article/details/8000055


#include <stdlib.h>

#include <windows.h>

#include <stdio.h>

#include "RWbmp.h"

 

/**

** method to remove noise from thecorrupted image by median value

* @param corrupted input grayscale binaryarray with corrupted info

* @param smooth output data for smoothresult, the memory need to be allocated outside of the function

* @param width width of the input grayscaleimage

* @param height height of the inputgrayscale image

*/ 

void medianFilter (unsigned char*corrupted, unsigned char* smooth, int width, int height) 

{      

       memcpy( smooth, corrupted, width*height*sizeof(unsigned char) ); 

       for(int j=1;j<height-1;j++) 

       { 

              for(int i=1;i<width-1;i++) 

              { 

          int k = 0; 

          unsigned char window[9]; 

                 for (int jj = j - 1; jj < j + 2; ++jj)

                 {

                        for (int ii = i - 1; ii < i + 2; ++ii)

                        {

                                   window[k++]= corrupted[jj * width + ii];

                        }

 

                 }

                 //Order elements (only half of them)  

                 for (int m = 0; m < 5; ++m) 

                 { 

                     int min = m; 

                     for (int n = m + 1; n < 9; ++n)

                        {

                                   if(window[n] < window[min])  min = n;

                        }

                        //Put found minimum element in itsplace  

                        unsigned char temp = window[m]; 

                        window[m] = window[min]; 

                        window[min] = temp; 

                 } 

                 smooth[ j*width+i ] = window[4]; 

              } 

       }

}

 

int main()

{

       intwidth,height;

       RdWtBmpmyRdWtBmp;

       BYTE*pImg=myRdWtBmp.Read8bitbmp("lftImArSptImage.bmp",&width,&height);

 

       BYTE*pMedImg=new BYTE[width*height];

       medianFilter(pImg,pMedImg,width,height);

 

       boolsuccess=myRdWtBmp.Writebitmapbmp(pMedImg,width,height,"pMedImg.bmp");

       if(success==true)

       {

              printf("中值滤波图像写成功!\n");

       }

 

       deletepMedImg;

       deletepImg;

      

       system("pause");

}


阅读更多
换一批

没有更多推荐了,返回首页