如题

解决方案 »

  1.   

    unit Unit1;interfaceuses
        Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
        Dialogs, StdCtrls, ExtCtrls, ExtDlgs;type
        TForm1 = class(TForm)
            Image1: TImage;
        Button2: TButton;
            Image2: TImage;
        Button1: TButton;
            OpenPictureDialog1: TOpenPictureDialog;
            procedure Button2Click(Sender: TObject);
            procedure Button1Click(Sender: TObject);
        private
            { Private declarations }
        public
            { Public declarations }
        end;var
        Form1: TForm1;
    implementation
    uses math;
    {$R *.dfm}procedure TForm1.Button2Click(Sender: TObject);
    var
        bmp1, bmp2: Tbitmap;
        p1, p2, p3, p4: pbytearray;
        //定义四个pbytearray类型变量
        i, j, z: integer;
        y: array[0..8] of integer;
    begin
        y[0] := 0; y[1] := -1; y[2] := 0;
        y[3] := -1; y[4] := 5; y[5] := -1;
        y[6] := 0; y[5] := -1; y[8] := 0;
        //卷积矩阵
        z := 1;
        //卷积核
        bmp1 := Tbitmap.Create;
        bmp2 := Tbitmap.Create;
        bmp1.Assign(image1.Picture.Bitmap);
        bmp1.PixelFormat := pf24bit;
        //24为格式便于处理
        bmp1.Width := image1.Picture.Graphic.Width;
        bmp1.Height := image1.Picture.Graphic.Height;
        bmp2.Assign(bmp1);
        //备用的位图
        bmp2.PixelFormat := pf24bit;
        for j := 1 to bmp1.Height - 2 do
        begin
            p1 := bmp1.ScanLine[j];
            //第一条扫描线
            p2 := bmp2.ScanLine[j - 1];
            //第二条扫描线,为了防止数据变化,在备用位图上操作
            p3 := bmp2.ScanLine[j];
            p4 := bmp2.ScanLine[j + 1];
            //第三条扫描线
                    //三条相邻的扫描线
            for i := 1 to bmp1.Width - 2 do
            begin
                //进行卷积操作获取新的象素值
                p1[3 * i + 2] := min(255, max(0, ((y[0] * p2[3 * (i - 1) + 2]
                    +
                    y[1] * p2[3 * i + 2] + y[2] * p2[3 * (i + 1) + 2] + y[3]
                    * p3[3
                    * (i - 1)
                        + 2] + y[4] * p3[3 * i + 2] + y[5] * p3[3 * (i + 1) +
                        2] +
                        y[6]
                    * p4[3
                    * (i - 1) + 2] + y[5] * p4[3 * i + 2] + y[8] * p4[3 * (i
                        +
                        1) + 2]))
                        div
                    z));
                //重新算出红色分量
                p1[3 * i + 1] := min(255, max(0, ((y[0] * p2[3 * (i - 1) + 1]
                    +
                    y[1] * p2[3 * i + 1] + y[2] * p2[3 * (i + 1) + 1] + y[3]
                    * p3[3
                    * (i - 1)
                        + 1] + y[4] * p3[3 * i + 1] + y[5] * p3[3 * (i + 1) +
                        1] +
                        y[6]
                    * p4[3
                    * (i - 1) + 1] + y[5] * p4[3 * i + 1] + y[8] * p4[3 * (i
                        +
                        1) + 1]))
                        div
                    z));
                //重新算出蓝色分量
                p1[3 * i] := min(255, max(0, ((y[0] * p2[3 * (i - 1)] + y[1]
                    *
                    p2[3 * i] + y[2] * p2[3 * (i + 1)] + y[3] * p3[3 * (i -
                        1)] +
                    y[4] * p3[3
                    * i] + y[5] * p3[3 * (i + 1)] + y[6] * p4[3 * (i - 1)] +
                        y[5]
                    * p4[3 * i]
                    + y[8] * p4[3 * (i + 1)])) div z));
                //重新算出绿色分量
            end;
        end;
        Image2.Picture.Bitmap.Assign(Bmp1);
        //重新显示
        Image2.Invalidate;
        Bmp1.Free;
        bmp2.Free;
        //释放资源
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
        Self.OpenPictureDialog1.Filter := '*.bmp|*.bmp';
        if OpenPictureDialog1.Execute then
        begin
            Image1.Picture.Bitmap.LoadFromFile(OpenPictureDialog1.FileName);
        end;
    end;end.