将图像转换为字节矩阵
convert image to byte Matrix
我正在尝试对图像 (png) 进行二值化处理以获得以下结果:
为此我使用了下面的代码,但现在我还没有得到预期的结果:
这给了我以下结果enter image description here
要比较两种颜色,请使用 ToArgb
方法,最好使用 StringBuilder
而不是每次都连接起来。
这对我有用:
var img = new Bitmap(pictureBox1.Image);
var sb = new StringBuilder();
for (int i = 0; i < img.Height; i++)
{
for (int j = 0; j < img.Width; j++)
{
if (img.GetPixel(j, i).ToArgb() == Color.White.ToArgb())
{
sb.Append("0");
}
else
{
sb.Append("1");
}
}
}
richTextBox1.Text = sb.ToString();
这会导致很多零和一些零,我的图片很大所以结果很难看,但在你的情况下它应该有效。
你可以这样做,它使用扫描线和锁位,而且速度相当快。
public unsafe static int[,] GetBits(string path )
{
using (var orig = new Bitmap(path))
{
var bounds = new Rectangle(0, 0, orig.Width, orig.Height);
// lock the array for direct access
var bitmapData = orig.LockBits(bounds, ImageLockMode.ReadWrite, PixelFormat.Format32bppPArgb);
try
{
// get the pointer
var scan0Ptr = (int*)bitmapData.Scan0;
// get the stride
var stride = bitmapData.Stride / 4;
// keep the black around
var black = Color.Black.ToArgb();
//Output
var array = new int[orig.Width, orig.Height];
// scan all x first then y
for (var y = 0; y < bounds.Bottom; y++)
for (var x = 0; x < bounds.Right; x++)
array[x, y] = *(scan0Ptr + x + y * stride) == black ? 0 : 1;
return array;
}
finally
{
// unlock the bitmap
orig.UnlockBits(bitmapData);
}
}
}
用法
var array = GetBits(@"d:\icon.png");
var w = array.GetLength(0);
var h = array.GetLength(1);
for (int i = 0; i < w; i++)
{
for (int j = 0; j < h; j++)
Console.Write(array[i,j]);
Console.WriteLine();
}
输出
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00011111111111111111111111111000
00011111111111111111111111111000
00011000000000000000000000011000
00011000000000000000000000011000
00011000000000000000000000011000
00011000000000000000000000011000
00011000000000000000000000011000
00011000000000000000000000011000
00011000000000000000000000011000
00011000000000000000000000011000
00011000000000000000000000011000
00011000000000000000000000011000
00011000000000011111111111111000
00011000000000011111111111111000
00011000000000011000000000011000
00011000000000011000000000011000
00011000000000011000000000011000
00011000000000011000000000011000
00011000000000011000000000011000
00011000000000011000000000011000
00011000000000011000000000011000
00011000000000011000000000011000
00011000000000011000000000011000
00011000000000011000000000011000
00011111111111111111111111111000
00011111111111111111111111111000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
原始图像 32x32
我正在尝试对图像 (png) 进行二值化处理以获得以下结果:
为此我使用了下面的代码,但现在我还没有得到预期的结果:
这给了我以下结果enter image description here
要比较两种颜色,请使用 ToArgb
方法,最好使用 StringBuilder
而不是每次都连接起来。
这对我有用:
var img = new Bitmap(pictureBox1.Image);
var sb = new StringBuilder();
for (int i = 0; i < img.Height; i++)
{
for (int j = 0; j < img.Width; j++)
{
if (img.GetPixel(j, i).ToArgb() == Color.White.ToArgb())
{
sb.Append("0");
}
else
{
sb.Append("1");
}
}
}
richTextBox1.Text = sb.ToString();
这会导致很多零和一些零,我的图片很大所以结果很难看,但在你的情况下它应该有效。
你可以这样做,它使用扫描线和锁位,而且速度相当快。
public unsafe static int[,] GetBits(string path )
{
using (var orig = new Bitmap(path))
{
var bounds = new Rectangle(0, 0, orig.Width, orig.Height);
// lock the array for direct access
var bitmapData = orig.LockBits(bounds, ImageLockMode.ReadWrite, PixelFormat.Format32bppPArgb);
try
{
// get the pointer
var scan0Ptr = (int*)bitmapData.Scan0;
// get the stride
var stride = bitmapData.Stride / 4;
// keep the black around
var black = Color.Black.ToArgb();
//Output
var array = new int[orig.Width, orig.Height];
// scan all x first then y
for (var y = 0; y < bounds.Bottom; y++)
for (var x = 0; x < bounds.Right; x++)
array[x, y] = *(scan0Ptr + x + y * stride) == black ? 0 : 1;
return array;
}
finally
{
// unlock the bitmap
orig.UnlockBits(bitmapData);
}
}
}
用法
var array = GetBits(@"d:\icon.png");
var w = array.GetLength(0);
var h = array.GetLength(1);
for (int i = 0; i < w; i++)
{
for (int j = 0; j < h; j++)
Console.Write(array[i,j]);
Console.WriteLine();
}
输出
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00011111111111111111111111111000
00011111111111111111111111111000
00011000000000000000000000011000
00011000000000000000000000011000
00011000000000000000000000011000
00011000000000000000000000011000
00011000000000000000000000011000
00011000000000000000000000011000
00011000000000000000000000011000
00011000000000000000000000011000
00011000000000000000000000011000
00011000000000000000000000011000
00011000000000011111111111111000
00011000000000011111111111111000
00011000000000011000000000011000
00011000000000011000000000011000
00011000000000011000000000011000
00011000000000011000000000011000
00011000000000011000000000011000
00011000000000011000000000011000
00011000000000011000000000011000
00011000000000011000000000011000
00011000000000011000000000011000
00011000000000011000000000011000
00011111111111111111111111111000
00011111111111111111111111111000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
原始图像 32x32