将图像转换为矩阵

编程入门 行业动态 更新时间:2024-10-27 04:26:14
本文介绍了将图像转换为矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试将图像(比方说黑白)转换为矩阵(其中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;

更多推荐

将图像转换为矩阵

本文发布于:2023-10-27 03:47:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1532217.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:矩阵   转换为   图像

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!