Delphi图像二值化


本文整理自网络,侵删。

 验证码识别之图像二值化

unit Unit1;

interface

uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, ExtDlgs, StdCtrls, ExtCtrls, Buttons;

type
    TForm1 = class(TForm)
        Image1: TImage;
        Image2: TImage;
        Button1: TButton;
        Button2: TButton;
        OpenPictureDialog1: TOpenPictureDialog;
    BitBtn1: TBitBtn;
    SavePictureDialog1: TSavePictureDialog;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    private
        { Private declarations }
    public
        { Public declarations }
    end;

var
    Form1: TForm1;

implementation
uses math;
{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
    Self.OpenPictureDialog1.Filter := '*.bmp|*.bmp';
    if self.OpenPictureDialog1.Execute then
    begin
        Image1.Picture.Bitmap.LoadFromFile(OpenPictureDialog1.FileName);
    end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
    p: PByteArray;
    Gray, x, y: Integer;
    Bmp: TBitmap;
begin
    Bmp := TBitmap.Create;
    Bmp.Assign(Image1.Picture.Bitmap);
    //设置为24位真彩色
    Bmp.PixelFormat := pf24Bit;
    randomize;
    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[x * 3 + 2] * 0.3 + p[x * 3 + 1] * 0.59 + p[x
                * 3] * 0.11);
            if gray > 128 then //全局阀值128
            begin
                p[x * 3] := 255;
                p[x * 3 + 1] := 255;
                p[x * 3 + 2] := 255;
            end
            else
            begin
                p[x * 3] := 0;
                p[x * 3 + 1] := 0;
                p[x * 3 + 2] := 0;
            end;
        end;
    end;
    Image2.Picture.Bitmap.Assign(Bmp);
    Bmp.Free;

end;

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
   Self.savepictureDialog1.Filter := '*.bmp|*.bmp';
    if self.savePictureDialog1.Execute then
    begin
        Image2.Picture.Bitmap.savetoFile(savePictureDialog1.FileName);
    end;
end;

end.

相关阅读 >>

Delphi通过将数据放入exe的资源信息中的方式制作自已的安装工具

Delphi中实现调整图像的色阶的算法

Delphi实现图像反色实例4种方法

Delphi 解决timage无法在窗体resize时改变canvas的大小

Delphi+汇编例子

Delphi 获取所有窗口标题

Delphi 将对象转换为json字符串或tjsonobject

firemonkey listbox 的几个事件:拖动,点击,长按

Delphi 运行时提升软件到管理员权限

Delphi webbroker 上传文件

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...