灰度处理的方法主要有如下3种:
1、最大值法:使R、G、B的值等于3值中最大的一个,即:
R=G=B=max(R,G,B)
最大值法会使形成高亮度很高的灰度图象
- var
- bitmap:tbitmap;
- i,j:integer;
- a,b,c,crgb,temp:longint;
- res:byte;
- begin
- bitmap:=tbitmap.Create;
- bitmap.Width:=image1.Width+1;
- bitmap.Height:=image1.Height+1;
- for i:=0 to image1.Width+1 do
- begin
- for j:=0 to image1.Height+1 do
- begin
- crgb:=colortorgb(image1.Canvas.Pixels[i,j]);
- a:=crgb;
- b:=crgb shr 8;
- c:=crgb shr 8;
-
- if a>b then
- temp:=a
- else temp:=b;
- if c>temp then
- temp:=c ;
- res:=byte(temp);
- bitmap.Canvas.Pixels[i,j]:=rgb(res,res,res);
- end;
- end;
- image1.Canvas.Draw(0,0,bitmap);
- bitmap.Free;
- end;
2、平均值方法:使R、G、B的值求出平均值,即:
R=G=B=(R+G+B)3
平均值法会形成较柔和的灰度图象。
- var
- bitmap:tbitmap;
- i,j:integer;
- crgb:longint;
- rr,gg,bb:byte;
- res:byte;
- begin
- bitmap:=tbitmap.Create;
- bitmap.Width:=image1.Width;
- bitmap.Height:=image1.Height;
- for i:=0 to image1.Width+1 do
- begin
- for j:=0 to image1.Height+1 do
- begin
- crgb:=colortorgb(image1.Canvas.Pixels[i,j]);
- rr:=byte(crgb);
- gg:=byte(crgb shr 8);
- bb:=byte(crgb shr 8);
- res:=(rr+gg+bb)div 3;
- bitmap.Canvas.Pixels[i,j]:=rgb(res,res,res);
- end;
- end;
- image1.Canvas.Draw(0,0,bitmap);
- bitmap.Free;
- end;
3、加权平均值法:根据重要性或其他指标给R、G、B赋予不同的权值,并使R、G、B它们的值加权平均,即:
R=G=B=(WrR+WrG+WbB)3,经实际经验和理论推导证明,采用R=G=B=0.30*R+0.59*G+0.11*B,可以得到最合理的灰度图象
- var
- bitmap:tbitmap;
- i,j:integer;
- rr,gg,bb,res:byte;
- crgb:longint;
- begin
- bitmap:=tbitmap.Create;
- bitmap.Width:=image1.Width+1;
- bitmap.Height:=image1.Height+1;
- for i:=0 to image1.Width+1 do
- begin
- for j:=0 to image1.Height+1 do
- begin
- crgb:=colortorgb(image1.Canvas.Pixels[i,j]);
- rr:=byte(crgb);
- gg:=byte(crgb shr 8);
- bb:=byte(crgb shr 8);
- res:=(30*rr+59*gg+11*bb)div 100;
- bitmap.Canvas.Pixels[i,j]:=rgb(res,res,res);
- end;
- end;
- image1.Canvas.Draw(0,0,bitmap);
- bitmap.Free;
- end;
http://blog.csdn.net/aroc_lo/article/details/9256077
delphi实现图象灰度处理的3种方法
原文:http://www.cnblogs.com/findumars/p/5400233.html