我正在尝试将图像(比方说黑白)转换为矩阵(其中0 =黑色,1 =白色)
I am trying to convert an image (lets say black and white) to a Matrix (where 0 = black and 1 = white)
我尝试使用此代码:
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;但它给了我错误的结果。
But it gave me wrong results.
任何想法?
推荐答案以下程序转换输入 ABitmap 位图到多维 AMatrix 字节数组,表示像素,其中0值表示白色像素,1表示任何其他像素color:
The following procedure converts the input ABitmap bitmap to a multidimensional AMatrix array of bytes, which represents pixels and where 0 value means white pixel and 1 means any other color:
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;此过程打印出多维 AMatrix 数组字节到 AMemo 备忘录框:
This procedure prints out the multidimensional AMatrix array of bytes to the AMemo memo box:
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;并使用上述程序:
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;上述 BitmapToMatrix 程序的此扩展允许您指定 亮度 等级由 AMinIntensity 参数给出非白色的像素。
This extension of the above BitmapToMatrix procedure allows you to specify at which luminance level given by the AMinIntensity parameter will be pixels taken as non-white.
AMinIntensity 值越接近0,像素越亮越亮被视为非白色。这允许您使用颜色强度容差(例如,以更好地识别抗锯齿文本):
The more the AMinIntensity value is closer to 0, the more lighter pixels are treated as non-white. This allows you to work with a color intensity tolerance (e.g. to better recognize antialiased text):
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;更多推荐
将图像转换为矩阵
发布评论