您现在的位置:首页 >> 图形媒体 >> 图形媒体 >> 内容

Delphi实现图象灰度处理的3种方法

时间:2011/9/3 15:01:59 点击:

  核心提示:1、最大值法:使R、G、B的值等于3值中最大的一个,即:R=G=B=max(R,G,B)最大值法会使形成高亮度很高的灰度图象varbitmap:tbitmap;i,j:integer;a,b,c,cr...
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;
      //求出3者之间的最大值
      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;

作者:网络 来源:转载
共有评论 0相关评论
发表我的评论
  • 大名:
  • 内容:
本类推荐
  • 没有
本类固顶
  • 没有
  • 盒子文章(www.2ccc.com) © 2024 版权所有 All Rights Reserved.
  • 沪ICP备05001939号