如何获得图象的灰度值(100分)

  • 主题发起人 主题发起人 jywyt
  • 开始时间 开始时间
J

jywyt

Unregistered / Unconfirmed
GUEST, unregistred user!
我想获取一张图象上任一点的灰度值,要通过什么方法呢?
 
procedure ConvertToGrayGraphic(var Bmp: TBitmap);
var
p :PByteArray;
Gray,x,y :Integer;
begin
for y:=0 to Bmp.Height-1 do
begin
p:=Bmp.scanline[y];
for x:=0 to Bmp.Width-1 do
begin
Gray:=Round(p[x*3+2]*0.3+p[x*3+1]*0.59+p[x*3]*0.11);
p[x*3]:=Gray;
p[x*3+1]:=Gray;
p[x*3+2]:=Gray;
end;
end;
end;
明白了么?这可是整副图象的灰度化。
至于任意一点,哈哈...
MyColor :=Image.Picture.Bitmap.Canvas.Pixels[x,y];
r :=GetRValue(MyColor);
g :=GetGValue(MyColor);
b :=GetBValue(MyColor);
灰度 :=0.3*r+0.59*g+0.11*b;
 
下面的代码,自己慢慢看吧:

procedure TfrmColor.Button1Click(Sender: TObject);
var
i,j,k : integer;
Source : DWORD;
begin
for i := 0 to min(328 ,Image1.Picture.Width) do
for j :=0 to min(328 ,Image1.Picture.Height) do
begin
// k := GetRValue(ColorToRGB(Image1.Canvas.Pixels[i,j]))+GetGValue(ColorToRGB(Image1.Canvas.Pixels[i,j]))+GetBValue(ColorToRGB(Image1.Canvas.Pixels[i,j]));
// k := k div 3;
Source := ColorToRGB(Image1.Canvas.Pixels[i,j]);
k := Round((0.30 * GetRValue(Source)) +
(0.59 * GetGValue(Source)) +
(0.11 * GetBValue(Source)));


Canvas.Pixels[i+320,j] := RGB(k,k,k);
end;
end;

procedure TfrmColor.Button2Click(Sender: TObject);
begin
close;
end;

procedure TfrmColor.Button3Click(Sender: TObject);
var
i,j,k, nRed, nGreen, nBlue: integer;
begin
for i := 0 to min(328 ,Image1.Picture.Width) do
for j :=0 to min(328 ,Image1.Picture.Height) do
begin
nRed := GetRValue(ColorToRGB(Image1.Canvas.Pixels[i,j]));
// nGreen := GetGValue(ColorToRGB(Image1.Canvas.Pixels[i,j]));
nBlue := GetBValue(ColorToRGB(Image1.Canvas.Pixels[i,j]));
Canvas.Pixels[i+320,j] := RGB(nRed,0,nBlue);
end;
end;

procedure TfrmColor.Button4Click(Sender: TObject);
var
i,j,k, nRed, nGreen, nBlue: integer;
begin
for i := 0 to min(328 ,Image1.Picture.Width) do
for j :=0 to min(328 ,Image1.Picture.Height) do
begin
// nRed := GetRValue(ColorToRGB(Image1.Canvas.Pixels[i,j]));
nGreen := GetGValue(ColorToRGB(Image1.Canvas.Pixels[i,j]));
nBlue := GetBValue(ColorToRGB(Image1.Canvas.Pixels[i,j]));
Canvas.Pixels[i+320,j] := RGB(0,nGreen,nBlue);
end;

end;

procedure TfrmColor.Button5Click(Sender: TObject);
var
i,j,k, nRed, nGreen, nBlue: integer;
begin
for i := 0 to min(328 ,Image1.Picture.Width) do
for j :=0 to min(328 ,Image1.Picture.Height) do
begin
nRed := GetRValue(ColorToRGB(Image1.Canvas.Pixels[i,j]));
nGreen := GetGValue(ColorToRGB(Image1.Canvas.Pixels[i,j]));
nBlue := GetBValue(ColorToRGB(Image1.Canvas.Pixels[i,j]));
Canvas.Pixels[i+320,j] := RGB(255-nRed,255-nGreen,255-nBlue);
end;

end;

procedure TfrmColor.Button6Click(Sender: TObject);
var
i,j,k, nRed, nGreen, nBlue: integer;
begin
for i := 0 to min(328 ,Image1.Picture.Width) do
for j :=0 to min(328 ,Image1.Picture.Height) do
begin
nRed := GetRValue(ColorToRGB(Image1.Canvas.Pixels[i,j]));
nGreen := GetGValue(ColorToRGB(Image1.Canvas.Pixels[i,j]));
// nBlue := GetBValue(ColorToRGB(Image1.Canvas.Pixels[i,j]));
Canvas.Pixels[i+320,j] := RGB(nRed,nGreen,0);
end;

end;

procedure TfrmColor.Button7Click(Sender: TObject);
var
i,j,k, nRed, nGreen, nBlue: integer;
begin
for i := 0 to min(328 ,Image1.Picture.Width) do
for j :=0 to min(328 ,Image1.Picture.Height) do
begin
nRed := GetRValue(ColorToRGB(Image1.Canvas.Pixels[i,j]));
nGreen := GetGValue(ColorToRGB(Image1.Canvas.Pixels[i,j]));
nBlue := GetBValue(ColorToRGB(Image1.Canvas.Pixels[i,j]));
Canvas.Pixels[i+320,j] := RGB(nRed,nBlue, nGreen);
end;

end;
 
卷起千堆雪tyn的好,速度也比较快!
 
多人接受答案了。
 
后退
顶部