procedure TForm1.nteEqualize(bmp:Tbitmap); var lTemp,i,j:longint; bMap:array[0..255] of byte;// 灰度映射表 lCount:array[0..255] of byte;// 灰度映射表 p: pbyteArray; lheight,lwidth:longint; begin lheight:=bmp.Height; lwidth:=bmp.Width; bmp.PixelFormat:=pf8bit; for i:=0 to 255 do begin lCount[i] := 0; end; for i:=0 to lHeight-1 do begin p:=bmp.ScanLine[i]; for j:=0 to lWidth-1 do inc(lcount[p[i]]); // 计数加1 end; // 计算灰度映射表 for i:=0 to 255 do begin lTemp := 0;// 初始为0 for j:=0 to i do begin lTemp := lTemp+lCount[j]; end; // 计算对应的新灰度值 bMap[i] := byte(Round(lTemp * 255 / lHeight / lWidth)); end; for i:=0 to bmp.Height-1 do begin p:=bmp.ScanLine[lHeight - 1 - i]; for j:=0 to bmp.Width-1 do p[j]:=bmap[p[j]]; end; end;
imageEn 控件上有直方图的東東
我翻译后没调试过,难免有错,要学会自己调试嘛,不要什么都要别人来帮你做!其实就一个地方改改就行了,找这个错误也要不了多少时间的!! 把lCount:array[0..255] of byte;改成:lCount:array[0..255] of integer;就可以了
直方图均衡子函数
************************************************************************/
BOOL WINAPI InteEqualize(LPSTR lpDIBBits, LONG lWidth, LONG lHeight)
{
// 指向源图像的指针
unsigned char* lpSrc;
// 临时变量
LONG lTemp;
// 循环变量
LONG i;
LONG j;
// 灰度映射表
BYTE bMap[256];
// 灰度映射表
LONG lCount[256];
// 图像每行的字节数
LONG lLineBytes;
// 计算图像每行的字节数
lLineBytes = WIDTHBYTES(lWidth * 8);
// 重置计数为0
for (i = 0; i < 256; i ++)
{
// 清零
lCount[i] = 0;
}
// 计算各个灰度值的计数
for (i = 0; i < lHeight; i ++)
{
for (j = 0; j < lWidth; j ++)
{
lpSrc = (unsigned char *)lpDIBBits + lLineBytes * i + j;
// 计数加1
lCount[*(lpSrc)]++;
}
}
// 计算灰度映射表
for (i = 0; i < 256; i++)
{
// 初始为0
lTemp = 0;
for (j = 0; j <= i ; j++)
{
lTemp += lCount[j];
}
// 计算对应的新灰度值
bMap[i] = (BYTE) (lTemp * 255 / lHeight / lWidth);
}
// 每行
for(i = 0; i < lHeight; i++)
{
// 每列
for(j = 0; j < lWidth; j++)
{
// 指向DIB第i行,第j个象素的指针
lpSrc = (unsigned char*)lpDIBBits + lLineBytes * (lHeight - 1 - i) + j;
// 计算新的灰度值
*lpSrc = bMap[*lpSrc];
}
}
// 返回
return TRUE;
}
var
lTemp,i,j:longint;
bMap:array[0..255] of byte;// 灰度映射表
lCount:array[0..255] of byte;// 灰度映射表
p: pbyteArray;
lheight,lwidth:longint;
begin
lheight:=bmp.Height;
lwidth:=bmp.Width;
bmp.PixelFormat:=pf8bit;
for i:=0 to 255 do
begin
lCount[i] := 0;
end; for i:=0 to lHeight-1 do
begin
p:=bmp.ScanLine[i];
for j:=0 to lWidth-1 do
inc(lcount[p[i]]); // 计数加1
end; // 计算灰度映射表
for i:=0 to 255 do
begin
lTemp := 0;// 初始为0
for j:=0 to i do
begin
lTemp := lTemp+lCount[j];
end;
// 计算对应的新灰度值
bMap[i] := byte(Round(lTemp * 255 / lHeight / lWidth));
end; for i:=0 to bmp.Height-1 do
begin
p:=bmp.ScanLine[lHeight - 1 - i];
for j:=0 to bmp.Width-1 do
p[j]:=bmap[p[j]];
end;
end;
把lCount:array[0..255] of byte;改成:lCount:array[0..255] of integer;就可以了