Delphi 图像二值化


本文整理自网络,侵删。

 

uses

  Vcl.Imaging.pngimage, Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,

  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,

  Vcl.ExtCtrls, Vcl.ComCtrls;

  

procedure TForm1.btn_erzhihuaClick(Sender: TObject);

var

  p: PByteArray;

  Gray, x, y: Integer;

  Bmp: TBitmap;

begin

 

  Bmp := TBitmap.Create;

  Bmp.Assign(img1.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 > 120 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;

  ClearPicNoise(Bmp, 2);

  img2.Picture.Bitmap.Assign(Bmp);

  Bmp.Free;

end;

来源:http://www.coder163.com/language/delphi/

相关阅读 >>

Delphi xe8 form.onshow的一个小问题

Delphi 数组类型与数组指针的巧妙利用

Delphi xe6 读取android设备联系人

Delphi之memo组件

Delphi 相对路径

Delphi在手机桌面图标上显示数字

Delphi 将字符串转换成 utf8 编码的函数

Delphi原生方法取得文件的md5

Delphi xe10 fdmemtable 数据保存本地和从本地加载

Delphi mediaplayer1 设置音量

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



打赏

取消

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

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

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

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

评论

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