打印本文 打印本文 关闭窗口 关闭窗口
图像的处理(一)----灰度图像像素颜色亮度处理
作者:武汉SEO闵涛  文章来源:敏韬网  点击数1647  更新时间:2009/4/23 18:34:59  文章录入:mintao  责任编辑:mintao

    以前看了一些有关图像处理的书,对我起到了很大的帮助。所以,今天我就将我学过的知识整理出来,一方面可以给人学习,另一方面也可以请各位高手指点指点。

我要说的图像处理是针对程序方面的。所以,先做一个程序来放置图形。在这里,我使用了Delphi作为工具。因为,在我使用过的众多编译器当中,Delphi对图形的支持最好。还有,这里我并不是讲语法。所以,有些代码我就不详细说明。不便之处,敬请原谅。

注意:本文章的示例程序所用的东西不超过GDI的范围。

 

在图像处理中,速度是很重要的。因此,我们得重新处理一下TBitmap,得到TVczhBitmap。这只是因为GetPixels和SetPixels的速度太慢,换一个方法而已。

unit untBitmapProc;

 

interface

 

uses Graphics, SysUtils;

 

type

 

  TVczhBitmap=class(TBitmap)

  private

    Data:PByteArray;

    Line:Integer;

    procedure SetFormat;

    function GetBytePointer(X,Y:Integer):PByte;

    procedure SetBytes(X,Y:Integer;Value:Byte);

    function GetBytes(X,Y:Integer):Byte;

  protected

  published

    constructor Create;

  public

    property Bytes[X,Y:Integer]:Byte read GetBytes write SetBytes;

    procedure LoadFromFile(FileName:String);

    procedure ToGray;

  end;

 

implementation

 

procedure TVczhBitmap.SetFormat;

begin

  HandleType:=bmDIB;

  PixelFormat:=pf24bit;

end;

 

function TVczhBitmap.GetBytePointer(X,Y:Integer):PByte;

begin

  if Line<>Y then

  begin

    Line:=Y;

    Data:=ScanLine[Y];

  end;

  Longint(result):=Longint(Data)+X;

end;

 

procedure TVczhBitmap.SetBytes(X,Y:Integer;Value:Byte);

begin

  GetBytePointer(X,Y)^:=Value;

end;

 

function TVczhBitmap.GetBytes(X,Y:Integer):Byte;

begin

  result:=GetBytePointer(X,Y)^;

end;

 

constructor TVczhBitmap.Create;

begin

  inherited Create;

  SetFormat;

  Line:=-1;

end;

 

procedure TVczhBitmap.LoadFromFile(FileName:String);

begin

  inherited LoadFromFile(FileName);

  SetFormat;

  Line:=-1;

end;

 

procedure TVczhBitmap.ToGray;

var X,Y,R:Integer;

    B:Byte;

begin

  for Y:=0 to Height-1 do

    for X:=0 to Width-1 do

    begin

      R:=0;

      for B:=0 to 2 do

        R:=R+GetBytes(X*3+B,Y);

      for B:=0 to 2 do

        SetBytes(X*3+B,Y,R div 3);

    end;

end;

 

end.

此后,我们需要建立几个窗体。第一个用来显示图片,第二个用来处理图片,其他的窗体都继承自第二个窗体,包含实际的处理方法。

先看第二个窗口:

unit untProc;

 

interface

 

uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, ExtCtrls, untBitmapProc, StdCtrls, ComCtrls;

 

type

  TfrmProcessor = class(TForm)

    pbBar: TPaintBox;

    gpProc: TGroupBox;

    Button1: TButton;

    procedure FormCreate(Sender: TObject);

    procedure FormDestroy(Sender: TObject);

    procedure FormShow(Sender: TObject);

   

[1] [2] [3]  下一页

打印本文 打印本文 关闭窗口 关闭窗口