c# - 如何处理 System.IndexOutOfRangeException

标签 c# .net exception machine-learning indexoutofrangeexception

我正在尝试调试代码并遇到此 System.IndexOutOfRangeException。有人可以帮助我如何解决它。请查看图中出现异常的位置。它成功读取“线阵列”的第一行,但当涉及到第二行时出现异常。当 i =1 且 j= 0 时发生异常。
这是:

(Image of Exception) (Image of Line Array)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace Handwriting.SVMs
{
    static class Features
    {
        /// <summary>
        ///   Extracts an image from a text containing the 
        ///   image representation as binary 0s and 1s.
        /// </summary>
        /// 
        public static Bitmap Extract(string text)
        {
            Bitmap bitmap = new Bitmap(32, 32, PixelFormat.Format32bppRgb);
            string[] lines = text.Split(new String[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < 32; i++)
            {
                for (int j = 0; j < 32; j++)
                {       
                    if (lines[i][j] == '0')
                        bitmap.SetPixel(j, i, Color.White);
                    else bitmap.SetPixel(j, i, Color.Black);
                }
            }
            return bitmap;
        }

        /// <summary>
        ///   Extracts a feature vector representation from
        ///   an image, "flattening" a binary image into an
        ///   array of 0s and 1s.
        /// </summary>
        /// 
        public static double[] Extract(Bitmap bmp)
        {
            double[] features = new double[32 * 32];
            for (int i = 0; i < 32; i++)
                for (int j = 0; j < 32; j++)
                    features[i * 32 + j] = (bmp.GetPixel(j, i).R == 255) ? 0 : 1;

            return features;
        }

        /// <summary>
        ///   Converts a feature vector containing 0s and 1s
        ///   representing each pixel in the image back into
        ///   an image.
        /// </summary>
        /// 
        public static Bitmap Export(double[] features)
        {
            Bitmap bitmap = new Bitmap(32, 32, PixelFormat.Format32bppRgb);

            for (int i = 0; i < 32; i++)
                for (int j = 0; j < 32; j++)
                {
                    double v = features[i * 32 + j];
                    v = 255 - Math.Max(0, Math.Min(255, Math.Abs(v) * 255));
                    bitmap.SetPixel(j, i, Color.FromArgb((int)v, (int)v, (int)v));
                }

            return bitmap;
        }

        public static double[] Preprocess(Bitmap bitmap)
        {
            double[] features = new double[64];

            for (int m = 0; m < 8; m++)
            {
                for (int n = 0; n < 8; n++)
                {
                    int c = m * 8 + n;
                    for (int i = m * 4; i < m * 4 + 4; i++)
                    {
                        for (int j = n * 4; j < n * 4 + 4; j++)
                        {
                            Color pixel = bitmap.GetPixel(j, i);
                            if (pixel.R == 0x00) // white
                                features[c] += 1;
                        }
                    }
                }
            }

            return features;
        }

    }
}

最佳答案

尝试替换

string[] lines = text.Split(new String[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

string[] lines = text.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);

此外,您必须确保 text 至少包含 32 个非空行,每行包含 32 个字符。如果更改 Split 对您没有帮助,那么问题出在 text 变量中。

根据您的描述,似乎没有 lines[1];

所有线条都放置在 lines[0]; 中 - 您可以在第二张图片中看到它

关于c# - 如何处理 System.IndexOutOfRangeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36863122/

相关文章:

c# - 移除不会再次运行的 quartz 触发器

c# - Reflector 将不再免费。还有其他选择吗?

c# - 避免在双链场景下指定key列

c# - 未处理的异常终止执行时退出代码?

javascript - 将 AJAX 中的 javascript 请求发送到 C# 方法,无返回值、ASP.NET、MVC

c# - 建议 Winforms 应用程序的任何错误记录工具,用于在远程数据库中写入错误

php - 如何使用 Symfony 4 中的事件处理异常?

c# - smtp 异常 收件人失败

c# - LinQ 到实体 : Doing the opposite query

c# - 如何在 Windows 10 uwp 中有效地使用 WriteableBitmap.setpixel()?