第1关:邻域平均法
#include "BMP.h"
BMP_Image* MeanFilter(BMP_Image* Image_In)
{
BMP_Image* Image_MeanFilter;
Image_MeanFilter = (BMP_Image*)malloc(sizeof(BMP_Image));
Image_MeanFilter->width = Image_In->width -2;
Image_MeanFilter->height = Image_In->height -2;
Image_MeanFilter->biBitCount = 8;
Image_MeanFilter->imageRgbQuad = (BMP_RgbQuad*)malloc(sizeof(BMP_RgbQuad)*256);
Image_MeanFilter->imageData = (unsigned char*)malloc((Image_In->height-2)*(Image_In->width-2));
int i;
for(i=0;i<256;i++)
{
Image_MeanFilter->imageRgbQuad[i].rgbBlue = Image_In->imageRgbQuad[i].rgbBlue;
Image_MeanFilter->imageRgbQuad[i].rgbGreen = Image_In->imageRgbQuad[i].rgbGreen;
Image_MeanFilter->imageRgbQuad[i].rgbRed = Image_In->imageRgbQuad[i].rgbRed;
Image_MeanFilter->imageRgbQuad[i].rgbReserved = 0;
Image_MeanFilter->imageRgbQuad[i].rgbBlue = Image_In->imageRgbQuad[i].rgbBlue;
Image_MeanFilter->imageRgbQuad[i].rgbGreen = Image_In->imageRgbQuad[i].rgbGreen;
Image_MeanFilter->imageRgbQuad[i].rgbRed = Image_In->imageRgbQuad[i].rgbRed;
Image_MeanFilter->imageRgbQuad[i].rgbReserved = 0;
Image_MeanFilter->imageRgbQuad[i].rgbBlue = Image_In->imageRgbQuad[i].rgbBlue;
Image_MeanFilter->imageRgbQuad[i].rgbGreen = Image_In->imageRgbQuad[i].rgbGreen;
Image_MeanFilter->imageRgbQuad[i].rgbRed = Image_In->imageRgbQuad[i].rgbRed;
Image_MeanFilter->imageRgbQuad[i].rgbReserved = 0;
}
//Â˲¨²Ù×÷ʱ²»¿¼ÂDZßÔµ£¬3*3¾ùÖµÂ˲¨µÄ»°»áʹͼÏñ¼õÉÙ2ÐÐ2ÁÐ
//ÔÚÂ˲¨Ê±Ó¦´ÓµÚ¶þÐеڶþÁпªÊ¼µ½µ¹ÊýµÚ¶þÐеڶþÁнáÊø
int j,index1,index2,index3,k=0,tmp;
for (i=1; i<Image_In->height-1; i++)
{
for (j=1; j<Image_In->width-1; j++)
{
/********* Begin *********/
index1 = (i-1)*Image_In->width+j;
index2 = i*Image_In->width+j;
index3 = (i+1)*Image_In->width+j;
tmp = (Image_In->imageData[index1-1] + Image_In->imageData[index1] + Image_In->imageData[index1+1] + Image_In->imageData[index2-1] + Image_In->imageData[index2] + Image_In->imageData[index2+1] +
Image_In->imageData[index3-1] + Image_In->imageData[index3] + Image_In->imageData[index3+1])/9;
Image_MeanFilter->imageData[k] = tmp;
k++;
/********* End *********/
}
}
return Image_MeanFilter;
}
第2关:中值滤波法(3*3)
#include "BMP.h"
int Med(int a,int b,int c)
{
int t;
if(a > b) {t = a; a=b; b=t;}
if(a > c) {t = a; a=c; c=t;}
if(b > c) {t = b; b=c; c=t;}
return b;
}
BMP_Image* MedFilter(BMP_Image* Image_In)
{
BMP_Image* Image_MedFilter;
Image_MedFilter = (BMP_Image*)malloc(sizeof(BMP_Image));
Image_MedFilter->width = Image_In->width -2;
Image_MedFilter->height = Image_In->height -2;
Image_MedFilter->biBitCount = 8;
Image_MedFilter->imageRgbQuad = (BMP_RgbQuad*)malloc(sizeof(BMP_RgbQuad)*256);
Image_MedFilter->imageData = (unsigned char*)malloc((Image_In->height-2)*(Image_In->width-2));
int i;
for(i=0;i<256;i++)
{
Image_MedFilter->imageRgbQuad[i].rgbBlue = Image_In->imageRgbQuad[i].rgbBlue;
Image_MedFilter->imageRgbQuad[i].rgbGreen = Image_In->imageRgbQuad[i].rgbGreen;
Image_MedFilter->imageRgbQuad[i].rgbRed = Image_In->imageRgbQuad[i].rgbRed;
Image_MedFilter->imageRgbQuad[i].rgbReserved = 0;
Image_MedFilter->imageRgbQuad[i].rgbBlue = Image_In->imageRgbQuad[i].rgbBlue;
Image_MedFilter->imageRgbQuad[i].rgbGreen = Image_In->imageRgbQuad[i].rgbGreen;
Image_MedFilter->imageRgbQuad[i].rgbRed = Image_In->imageRgbQuad[i].rgbRed;
Image_MedFilter->imageRgbQuad[i].rgbReserved = 0;
Image_MedFilter->imageRgbQuad[i].rgbBlue = Image_In->imageRgbQuad[i].rgbBlue;
Image_MedFilter->imageRgbQuad[i].rgbGreen = Image_In->imageRgbQuad[i].rgbGreen;
Image_MedFilter->imageRgbQuad[i].rgbRed = Image_In->imageRgbQuad[i].rgbRed;
Image_MedFilter->imageRgbQuad[i].rgbReserved = 0;
}
//Â˲¨²Ù×÷ʱ²»¿¼ÂDZßÔµ£¬3*3ÖÐÖµÂ˲¨µÄ»°»áʹͼÏñ¼õÉÙ2ÐÐ2ÁÐ
//ÔÚÂ˲¨Ê±Ó¦´ÓµÚ¶þÐеڶþÁпªÊ¼µ½µ¹ÊýµÚ¶þÐеڶþÁнáÊø
int j,index1,index2,index3,k=0;
for (i=1; i<Image_In->height-1; i++)
{
for (j=1; j<Image_In->width-1; j++)
{
/********* Begin *********/
index1=(i-1)*Image_In->width+j;
index2=i*Image_In->width+j;
index3=(i+1)*Image_In->width+j;
int Med1 = Med(Image_In->imageData[index1-1],Image_In->imageData[index1],Image_In->imageData[index1+1]);
int Med2 = Med(Image_In->imageData[index2-1],Image_In->imageData[index2],Image_In->imageData[index2+1]);
int Med3 = Med(Image_In->imageData[index3-1],Image_In->imageData[index3],Image_In->imageData[index3+1]);
int Med4 = Med(Med1,Med2,Med3);
Image_MedFilter->imageData[k] = Med4;
k++;
/********* End *********/
}
}
return Image_MedFilter;
}
第3关:中值滤波法(5*5)
#include "BMP.h"
//Èý¸öÊýÇóÖÐÖµ
int ForMed(int a,int b,int c)
{
int t;
if(a > b) {t = a; a=b; b=t;}
if(a > c) {t = a; a=c; c=t;}
if(b > c) {t = b; b=c; c=t;}
return b;
}
BMP_Image* MedFilter1(BMP_Image* Image_In)
{
BMP_Image* Image_MedFilter1;
Image_MedFilter1 = (BMP_Image*)malloc(sizeof(BMP_Image));
Image_MedFilter1->width = Image_In->width -4;
Image_MedFilter1->height = Image_In->height -4;
Image_MedFilter1->biBitCount = 8;
Image_MedFilter1->imageRgbQuad = (BMP_RgbQuad*)malloc(sizeof(BMP_RgbQuad)*256);
Image_MedFilter1->imageData = (unsigned char*)malloc((Image_In->height-4)*(Image_In->width-4));
int i;
for(i=0;i<256;i++)
{
Image_MedFilter1->imageRgbQuad[i].rgbBlue = Image_In->imageRgbQuad[i].rgbBlue;
Image_MedFilter1->imageRgbQuad[i].rgbGreen = Image_In->imageRgbQuad[i].rgbGreen;
Image_MedFilter1->imageRgbQuad[i].rgbRed = Image_In->imageRgbQuad[i].rgbRed;
Image_MedFilter1->imageRgbQuad[i].rgbReserved = 0;
Image_MedFilter1->imageRgbQuad[i].rgbBlue = Image_In->imageRgbQuad[i].rgbBlue;
Image_MedFilter1->imageRgbQuad[i].rgbGreen = Image_In->imageRgbQuad[i].rgbGreen;
Image_MedFilter1->imageRgbQuad[i].rgbRed = Image_In->imageRgbQuad[i].rgbRed;
Image_MedFilter1->imageRgbQuad[i].rgbReserved = 0;
Image_MedFilter1->imageRgbQuad[i].rgbBlue = Image_In->imageRgbQuad[i].rgbBlue;
Image_MedFilter1->imageRgbQuad[i].rgbGreen = Image_In->imageRgbQuad[i].rgbGreen;
Image_MedFilter1->imageRgbQuad[i].rgbRed = Image_In->imageRgbQuad[i].rgbRed;
Image_MedFilter1->imageRgbQuad[i].rgbReserved = 0;
}
int j,index1,index2,index3,index4,index5,k=0;
//Â˲¨²Ù×÷ʱ²»¿¼ÂDZßÔµ£¬5*5ÖÐÖµÂ˲¨µÄ»°»áʹͼÏñ¼õÉÙ4ÐÐ4ÁÐ
//ÔÚÂ˲¨Ê±Ó¦´ÓµÚÈýÐеÚÈýÁпªÊ¼µ½µ¹ÊýµÚÈýÐеÚÈýÁнáÊø
for (i=2; i<Image_In->height-2; i++)
{
for (j=2; j<Image_In->width-2; j++)
{
/********* Begin *********/
index1=(i-2)*Image_In->width+j;
index2=(i-1)*Image_In->width+j;
index3=i*Image_In->width+j;
index4=(i+1)*Image_In->width+j;
index5=(i+2)*Image_In->width+j;
int Med1 = ForMed(Image_In->imageData[index1-1],Image_In->imageData[index1],Image_In->imageData[index1+1]);
int Med2 = ForMed(Image_In->imageData[index2-1],Image_In->imageData[index2],Image_In->imageData[index2+1]);
int Med3 = ForMed(Image_In->imageData[index3-1],Image_In->imageData[index3],Image_In->imageData[index3+1]);
int Med4 = ForMed(Image_In->imageData[index4-1],Image_In->imageData[index4],Image_In->imageData[index4+1]);
int Med5 = ForMed(Image_In->imageData[index5-1],Image_In->imageData[index5],Image_In->imageData[index5+1]);
int Med6= ForMed(Med1,Med2,Med3);
int Med7= ForMed(Med4,Med5,Med6);
Image_MedFilter1->imageData[k] = Med7;
k++;
/********* End *********/
}
}
return Image_MedFilter1;
}