delphi FMX图像简单滤波(中值、均值、高斯)

//排序 paixu
procedure paixu(var temp : array of Byte);
var
   i,j : Integer;
   t : Byte;
begin
   for i := low(temp) to high(temp) do
    for j := i to high(temp) do
     if temp[i]<temp[j] then
     begin
       t := temp[i];
       temp[i] := temp[j];
       temp[j] := t;
     end;
end;
//灰度图的中值滤波 3x3
procedure zhongzhi(b : TBitmap);
var
   b_read : TBitmap;
   x, y : Integer;
   wdata , rdata : TBitmapData ;
   p: PByteArray;
   p1 , p2 , p3 : PByteArray;
   temp : array [0..8] of Byte;
begin
   b_read := TBitmap.Create;
   b_read.Assign(b);
   //一个用来修改  w一个用来读取 r
   if  b.Map( TMapAccess.Write,wdata) and b_read.Map( TMapAccess.Read,rdata) then
   begin
        for y := 1 to rdata.Height - 2 do
        begin
            p := wdata.GetScanline(y);
            p1 := rdata.GetScanline(y-1);
            p2 := rdata.GetScanline(y);
            p3 := rdata.GetScanline(y+1);
            for x := 1 to rdata.Width - 2 do
            begin
                temp[0] := p1[x*4-4];
                temp[1] := p1[x*4];
                temp[2] := p1[x*4+4];
                temp[3] := p2[x*4-4];
                temp[4] := p2[x*4];
                temp[5] := p2[x*4+4];
                temp[6] := p3[x*4-4];
                temp[7] := p3[x*4];
                temp[8] := p3[x*4+4];
                //排序 mao
                paixu(temp);
                //赋值  f
                p[x*4] := temp[4];
                p[x*4+1] := temp[4];
                p[x*4+2] := temp[4];
                //不透明 b
                p[x*4+3] := 255;
            end;
        end;
        b.Unmap(wdata);
        b_read.Unmap(rdata);
   end;
   b_read.Destroy;
end;

//求和 sum
function sum(temp : array of Byte):Byte;
var
   I : Integer;
begin
   result := 0;
   for I := Low(temp) to High(temp) do
   begin
       result := result + temp[I];
   end;
end;
//均值滤波  3x3
procedure junzhi(b : TBitmap);
var
   b_read : TBitmap;
   x, y : Integer;
   wdata , rdata : TBitmapData ;
   p: PByteArray;
   p1 , p2 , p3 : PByteArray;
   temp : array [0..8] of Byte;
begin
   b_read := TBitmap.Create;
   b_read.Assign(b);
   //一个用来修改  w一个用来读取 r
   if  b.Map( TMapAccess.Write,wdata) and b_read.Map( TMapAccess.Read,rdata) then
   begin
        for y := 1 to rdata.Height - 2 do
        begin
            p := wdata.GetScanline(y);
            p1 := rdata.GetScanline(y-1);
            p2 := rdata.GetScanline(y);
            p3 := rdata.GetScanline(y+1);
            for x := 1 to rdata.Width - 2 do
            begin
                temp[0] := p1[x*4-4] div 9;
                temp[1] := p1[x*4] div 9;
                temp[2] := p1[x*4+4] div 9;
                temp[3] := p2[x*4-4] div 9;
                temp[4] := p2[x*4] div 9;
                temp[5] := p2[x*4+4] div 9;
                temp[6] := p3[x*4-4] div 9;
                temp[7] := p3[x*4] div 9;
                temp[8] := p3[x*4+4] div 9;
                //平均  pin
                temp[4] := sum(temp);
                //赋值  f
                p[x*4] := temp[4];
                p[x*4+1] := temp[4];
                p[x*4+2] := temp[4];
                //不透明 b
                p[x*4+3] := 255;
            end;
        end;
        b.Unmap(wdata);
        b_read.Unmap(rdata);
   end;
   b_read.Destroy;
end;

//高斯滤波 3x3
procedure gaosi(b : TBitmap);
var
   b_read : TBitmap;
   x, y : Integer;
   wdata , rdata : TBitmapData ;
   p: PByteArray;
   p1 , p2 , p3 : PByteArray;
   temp : array [0..8] of Byte;
begin
   b_read := TBitmap.Create;
   b_read.Assign(b);
   //一个用来修改  w一个用来读取 r
   if  b.Map( TMapAccess.Write,wdata) and b_read.Map( TMapAccess.Read,rdata) then
   begin
        for y := 1 to rdata.Height - 2 do
        begin
            p := wdata.GetScanline(y);
            p1 := rdata.GetScanline(y-1);
            p2 := rdata.GetScanline(y);
            p3 := rdata.GetScanline(y+1);
            for x := 1 to rdata.Width - 2 do
            begin
                temp[0] := p1[x*4-4] div 16;
                temp[1] := p1[x*4] div 8;
                temp[2] := p1[x*4+4] div 16;
                temp[3] := p2[x*4-4] div 8;
                temp[4] := p2[x*4] div 4;
                temp[5] := p2[x*4+4] div 8;
                temp[6] := p3[x*4-4] div 16;
                temp[7] := p3[x*4] div 8;
                temp[8] := p3[x*4+4] div 16;
                //高斯模板 3x3
                temp[4] := sum(temp);
                //赋值  f
                p[x*4] := temp[4];
                p[x*4+1] := temp[4];
                p[x*4+2] := temp[4];
                //不透明 b
                p[x*4+3] := 255;
            end;
        end;
        b.Unmap(wdata);
        b_read.Unmap(rdata);
   end;
   b_read.Destroy;
end;

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大米粥哥哥

感谢认可!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值