Delphi二值图像腐蚀算法


本文整理自网络,侵删。

 procedure BitmapErose(Bitmap: TBitmap; bHoric: Boolean);
 //传入的Bitmap是一个二值位图,bHoric标志是水平方向还是竖直方向腐蚀
var
   X, Y: integer;
   newbmp: TBitmap;
   P, Q, R, O: pByteArray;
begin
   newbmp := TBitmap.Create;
   //动态创建位图
   newbmp.Assign(bitmap);
   if (bHoric) then
   begin
      for Y := 1 to newbmp.Height - 2 do
      begin
         O := bitmap.ScanLine[Y];
         P := newbmp.ScanLine[Y - 1];
         Q := newbmp.ScanLine[Y];
         R := newbmp.ScanLine[Y + 1];
         for X := 1 to newbmp.Width - 2 do
         begin
            if ((O[3 * X] = 0) and (O[3 * X + 1] = 0) and (O[3 * X + 2]= 0)) then
            begin
               // 判断黑点左右邻居是否有白色点,有则腐蚀,置该点为白色
               // 白色点则保持不变
               if (((Q[3 * (X - 1)] = 255) and (Q[3 * (X - 1) + 1] = 255) and (Q[3 * (X - 1) + 2] = 255))
                  or ((Q[3 * (X + 1)] = 255) and (Q[3 * (X + 1) + 1] = 255) and (Q[3 * (X + 1) + 2] = 255))
                  or ((P[3 * X] = 0) and (P[3 * X + 1] = 255) and (P[3 * X + 2] = 255))
                  or ((R[3 * X] = 255) and (R[3 * X + 1] = 255) and (R[3 * X + 2] = 255))) then
               begin
                  O[3 * X] := 255;
                  O[3 * X + 1] := 255;
                  O[3 * X + 2] := 255;
                  // 将满足条件的黑色点置为白色
               end;
            end;
         end;
      end;
   end else begin
      for Y := 1 to newbmp.Height - 2 do
      begin
         O := bitmap.ScanLine[Y];
         Q := newbmp.ScanLine[Y];
         for X := 1 to newbmp.Width - 2 do
         begin
            //  判断一个黑点上下邻居是否有白点,有则腐蚀,置黑点为白色
            //  白色点就保持不变
            if ((O[3 * X] = 0) and (O[3 * X + 1] = 0) and (O[3 * X + 2]= 0)) then
            begin
               if (((Q[3 * (X - 1)] = 255) and (Q[3 * (X - 1) + 1] = 255) and (Q[3 * (X - 1) + 2] = 255))
                  or ((Q[3 * (X + 1)] = 255) and (Q[3 * (X + 1) + 1] = 255) and (Q[3 * (X + 1) + 2] = 255))) then
               begin
                  O[3 * X] := 255;
                  O[3 * X + 1] := 255;
                  O[3 * X + 2] := 255;
                  // 将满足条件的黑色点置为白色
               end;
            end;
         end;
      end;
   end;
end;

相关阅读 >>

Delphi webbrowser 查找对话框

Delphi 通过进程名获得文件全路径的函数

Delphi tms web core 实现下载

Delphi xe8 androdi利用httpclient实现的一个app自动更新组件

Delphi 获取计算机从开机开始的已运行时间

Delphi indy smtp 发送邮件

Delphi启动控制面板的方法列表

Delphi winapi: getcurrentthread、getcurrentthreadid、getcurrentprocess、getcurrentprocessid

Delphi中创建json字符串

Delphi 流与字符串

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



打赏

取消

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

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

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

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

评论

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