第1关:Roberts梯度算子
#include "BMP.h"
BMP_Image* Roberts(BMP_Image* Image_In)
{
BMP_Image* Roberts;
Roberts = (BMP_Image*)malloc(sizeof(BMP_Image));
Roberts->width = Image_In->width -1;
Roberts->height = Image_In->height -1;
Roberts->biBitCount = 8;
Roberts->imageRgbQuad = (BMP_RgbQuad*)malloc(sizeof(BMP_RgbQuad)*256);
Roberts->imageData = (unsigned char*)malloc((Image_In->height-1)*(Image_In->width-1));
int i;
for(i=0;i<256;i++)
{
Roberts->imageRgbQuad[i].rgbBlue = Image_In->imageRgbQuad[i].rgbBlue;
Roberts->imageRgbQuad[i].rgbGreen = Image_In->imageRgbQuad[i].rgbGreen;
Roberts->imageRgbQuad[i].rgbRed = Image_In->imageRgbQuad[i].rgbRed;
Roberts->imageRgbQuad[i].rgbReserved = 0;
Roberts->imageRgbQuad[i].rgbBlue = Image_In->imageRgbQuad[i].rgbBlue;
Roberts->imageRgbQuad[i].rgbGreen = Image_In->imageRgbQuad[i].rgbGreen;
Roberts->imageRgbQuad[i].rgbRed = Image_In->imageRgbQuad[i].rgbRed;
Roberts->imageRgbQuad[i].rgbReserved = 0;
Roberts->imageRgbQuad[i].rgbBlue = Image_In->imageRgbQuad[i].rgbBlue;
Roberts->imageRgbQuad[i].rgbGreen = Image_In->imageRgbQuad[i].rgbGreen;
Roberts->imageRgbQuad[i].rgbRed = Image_In->imageRgbQuad[i].rgbRed;
Roberts->imageRgbQuad[i].rgbReserved = 0;
}
int j,index1,index2,index3,k=0,tmp;
for (i=0; i<Image_In->height-1; i++)
{
for (j=0; j<Image_In->width-1; j++)
{
/********* Begin *********/
index1=i*Image_In->width+j;
index2=(i+1)*Image_In->width+j;
int d1 = Image_In->imageData[index2+1];
int d2 = Image_In->imageData[index1];
int d3 = Image_In->imageData[index2];
int d4 = Image_In->imageData[index1+1];
tmp = abs(Image_In->imageData[index2+1] - Image_In->imageData[index1]) + abs(Image_In->imageData[index2] - Image_In->imageData[index1+1]);
Roberts->imageData[k] = tmp;
k++;
/********* End *********/
}
}
return Roberts;
}
第2关:Sobel梯度算子
#include "BMP.h"
BMP_Image* Sobel(BMP_Image* Image_In)
{
BMP_Image* Image_Sobel;
Image_Sobel = (BMP_Image*)malloc(sizeof(BMP_Image));
Image_Sobel->width = Image_In->width -2;
Image_Sobel->height = Image_In->height -2;
Image_Sobel->biBitCount = 8;
Image_Sobel->imageRgbQuad = (BMP_RgbQuad*)malloc(sizeof(BMP_RgbQuad)*256);
Image_Sobel->imageData = (unsigned char*)malloc((Image_In->height-2)*(Image_In->width-2));
int i;
for(i=0;i<256;i++)
{
Image_Sobel->imageRgbQuad[i].rgbBlue = Image_In->imageRgbQuad[i].rgbBlue;
Image_Sobel->imageRgbQuad[i].rgbGreen = Image_In->imageRgbQuad[i].rgbGreen;
Image_Sobel->imageRgbQuad[i].rgbRed = Image_In->imageRgbQuad[i].rgbRed;
Image_Sobel->imageRgbQuad[i].rgbReserved = 0;
Image_Sobel->imageRgbQuad[i].rgbBlue = Image_In->imageRgbQuad[i].rgbBlue;
Image_Sobel->imageRgbQuad[i].rgbGreen = Image_In->imageRgbQuad[i].rgbGreen;
Image_Sobel->imageRgbQuad[i].rgbRed = Image_In->imageRgbQuad[i].rgbRed;
Image_Sobel->imageRgbQuad[i].rgbReserved = 0;
Image_Sobel->imageRgbQuad[i].rgbBlue = Image_In->imageRgbQuad[i].rgbBlue;
Image_Sobel->imageRgbQuad[i].rgbGreen = Image_In->imageRgbQuad[i].rgbGreen;
Image_Sobel->imageRgbQuad[i].rgbRed = Image_In->imageRgbQuad[i].rgbRed;
Image_Sobel->imageRgbQuad[i].rgbReserved = 0;
}
//printf("SobelÈñ»¯¿ªÊ¼\n");
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 tmp1 = abs(-Image_In->imageData[index1-1] - 2*Image_In->imageData[index1] - Image_In->imageData[index1+1] +
Image_In->imageData[index3-1] + 2*Image_In->imageData[index3] + Image_In->imageData[index3+1]);
int tmp2 = abs(-Image_In->imageData[index1-1] - 2*Image_In->imageData[index2-1] - Image_In->imageData[index2-1] +
Image_In->imageData[index1+1] + 2*Image_In->imageData[index2+1] + Image_In->imageData[index3+1]);
Image_Sobel->imageData[k] = tmp1+tmp2;
k++;
/********* End *********/
}
}
return Image_Sobel;
}
第3关:Prewitt梯度算子
#include "BMP.h"
BMP_Image* Prewitt(BMP_Image* Image_In)
{
BMP_Image* Image_Prewitt;
Image_Prewitt = (BMP_Image*)malloc(sizeof(BMP_Image));
Image_Prewitt->width = Image_In->width -2;
Image_Prewitt->height = Image_In->height -2;
Image_Prewitt->biBitCount = 8;
Image_Prewitt->imageRgbQuad = (BMP_RgbQuad*)malloc(sizeof(BMP_RgbQuad)*256);
Image_Prewitt->imageData = (unsigned char*)malloc((Image_In->height-2)*(Image_In->width-2));
int i;
for(i=0;i<256;i++)
{
Image_Prewitt->imageRgbQuad[i].rgbBlue = Image_In->imageRgbQuad[i].rgbBlue;
Image_Prewitt->imageRgbQuad[i].rgbGreen = Image_In->imageRgbQuad[i].rgbGreen;
Image_Prewitt->imageRgbQuad[i].rgbRed = Image_In->imageRgbQuad[i].rgbRed;
Image_Prewitt->imageRgbQuad[i].rgbReserved = 0;
Image_Prewitt->imageRgbQuad[i].rgbBlue = Image_In->imageRgbQuad[i].rgbBlue;
Image_Prewitt->imageRgbQuad[i].rgbGreen = Image_In->imageRgbQuad[i].rgbGreen;
Image_Prewitt->imageRgbQuad[i].rgbRed = Image_In->imageRgbQuad[i].rgbRed;
Image_Prewitt->imageRgbQuad[i].rgbReserved = 0;
Image_Prewitt->imageRgbQuad[i].rgbBlue = Image_In->imageRgbQuad[i].rgbBlue;
Image_Prewitt->imageRgbQuad[i].rgbGreen = Image_In->imageRgbQuad[i].rgbGreen;
Image_Prewitt->imageRgbQuad[i].rgbRed = Image_In->imageRgbQuad[i].rgbRed;
Image_Prewitt->imageRgbQuad[i].rgbReserved = 0;
}
//printf("PrewittÈñ»¯¿ªÊ¼\n");
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 tmp1 = abs(-Image_In->imageData[index1-1] - Image_In->imageData[index1] - Image_In->imageData[index1+1] +
Image_In->imageData[index3-1] + Image_In->imageData[index3] + Image_In->imageData[index3+1]);
int tmp2 = abs(-Image_In->imageData[index1-1] - Image_In->imageData[index2-1] - Image_In->imageData[index2-1] +
Image_In->imageData[index1+1] + Image_In->imageData[index2+1] + Image_In->imageData[index3+1]);
Image_Prewitt->imageData[k] = tmp1+tmp2;
k++;
/********* End *********/
}
}
return Image_Prewitt;
}
第4关:Laplacian梯度算子
#include "BMP.h"
BMP_Image* Laplacian(BMP_Image* Image_In)
{
BMP_Image* Image_Laplacian;
Image_Laplacian = (BMP_Image*)malloc(sizeof(BMP_Image));
Image_Laplacian->width = Image_In->width -2;
Image_Laplacian->height = Image_In->height -2;
Image_Laplacian->biBitCount = 8;
Image_Laplacian->imageRgbQuad = (BMP_RgbQuad*)malloc(sizeof(BMP_RgbQuad)*256);
Image_Laplacian->imageData = (unsigned char*)malloc((Image_In->height-2)*(Image_In->width-2));
int i;
for(i=0;i<256;i++)
{
Image_Laplacian->imageRgbQuad[i].rgbBlue = Image_In->imageRgbQuad[i].rgbBlue;
Image_Laplacian->imageRgbQuad[i].rgbGreen = Image_In->imageRgbQuad[i].rgbGreen;
Image_Laplacian->imageRgbQuad[i].rgbRed = Image_In->imageRgbQuad[i].rgbRed;
Image_Laplacian->imageRgbQuad[i].rgbReserved = 0;
Image_Laplacian->imageRgbQuad[i].rgbBlue = Image_In->imageRgbQuad[i].rgbBlue;
Image_Laplacian->imageRgbQuad[i].rgbGreen = Image_In->imageRgbQuad[i].rgbGreen;
Image_Laplacian->imageRgbQuad[i].rgbRed = Image_In->imageRgbQuad[i].rgbRed;
Image_Laplacian->imageRgbQuad[i].rgbReserved = 0;
Image_Laplacian->imageRgbQuad[i].rgbBlue = Image_In->imageRgbQuad[i].rgbBlue;
Image_Laplacian->imageRgbQuad[i].rgbGreen = Image_In->imageRgbQuad[i].rgbGreen;
Image_Laplacian->imageRgbQuad[i].rgbRed = Image_In->imageRgbQuad[i].rgbRed;
Image_Laplacian->imageRgbQuad[i].rgbReserved = 0;
}
//printf("LaplacianÈñ»¯¿ªÊ¼\n");
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] - Image_In->imageData[index2-1] + 4*Image_In->imageData[index2] - Image_In->imageData[index2+1] - Image_In->imageData[index3]);
if(tmp<0) tmp = 0;
else if(tmp>=255) tmp = 255;
Image_Laplacian->imageData[k] = tmp ;
k++;
/********* End *********/
}
}
return Image_Laplacian;
}