【发布时间】:2010-11-07 21:26:07
【问题描述】:
好的,我需要像这样创建两个数组:
double[][] TrainPatterns = {
new double[] { 0.0, 0.0 },
new double[] { 0.0, 1.0 },
new double[] { 0.0, 2.0 }
};
double[][] TrainResults = {
new double[] { 0.0 },
new double[] { 1.0 },
new double[] { 1.0 }
};
但每个都有 100 个项目。
我正在从图像中读取数组的值(TrainPatterns 包含像素的 x 和 y 坐标,TrainResults 包含像素的颜色,0 表示黑色,1 表示白色)。
我正在像这样遍历图像:
Bitmap objBitmap = new Bitmap("im.bmp");
for (int y = 0; y < objBitmap.Height; y++)
{
for (int x = 0; x < objBitmap.Width; x++)
{
// here I need to insert new double[] { (double)x, (double)y } to the
// TrainPatterns array
Color col = objBitmap.GetPixel(x, y);
if (col.R == 255 && col.G == 255 && col.B == 255)
{
// white color
// here I need to insert new double[] { 1.0 } to the
// TrainResults
}
if (col.R == 0 && col.G == 0 && col.B == 0)
{
// black color
// here I need to insert new double[] { 0.0 } to the
// TrainResults
}
}
}
如何在 for 循环中动态地将项目添加到这些数组中?我知道图像的宽度为 10px,高度为 10px,所以我知道数组的长度都是 100。
【问题讨论】:
-
我只需要问一些愚蠢的问题。 1)为什么要存储每个坐标?由于数组是密集的,你当然可以只保存这些值,它们的位置决定了坐标。 2) 为什么将每个结果存储为单个元素
double[]?你不能只使用double吗? 3) 你可以使用位图本身作为数据结构,使用 GetPixel 和 SetPixel 作为访问器吗? 4) 或者,您能否将整个位图存储为两个长整数(空间足以容纳 128 位)?
标签: c#