如何把BMP的真彩位图变为灰阶图,在线等待。。。。(50分)

W

wjames

Unregistered / Unconfirmed
GUEST, unregistred user!
如何把BMP的真彩位图变为灰阶图,最好可以给出源码,分不够可以加,在线等待。。。。
 
不用自己写了,查找一下别人写的给你:)

话题1007292的标题是: 真彩图片转为灰度图片.
分类:图形图象 eastweast (2002-3-28 9:35:00)
如何才能够将一幅真彩色的图像转换为256的灰度图像?

卷起千堆雪tyn (2002-3-28 9:39:00)
var
p :pByteArray;
Gray,x,y :Integer;
Bmp :TBitmap;
begin
Bmp :=TBitmap.Create;
Bmp.Assign(Image.Picture.Bitmap);
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;
Image.Picture.Bitmap.Assign(Bmp);
Bmp.Free;
end;

 
[8D]PhotoShop应该可以转换

 
procedure Tmainform.togreyClick(Sender: TObject);
var
lo:tbitmap;
i,j:integer;
kl:longint;
rr,gg,bb:byte;
res:byte;
begin
lo:=tbitmap.create;
lo.Width:=image.Width;
lo.height:=image.height;
for i:=0 to image.Width-1 do
begin
for j:=0 to image.height-1 do
begin
kl:=ColorToRGB(image.Canvas.Pixels[i,j]);
rr:=byte(kl);
gg:=byte(kl shr 8);
bb:=byte(kl shr 8);
res:=(rr+gg+bb) div 3;
lo.Canvas.Pixels[i,j]:=rgb(res,res,res);
end;
end;
image.Canvas.Draw(0,0,lo);
lo.free;
end;
end;
 
卷起千堆雪tyn的方法速度快,chemer速度慢些,应该都可以,轻松虎的代码中最好加一句
bmp.pixelformat:=pf24bit;
 
我自己优化了一下,最快的代码!!全整数无浮点运算:)
var
p :pByteArray;
x,y,p3 :Integer;
Bmp :TBitmap;
begin
Bmp :=TBitmap.Create;
Bmp.Assign(Image.Picture.Bitmap);
bmp.pixelformat:=pf24bit; //这样保险:)
for y:=0 to Bmp.Height-1 do
begin
p:=Bmp.scanline[y];
for x:=0 to Bmp.Width-1 do
begin
p[0]:=(p[2]*2 + p[1] * 5 + p[0]) shr 3;
p[1]:=p[0];
p[2]:=p[2];
inc(p,3);
end;
end;
Image.Picture.Bitmap.Assign(Bmp);
Bmp.Free;
end;
 
多人接受答案了。
 
顶部