将图像转换为矩阵

Oue*_*ine 3 delphi image matrix bmp

我试图将图像(比方说黑白)转换为矩阵(其中0 =黑色,1 =白色)

我试过这个代码:

procedure TForm1.Button1Click(Sender: TObject);
type
  tab = array[1..1000,1..1000] of byte;
var i,j: integer;
    s : string;
    image : TBitmap;
    t : tab;
begin
  image := TBitmap.Create;
  image.LoadFromFile('c:\image.bmp');

  s := '';
  for i := 0 to image.Height do
  begin
     for j := 0 to image.Width do
     begin
      if image.Canvas.Pixels[i,j] = clWhite then
        t[i,j] := 0
      else
        t[i,j] := 1;

     end;
  end;
  for i := 0 to image.Height do
  begin
    for j := 0 to image.Width do
     begin
      s:=s + IntToStr(t[i,j]);
     end;
     Memo1.Lines.Add(s);
     s:='';
  end;
end;
Run Code Online (Sandbox Code Playgroud)

但它给了我错误的结果.

任何的想法?

And*_*and 12

您的代码中有五个错误和另外两个问题!

首先,

for i := 0 to image.Height do
Run Code Online (Sandbox Code Playgroud)

必须被替换

for i := 0 to image.Height - 1 do
Run Code Online (Sandbox Code Playgroud)

(为什么?)和类似的,

for j := 0 to image.Width do
Run Code Online (Sandbox Code Playgroud)

必须被替换

for j := 0 to image.Width - 1 do
Run Code Online (Sandbox Code Playgroud)

其次,Pixels数组接受参数[x, y],而不是[y, x].因此,您需要更换

image.Canvas.Pixels[i,j]
Run Code Online (Sandbox Code Playgroud)

通过

image.Canvas.Pixels[j,i]
Run Code Online (Sandbox Code Playgroud)

第三,你写了"0 =黑色和1 =白色",但显然你做了相反的事情!

第四,你尝试访问t[0, 0],即使你的矩阵开始索引1.使用array[0..1000,0..1000] of byte;来解决这个问题.

第五,你有内存泄漏(image没有释放 - 使用try..finally).

此外,最好使用动态数组:

type
  TByteMatrix = array of array of byte;

var
  mat: TByteMatrix;
Run Code Online (Sandbox Code Playgroud)

然后你开始

SetLength(mat, image.Height - 1, image.Width - 1);
Run Code Online (Sandbox Code Playgroud)

如果你想要它索引[y, x],否则相反.

最后,Pixels在这种情况下你根本不应该使用该属性,因为它非常慢.相反,使用该Scanline属性.有关更多信息,请参阅那个其他内容.

此外,只需在更新备忘录控件Memo1.Lines.BeginUpdate之前和Memo1.Lines.EndUpdate之后添加,您就可以获得很快的速度.


TLa*_*ama 5

以下过程将输入ABitmap位图转换为多维AMatrix字节数组,表示像素,其中0值表示白色像素,1表示任何其他颜色:

type
  TPixelMatrix = array of array of Byte;

procedure BitmapToMatrix(ABitmap: TBitmap; var AMatrix: TPixelMatrix);
type
  TRGBBytes = array[0..2] of Byte;
var
  I: Integer;
  X: Integer;
  Y: Integer;
  Size: Integer;
  Pixels: PByteArray;
  SourceColor: TRGBBytes;
const
  TripleSize = SizeOf(TRGBBytes);
begin
  case ABitmap.PixelFormat of
    pf24bit: Size := SizeOf(TRGBTriple);
    pf32bit: Size := SizeOf(TRGBQuad);
  else
    raise Exception.Create('ABitmap must be 24-bit or 32-bit format!');
  end;

  SetLength(AMatrix, ABitmap.Height, ABitmap.Width);
  for I := 0 to TripleSize - 1 do
    SourceColor[I] := Byte(clWhite shr (16 - (I * 8)));

  for Y := 0 to ABitmap.Height - 1 do
  begin
    Pixels := ABitmap.ScanLine[Y];
    for X := 0 to ABitmap.Width - 1 do
    begin
      if CompareMem(@Pixels[(X * Size)], @SourceColor, TripleSize) then
        AMatrix[Y, X] := 0
      else
        AMatrix[Y, X] := 1;
    end;
  end;
end;
Run Code Online (Sandbox Code Playgroud)

此过程将多维AMatrix数组的字节打印到AMemo备注框:

procedure ShowPixelMatrix(AMemo: TMemo; const AMatrix: TPixelMatrix);
var
  S: string;
  X: Integer;
  Y: Integer;
begin
  AMemo.Clear;
  AMemo.Lines.BeginUpdate;
  try
    AMemo.Lines.Add('Matrix size: ' + IntToStr(Length(AMatrix[0])) + 'x' +
      IntToStr(Length(AMatrix)));
    AMemo.Lines.Add('');

    for Y := 0 to High(AMatrix) do
    begin
      S := '';
      for X := 0 to High(AMatrix[Y]) - 1 do
      begin
        S := S + IntToStr(AMatrix[Y, X]);
      end;
      AMemo.Lines.Add(S);
    end;
  finally
    AMemo.Lines.EndUpdate;
  end;
end;
Run Code Online (Sandbox Code Playgroud)

并使用上述程序:

procedure TForm1.Button1Click(Sender: TObject);
var
  Bitmap: TBitmap;
  PixelMatrix: TPixelMatrix;
begin
  Bitmap := TBitmap.Create;
  try
    Bitmap.LoadFromFile('d:\Image.bmp');
    BitmapToMatrix(Bitmap, PixelMatrix);
  finally
    Bitmap.Free;
  end;
  ShowPixelMatrix(Memo1, PixelMatrix);
end;
Run Code Online (Sandbox Code Playgroud)

上述BitmapToMatrix过程的这一扩展允许您指定参数luminance给定的级别AMinIntensity将像素视为非白色.

AMinIntensity值越接近0,越打火机像素被视为非白色.这允许您使用颜色强度容差(例如,以更好地识别抗锯齿文本):

procedure BitmapToMatrixEx(ABitmap: TBitmap; var AMatrix: TPixelMatrix;
  AMinIntensity: Byte);
type
  TRGBBytes = array[0..2] of Byte;
var
  X: Integer;
  Y: Integer;
  Gray: Byte;
  Size: Integer;
  Pixels: PByteArray;
begin
  case ABitmap.PixelFormat of
    pf24bit: Size := SizeOf(TRGBTriple);
    pf32bit: Size := SizeOf(TRGBQuad);
  else
    raise Exception.Create('ABitmap must be 24-bit or 32-bit format!');
  end;

  SetLength(AMatrix, ABitmap.Height, ABitmap.Width);

  for Y := 0 to ABitmap.Height - 1 do
  begin
    Pixels := ABitmap.ScanLine[Y];
    for X := 0 to ABitmap.Width - 1 do
    begin
      Gray := 255 - Round((0.299 * Pixels[(X * Size) + 2]) +
        (0.587 * Pixels[(X * Size) + 1]) + (0.114 * Pixels[(X * Size)]));

      if Gray < AMinIntensity then
        AMatrix[Y, X] := 0
      else
        AMatrix[Y, X] := 1;
    end;
  end;
end;
Run Code Online (Sandbox Code Playgroud)