我正在尝试从名为btm
的图像中获取最大强度值和最小强度,以从“ max,min”中获取平均值,然后使用该平均值作为将图像转换为二进制图像的阈值。
因此,我使用了来自aforge库的直方图类,该类带有一个int数组,因此我尝试将图像btm
转换为该数组,但使用了函数ImageToByteArray
来将字节数据类型的返回数组转换为数组
System.Drawing.Image img = (System.Drawing.Image)btm;
byte[] imgarr = ImageToByteArray(img);
Histogram h = new Histogram(imgarr);
int Maxval= h.max();
int Minval= h.min();
。
public static byte[] ImageToByteArray(System.Drawing.Image imageIn)
{
using (var ms = new MemoryStream())
{
imageIn.Save(ms, imageIn.RawFormat);
return ms.ToArray();
}
}
答案 0 :(得分:1)
我要发布两个例程。您可以检查他们,以获取有关如何完成任务的想法。
步骤1。将Bitmap
转换为int[,]
:
public static int[,] ToInteger(Bitmap input)
{
//// We are presuming that the image is grayscale.
//// A color image is impossible to convert to 2D.
int Width = input.Width;
int Height = input.Height;
int[,] array2d = new int[Width, Height];
for (int y = 0; y < Height; y++)
{
for (int x = 0; x < Width; x++)
{
Color cl = input.GetPixel(x, y);
// image is Grayscale
// three elements are averaged.
int gray = (int)Convert.ChangeType(cl.R * 0.3 + cl.G * 0.59 + cl.B * 0.11, typeof(int));
array2d[x, y] = gray;
}
}
return array2d;
}
第2步。搜索最大和最小。
public int Max(int[,] values)
{
int max = 0;
for (int i = 1; i < values.GetLength(0); i++)
{
for (int j = 1; j < values.GetLength(1); j++)
{
if (values[i,j] > 0)
{
max = values[i, j];
}
}
}
return max;
}
public int Min(int[,] values)
{
... ... ...
if (values[i,j] < 0)
{
min = values[i];
}
... ... ...
return min;
}
您可以结合使用最后两个。
希望您能明白。