分类: C/C++
2012-01-27 01:31:05
1.彩色图像到灰度图的转换公式
Gray(i,j)=0.11 * R(i,j) + 0.59 * G(i,j) + 0.3 * B(i,j)
在OpenCV中采用的是下面公式
Gray = 0.212671 * R + 0.715160 * G + 0.072169 * B +0 *A
2.灰度到伪彩色的转换公式
f表示某一像素点的灰度
if 0<=f<63 then
begin
r :=0; g :=254-4*f; b :=255;
end;
if 64<=f<127 then
begin
r :=0; g :=4*f-254; b :=510-4*f;
end;
if 128<=f<191 then
begin
r :=4*f-510; g :=255; b :=0;
end;
if 192<=f<=255 then
begin
r :=255; g :=1022-4*f; b :=0;
end;
//灰度图像转成伪彩色实例:
procedure GrayToColor(Bmp: TBitmap);
var
i, j, uG: Integer;
P: PByteArray;
begin
Bmp.PixelFormat := pf24bit;
for j := 0 to Bmp.Height - 1 do
begin
P := Bmp.ScanLine[j];
for i := 0 to Bmp.Width - 1 do
begin
uG := P[3 * i];
if (0 <= uG) and (uG < 63) then //灰度------>伪彩色
begin
P[3 * i + 2] := 0;
P[3 * i + 1] := 254 - 4 * uG;
P[3 * i] := 255;
end;
if (64 <= uG) and (uG < 127) then
begin
P[3 * i + 2] := 0;
P[3 * i + 1] := 4 * uG - 254;
P[3 * i] := 510 - 4 * uG;
end;
if (128 <= uG) and (uG < 191) then
begin
P[3 * i + 2] := 4 * uG - 510;
P[3 * i + 1] := 255;
P[3 * i] := 0;
end;
if (192 <= uG) and (uG <= 255) then
begin
P[3 * i + 2] := 255;
P[3 * i + 1] := 1022 - 4 * uG;
P[3 * i] := 0;
end;
end;
end;
end;