我需要代码来从文件中读取图像并将图像转换为整数数组。图片的格式为BMP
,而我使用的是vb.net-2010
答案 0 :(得分:0)
您可以在How can I read image pixels' values as RGB into 2d array?
上找到类似的问题和有价值的答案(尽管该问题和答案是针对c#的,我认为它们将帮助您理解解决方案)。首先,您需要将文件加载到System.Drawing.Bitmap对象。然后,您可以使用GetPixel方法读取像素值。注意,每个像素数据都包括一个Color值。您可以使用ToArgb()方法将此值转换为整数值。
Imports System.Drawing;
...
Dim img As New Bitmap("C:\test.JPG")
Dim imageArray (img.Width, img.Height) As Integer
Dim i, j As Integer
For i = 0 To img.Width
For j = 0 To img.Height
Dim pixel As Color = img.GetPixel(i,j)
imageArray (i,j) = pixel.ToArgb()
Next j
Next i
...
以及将2D数组存储到BMP对象的情况(假设您有一个100x100 2D数组imageArray)
Imports System.Drawing;
...
Dim img As New Bitmap(100,100)
Dim i, j As Integer
For i = 0 To img.Width
For j = 0 To img.Height
img.SetPixel(i,j,Color.FromArgb(imageArray(i,j)))
Next j
Next i
...