void CSDIELSView::OnMedianFilterImprove()
{
//程序编制:李立宗
//2012-8-10
if(myImage1.IsNull())
OnOpenResourceFile();
if(!myImage2.IsNull())
myImage2.Destroy();
if(myImage2.IsNull()){
myImage2.Create(myImage1.GetWidth(),myImage1.GetHeight(),24,0);
}
//COLORREF pixel;
int maxY = myImage1.GetHeight();
int maxX=myImage1.GetWidth();
byte* pRealData;
byte* pRealData2;
pRealData=(byte*)myImage1.GetBits();
pRealData2=(byte*)myImage2.GetBits();
int pit=myImage1.GetPitch();
int pit2=myImage2.GetPitch();
//需要注意,pit和pit2的值并不一样,所以如果使用一个值,会导致不同的结果出现
//CString str;
//str.Format(TEXT("%d"),pit);
//MessageBox(str);
//str.Format(TEXT("%d"),pit2);
//MessageBox(str);
int bitCount=myImage1.GetBPP()/8;
int bitCount2=myImage2.GetBPP()/8;
int tempR,tempG,tempB;
int temp,tempX,tempY;
//int M[3][3]={{1,2,1},{2,4,2},{1,2,1}};
int M[3][3]={{1,1,1},{1,2,1},{1,1,1}};
int sum=0;
int m;
int pixel[9];
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
sum=sum+M[i][j];
// tempR=tempG=tempG=0;
//说明:将生产的图像作为24位图处理。
for (int y=1; y<maxY-1; y++) {
for (int x=1; x<maxX-1; x++) {
m=0;
for(int i=y-1;i<=y+1;i++)
for(int j=x-1;j<=x+1;j++)
{
pixel[m++]= *(pRealData+pit*(i)+(j)*bitCount) ;
}
tempR=getValue(pixel);
if(bitCount==1)
{
tempG=tempR;
tempB=tempR;
}
else
{ m=0;
for(int i=y-1;i<=y+1;i++)
for(int j=x-1;j<=x+1;j++)
{
pixel[m++]= *(pRealData+pit*(i)+(j)*bitCount+1) ;
}
tempG=getValue(pixel);
tempG=pixel[8];
m=0;
for(int i=y-1;i<=y+1;i++)
for(int j=x-1;j<=x+1;j++)
{
pixel[m++]= *(pRealData+pit*(i)+(j)*bitCount+2) ;
}
tempB=getValue(pixel);
}
*(pRealData2+pit2*y+x*bitCount2)=tempR;
*(pRealData2+pit2*y+x*bitCount2+1)=tempG;
*(pRealData2+pit2*y+x*bitCount2+2)=tempB;
}
}
Invalidate();
}
定义的函数getValue如下:
int getValue(int a[9])
{
int i,j,temp;
int current;
current=a[4];
for(i=0;i<8;i++)
for(j=i+1;j<9;j++) /*注意循环的上下限*/
if(a[i]>a[j]) {
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
if(current==a[0]||current==a[8])
return a[4];
else
return current;
}