灰度增强是一种在空域进行的图像增强方法。它是指将一定范围的像素(l,h)线性映射到一个新的范围(nl,nh)的操作。如果原来某个位置(x,y)的像素p(x,y)在(l,h)的范围内。那么,变换后的像素
f(p(x,y))=(p(x,y)-l)*(nh-nl)/(h-l);
使用gdi+对应的处理代码如下:
void EnhanceImage_Linear(Bitmap *bmp, int oldLow,int oldHigh, int newLow,int newHigh)
{
ASSERT(bmp!=NULL);
ASSERT(oldLow>=0
&&oldLow<=255);
ASSERT(oldHigh>=0
&&oldHigh<=255);
ASSERT(newLow>=0
&&newLow<=255);
ASSERT(newHigh>=0
&&newHigh<=255);
ASSERT(oldLow<oldHigh);
ASSERT(newLow<newHigh);
long width=bmp->GetWidth();
long height=bmp->GetHeight();
BitmapData bmpData;
bmp->LockBits(&Rect(0,0,width,height), ImageLockModeRead|ImageLockModeWrite,PixelFormat24bppRGB ,&bmpData);
unsigned char *pStart=(unsigned char *)(bmpData.Scan0);
REAL oldDiff=oldHigh-oldLow;
REAL newDiff=newHigh-newLow;
REAL ratio=newDiff/oldDiff;
for(int j=0;j<height;j++)
{
for(int i=0;i<width;i++)
{
pStart+=j*bmpData.Stride+3*i;
for(int k=0;k<3;k++)
{
if(pStart[k]>=oldLow
&&pStart[k]<=oldHigh)
{
int data=((pStart[k]-oldLow)*ratio);
if(data>255)
{
data=255;
}
pStart[k]=(unsigned char)data;
}
}
}
}
bmp->UnlockBits(&bmpData);
}
文章来源:
http://www.cppblog.com/dingding/archive/2008/12/14/69400.html