Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1228502
  • 博文数量: 788
  • 博客积分: 4000
  • 博客等级: 上校
  • 技术积分: 7005
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-19 15:52
文章存档

2017年(81)

2011年(1)

2009年(369)

2008年(337)

分类:

2008-08-31 09:52:09



如何将一张彩色图片转换成黑白图片

 
  procedure   TForm1.Button3Click(Sender:   TObject);  
  var  
      P:   PByteArray;  
      x,   y:   Integer;  
      Gray:   Integer;  
      Bmp:   TBitmap;  
  begin  
      Bmp   :=   TBitmap.Create;  
      Bmp.Assign(Image1.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  
              Gray   :=   (P[3*x+2]   +   P[3*x+1]   +   P[3*x])   div   3;  
              P[3*x+2]   :=   Byte(Gray);  
              P[3*x+1]   :=   Byte(Gray);  
              P[3*x]   :=   Byte(Gray);  
          end;  
      end;  
      Canvas.Draw(0,   0,   Bmp);  
      Bmp.Free;  
  end;  
 

或者:  
   
  procedure   TForm1.Button3Click(Sender:   TObject);  
  var  
      P:   PByteArray;  
      x,   y:   Integer;  
      Gray:   Integer;  
      Bmp:   TBitmap;  
  begin  
      Bmp   :=   TBitmap.Create;  
      Bmp.Assign(Image1.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  
              Gray   :=   Max(P[3*x+2],   P[3*x+1]);  
              Gray   :=   Max(Gray,   P[3*x]);  
              P[3*x+2]   :=   Byte(Gray);  
              P[3*x+1]   :=   Byte(Gray);  
              P[3*x]   :=   Byte(Gray);  
          end;  
      end;  
      Canvas.Draw(0,   0,   Bmp);  
      Bmp.Free;  
  end;  
 

或者:  
   
  procedure   TForm1.Button3Click(Sender:   TObject);  
  var  
      P:   PByteArray;  
      x,   y:   Integer;  
      Gray:   Integer;  
      Bmp:   TBitmap;  
  begin  
      Bmp   :=   TBitmap.Create;  
      Bmp.Assign(Image1.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  
              Gray   :=   Round(P[3*x+2]   *   0.3   +   P[3*x+1]   *   0.59   +   P[3*x]   *   0.11);  
              P[3*x+2]   :=   Byte(Gray);  
              P[3*x+1]   :=   Byte(Gray);  
              P[3*x]   :=   Byte(Gray);  
          end;  
      end;  
      Canvas.Draw(0,   0,   Bmp);  
      Bmp.Free;  
  end;  
 

mark  
   
  学习



[新闻]Google推出Android Market挑战App Store
阅读(966) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~