3*3中值滤波程序


#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");

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东山一角

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值