第1关:图像反色
#include "BMP.h"
BMP_Image* InvertColor(BMP_Image* Image_In)
{
BMP_Image* Image_InvertColor;
Image_InvertColor = (BMP_Image*)malloc(sizeof(BMP_Image));
int i;
memcpy(Image_InvertColor,Image_In,sizeof(BMP_Image));
//ÔÚÑ»·ÌåÄÚ¶ÔͼÏñÿһ¸öÏñËØ·´É«´¦Àí
for (i=0; i<Image_InvertColor->height*Image_InvertColor->width; i++)
{
/********** Begin *********/
Image_InvertColor->imageData[i] = 0xffff - Image_In->imageData[i];
/********** End *********/
//Íê³É»Ò¶Èͼ·´É«´¦Àí´úÂë±àд
}
return Image_InvertColor;
}
第2关:线性变换
#include "BMP.h"
//ͨ¹ýÔÚ´Ë´¦¸ü¸ÄÏßÐԱ任²ÎÊý¹Û²ìÔͼ´¦ÀíºóµÄЧ¹û£¬Ñ¡ÔñÒ»½ÏÓŲÎÊý
#define a 0.08*255
#define b 0.47*255
#define a1 0.3*255
#define b1 0.5*255
BMP_Image* LinearTrans(BMP_Image* Image_In)
{
BMP_Image* Image_LinerTrans;
Image_LinerTrans = (BMP_Image*)malloc(sizeof(BMP_Image));
int i;
memcpy(Image_LinerTrans,Image_In,sizeof(BMP_Image));
//ÔÚÑ»·ÌåÄÚ¶ÔͼÏñÿһ¸öÏñËØÏßÐԱ任´¦Àí
for (i=0; i<Image_LinerTrans->height*Image_LinerTrans->width; i++)
{
if(Image_In->imageData[i]<=a)
Image_LinerTrans->imageData[i] = 2;
else if(Image_In->imageData[i]>=b)
Image_LinerTrans->imageData[i] = 255;
else
/********** Begin *********/
Image_LinerTrans->imageData[i] = (int) (255*1.0/(b-a)*Image_In->imageData[i] - 255*a/(b-a));
/********** End *********/
//Íê³É»Ò¶ÈͼÏßÐԱ任´¦Àí´úÂë±àд£¬×¢Òâ·Ö¶Îº¯Êý±ß½çÌõ¼þµÄ´¦Àí¡£
}
return Image_LinerTrans;
}
第3关:直方图均衡化
#include "BMP.h"
BMP_Image* HistNormolize(BMP_Image* Image_In)
{
int NumPixel [256] = {0}; //ͳ¼ÆÔͼÏñ¸÷»Ò¶ÈÊýÄ¿£¬¹²256¸ö»Ò¶È¼¶
float ProbPixel [256] = {0}; //ͳ¼ÆÔͼÏñ¸÷»Ò¶È¸ÅÂÊ
float AccuProbPixel [256] = {0}; //ÀۼƻҶȸÅÂÊ
int AccuPixel [256] = {0}; //ÇóÕû
int HistNumPixel [256] = {0}; //ͳ¼ÆÖ±·½Í¼¾ùºâ»¯ºóͼÏñ¸÷»Ò¶ÈÊýÄ¿£¬¹²256¸ö»Ò¶È¼¶
BMP_Image* Image_HistNormolize;
Image_HistNormolize = (BMP_Image*)malloc(sizeof(BMP_Image));
memcpy(Image_HistNormolize,Image_In,sizeof(BMP_Image));
int size = Image_In->height * Image_In->width;
int i;
//½øÐÐÏñËػҶÈͳ¼Æ
for (i=0; i<size; i++)
{
/********** Begin *********/
NumPixel[Image_In->imageData[i]]++;//完成输入图像像素值统计代码
/********** End *********/
//Íê³ÉÊäÈëͼÏñÏñËØֵͳ¼Æ´úÂë
}
//¼ÆËã»Ò¶È·Ö²¼ÃܶÈ
for (i=0; i<256; i++)
{
/********** Begin *********/
ProbPixel[i] = (float)NumPixel[i]/(float)size;//完成输入图像像素值概率代码 ,注意整数型转换成浮点型
/********* End *********/
/********** End *********/
//Íê³ÉÊäÈëͼÏñÏñËØÖµ¸ÅÂÊ´úÂë £¬×¢ÒâÕûÊýÐÍת»»³É¸¡µãÐÍ
}
// ¼ÆËãÀÛ¼ÆÖ±·½Í¼·Ö²¼
AccuProbPixel[0] = ProbPixel[0];
for ( i = 1; i< 256; i++)
{
/********** Begin *********/
AccuProbPixel[i] = AccuProbPixel[i-1] + ProbPixel[i];//完成输入图像累积直方图计算
/********** End *********/
//Íê³ÉÊäÈëͼÏñÀÛ»ýÖ±·½Í¼¼ÆËã
}
//ÀۼƷֲ¼È¡Õû£¬±£´æ¼ÆËã³öÀ´µÄ»Ò¶ÈÓ³Éä¹Øϵ
for (i = 0; i< 256; i++)
{
/********** Begin *********/
AccuPixel[i] = (int)(AccuProbPixel[i]*255);//完成输入图像累积直方图取整代码编写,注意浮点型转换为整数型
/********** End *********/
//Íê³ÉÊäÈëͼÏñÀÛ»ýÖ±·½Í¼È¡Õû´úÂë±àд£¬×¢Ò⸡µãÐÍת»»ÎªÕûÊýÐÍ
}
//½øÐлҶÈÓ³Éä¾ùºâ»¯
for (i=0; i<size; i++)
{
/********** Begin *********/
Image_HistNormolize->imageData[i] = AccuPixel[Image_In->imageData[i]];//通过均衡化后对原图像像素值重新映射
/********** End *********/
//ͨ¹ý¾ùºâ»¯ºó¶ÔÔͼÏñÏñËØÖµÖØÐÂÓ³Éä
}
return Image_HistNormolize;
}