delphi FMX图像的二值化

来源:互联网 发布:淘宝中老年女夏装 编辑:程序博客网 时间:2024/05/21 01:57
procedure TForm2.TrackBar1Change(Sender: TObject);var   Gray, x, y ,v: Integer;   A_BMPData : TBitmapData ;   p: PByteArray;   b : TBitmap;begin   b := TBitmap.Create;   b.Assign(Image1.Bitmap);   v :=  round(TrackBar1.Value);   //b.PixelFormat := TPixelFormat.RGB;   //showmessage(inttostr(Integer(b.PixelFormat)));   //    PixelFormat 格式的问题  RGBA 但是无法设置成 RGB 他这个是只读属性   //    所以在计算的时候 x*4   if  b.Map( TMapAccess.ReadWrite, A_BMPData)  then   begin        for y := 0 to A_BMPData.Height - 1 do        begin            p := A_BMPData.GetScanline(y);            for x := 0 to A_BMPData.Width - 1 do            begin                //一个象素点三个字节                Gray := Round(p[x * 4 + 2] * 0.3 +                        p[x * 4 + 1] * 0.59 +                        p[x * 4] * 0.11);                //最后一个是设置透明度的 A                if gray >= v then                begin                    p[x * 4]:= 255; p[x * 4 + 1] := 255; p[x * 4 + 2] := 255;                    p[x * 4 + 3] := 255;                end                else                begin                    p[x * 4]:= 0;   p[x * 4 + 1] := 0;   p[x * 4 + 2] := 0;                    p[x * 4 + 3] := 255;                end;            end;        end;        b.Unmap(A_BMPData);   end;   Image2.Bitmap.Assign(b);   b.Destroy;end;