代码之家  ›  专栏  ›  技术社区  ›  Richard Knop

如何在for循环中创建数组

c#
  •  1
  • Richard Knop  · 技术社区  · 14 年前

            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件。

    我在图像中迭代如下:

            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长。

    5 回复  |  直到 14 年前
        1
  •  2
  •   Hans Passant    14 年前

    您肯定想在开始循环之前分配数组,完成后再使用结果。当像素不是白色或黑色时,你应该做一些有意义的事情。像这样的:

            Bitmap objBitmap = new Bitmap("im.bmp");
            double[][] array = new double[objBitmap.Height][];
            for (int y = 0; y < objBitmap.Height; y++) {
                array[y] = new double[objBitmap.Width];
                for (int x = 0; x < objBitmap.Width; x++) {
                    Color col = objBitmap.GetPixel(x, y);
                    if (col == Color.White) array[y][x] = 1.0;
                    else if (col == Color.Black) array[y][x] = 0.0;
                    else array[y][x] = 0.5;  // whatever
                }
            }
            // Do something with array
            //...
    

    不确定位图的生存期,应该在某个地方调用objBitmap.Dispose()。利用 使用

        2
  •  1
  •   Carlo V. Dango    14 年前

    就在你的 for (int x = 0; x < objBitmap.Width; x++) 创建双精度数组并将其插入到前面的循环中。然后在循环结束后将其插入 TrainPatterns

        3
  •  1
  •   Merlyn Morgan-Graham    14 年前

    使用 List<double> 而不是 double[]

    var trainResults = new List<double[]>();
    
    for(int y = 0; ...)
    {
        for(int x = 0; ...)
        {
            if(colors match...)
            {
                trainResults.Add(new double[] { x, y });
            }
            // etc...
        }
    }
    

    另外,我建议您创建一个新的结构,而不是一个双数组,这样当您使用这些结果时,您的代码所做的事情就更加明显了:

    class TrainResult
    {
        TrainResult(int x = 0; int y = 0)
        {
            this.X = x;
            this.Y = y;
        }
    
        public int X;
        public int Y;
    }
    
    // ...
    
    for(int y = 0; ...)
        for(int x = 0; ...)
            if(match ...)
                trainResults.Add(new TrainResult(x, y));
    
    // ...
    
    foreach(TrainResult result in trainResults)
    {
        // Do something with result.X, result.Y.
        // This is more obvious than result[0], result[1]
    }
    
        4
  •  1
  •   Gabe Timothy Khouri    14 年前

    看起来你想做的事情可以用LINQ而不是 for 循环:

    Bitmap objBitmap = new Bitmap("im.bmp");
    var TrainPattern = (from y in Enumerable.Range(0, objBitmap.Height)
                        from x in Enumerable.Range(0, objBitmap.Width)
                        select new double[] { x, y }).ToArray();
    var TrainResults = (from y in Enumerable.Range(0, objBitmap.Height)
                        from x in Enumerable.Range(0, objBitmap.Width)
                        let col = objBitmap.GetPixel(x, y)
                        select (col.R == 255 && col.G == 255 && col.B == 255) ?
                                new[] { 1.0 } : new[] { 0.0 }).ToArray();
    

        5
  •  1
  •   Marcelo Cantos    14 年前

    除非您的数据结构有特殊要求,否则我强烈建议将位存储在 BitArray :

    Bitmap objBitmap = new Bitmap("im.bmp");
    var white = new BitArray(objBitmap.Width*objBitmap.Height);
    var black = new BitArray(objBitmap.Width*objBitmap.Height);
    for (int y = 0, i = 0; y < objBitmap.Height; ++y)
    {
        for (int x = 0; x < objBitmap.Width; ++x, ++i)
        {
            Color c = objBitmap.GetPixel(x, y);
            white[i] = c.R == 255 && c.G == 255 && c.B == 255;
            black[i] = c.R == 0 && c.G == 0 && c.B == 0;
        }
    }
    

    var middle = black[i].Clone().Not().And(white.Clone().Not()) (我想;我从来没有用过这些方法)。

    从这些位图访问位非常简单:

    if (white[x + objBitmap.Width*y]) { /* The pixel at (x, y) is white. */ }