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 如何把一个exe做为res加入到dll中,并在运行时生成exe文件执行

Delphi 官方stylebook大全使用

Delphi idhttp解决获取utf-8网页中文乱码问题

Delphi应用程序 paramstr()带有参数

Delphi 如何读取内存中的数据?

Delphi now、date、time、currentyear - 当前日期

Delphi tstringlist的用法

Delphi 实现php的urlencode编码效果

Delphi 防止程序重复打开运行

Delphi form的borderstyle属性

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



打赏

取消

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

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

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

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

评论

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