Delphi GDI+ 图形处理(3)

本文介绍了使用Delphi的GDI+进行图像处理的几种基本算法,包括柔化、锐化、图像混合、转为灰度图像、对比度调整、反色处理、亮度调整、浮雕效果和马赛克效果,并提供了相应的源代码示例。
摘要由CSDN通过智能技术生成
探究Delphi的图形处理 之七 -- 柔化和锐化处理 

 

  第三章           基本图像处理算法

3.1柔化和锐化处理

柔化处理的原理是将图片中每一个像素都由与其相邻的n*n个像素的平均值来代替。N的取值决定了其模糊程度。下面是柔化处理的程序

程序3.1

unit Unit1;

{柔化处理}

Interface

uses

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

  Dialogs, GraphicProcess, StdCtrls, ExtCtrls;

type

  TForm1 = class(TForm)

    PaintBox1: TPaintBox;

    btnExe: TButton;

    txtN: TEdit;

    Label1: TLabel;

    procedure FormCreate(Sender: TObject);

    procedure PaintBox1Paint(Sender: TObject);

    procedure btnExeClick(Sender: TObject);

    procedure FormDestroy(Sender: TObject);

  private

    Procedure SmoothPicture(const Bit : TBitmap;var  n : Integer);

  public

    { Public declarations }

  end;

 procedure WritePixel(Pic: TBitmap; tPix: TPixels);

 procedure ReadPixel(Pic: Tbitmap; var tPix: TPixels);

var

  Form1: TForm1;

  Bits : TBitmap;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);

begin

  Bits:=TBitmap.Create;

  Bits.LoadFromFile(‘Test.Bmp‘);

end;

procedure TForm1.PaintBox1Paint(Sender: TObject);

begin

  PaintBox1.Canvas.StretchDraw(Rect(0,0,400,300),Bits);

end;

procedure TForm1.SmoothPicture(const Bit: TBitmap;var n: Integer);

var R,G,B:Integer;

    i,j,k,l : Integer;

    Pix : TPixels;

    nDiv : Integer;

    nDivRs : Integer;

    jP,jM,ip,im:Integer;

    OpCount : Integer;

begin

  ReadPixel(Bit,Pix);

  if n mod 2 = 0 then n := n +1;

  nDiv := n * n;

  nDivRs := n div 2;

  For i := 0 to Bit.Width-1 do begin

    ip:= i + nDivRs;

    im := i ;

    if im < 0 then im := 0;

    if ip > Bit.Width -1 then ip := Bit.Width-1;

    For j := 0 to Bit.Height -1 do

    begin

      R:=0;

      G:=0;

      B:=0;

      jP := j + nDivRs;

      jM := j - nDivRs;

      if Jp > bit.Height-1 then

       jp := Bit.Height-1;

      if jm <0 then jm :=0;

      OpCount := (ip - im+1) *(jp-jm+1);

      For k := im to Ip do  begin

        For l := jm to jp do

        begin

          R := R + Pix[k,l].rgbtRed;

          G := G + Pix[k,l].rgbtGreen;

          B := B + Pix[k,l].rgbtBlue;

        end;

      end;

      Pix[i,j].rgbtBlue := B div opCount;

      Pix[i,j].rgbtGreen := G div opCount;

      Pix[i,j].rgbtRed := R div  opCount;

    end;

  end;

  WritePixel(Bit,Pix);

end;

procedure ReadPixel(Pic: Tbitmap; var tPix: TPixels);

Var PixPtr:PbyteArray;i,j,m:Integer;

begin

  SetLength(tPix,Pic.Width,Pic.Height);

  Pic.PixelFormat := pf24bit;

  Pic.HandleType:=bmDIB;

  For i :=0 to pic.Height-1 do begin

      PixPtr:=Pic.ScanLine[i];

      for  j:= 0 to pic.Width-1 do begin

         m := j*3;

         tPix[j,i].rgbtBlue:=PixPtr[m];

         tPix[j,i].rgbtGreen := PixPtr[m+1];

         tPix[j,i].rgbtRed := PixPtr[m+2];

      end;

  end;

end;

procedure WritePixel(Pic: TBitmap; tPix: TPixels);

var PixPtr:PByteArray;i,j,m:Integer;

begin

  pic.PixelFormat := pf24bit;

  pic.HandleType:=bmDIB;

  Pic.Height := High(tPix[0])+1;

  Pic.Width:= High(tPix)+1;

  For i :=0 to pic.Height-1 do begin

      PixPtr:=Pic.ScanLine[i];

      for  j:= 0 to pic.Width-1 do begin

         m := j*3;

         PixPtr[M] := tPix[j,i].rgbtBlue;

         PixPtr[m+1] := tPix[j,i].rgbtGreen;

         PixPtr[m+2] := tPix[j,i].rgbtRed;

      end;

  end;

end;

procedure TForm1.btnExeClick(Sender: TObject);

var n :Integer;

begin

  n := StrToInt(txtN.Text);

  Bits.LoadFromFile(‘Test.bmp‘);

  SmoothPicture(Bits,n);

  PaintBox1.Refresh;

end;

procedure TForm1.FormDestroy(Sender: TObject);

begin

  Bits.Free;

end;

end.

程序的运行结果如下图所示。

原图

柔化系数=21

       锐化处理的原理是把每一像素的值与该像素斜上方的像素值之差乘以一个系数再加上该像素原来的颜色值。

如果记图像中任意一个像素(x,y) (x∈[1,图像宽度-1],y∈[1,图像高度-1])修改前的RGB分值分别为OldRed, OldGreen, OldBlue, 修改后的RGB分值分别为NewR,NewG,NewB,有:

 

newR = (oldR - (x-1,y-1)的Red分值)×待定系数 + OldRed

newG = (oldG - (x-1,y-1)的Green分值)×待定系数 + OldGreen

newB = (oldB - (x-1,y-1)的Blue分值)×待定系数 + OldBlue

 

根据这个公式,我们的程序如下:

程序3.12

unit Sharp;

 

interface

 

uses

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

  Dialogs, StdCtrls, ExtCtrls;

 

type

  TPixels = Array of Array of TRGBTriple;

  TfrmMain = class(TForm)

    PaintBox1: TPaintBox;

    btnExecute: TButton;

    lblCap: TLabel;

    txtS: TEdit;

    procedure FormCreate(Sender: TObject);

    procedure FormDestroy(Sender: TObject);

    procedure btnExecuteClick(Sender: TObject);

    procedure PaintBox1Paint(Sender: TObject);

  private

    Procedure SharpPic(Bit : TBitmap; n : Single);

  public

    { Public declarations }

  end;

  procedure WritePixel(Pic: TBitmap; tPix: TPixels);

  procedure ReadPixel(Pic: Tbitmap; var tPix: TPixels);

 

var

  frmMain: TfrmMain;

  Bits : TBitmap;

implementation

{$R *.dfm}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值