0

我想調試代碼並運行到此System.IndexOutOfRangeException。有人可以幫助我如何解決它。請查看發生異常的圖像。它會成功讀取「線陣」的第一行,但會發生第二行異常。當i = 1和j = 0
這裏是發生異常:如何處理System.IndexOutOfRangeException

(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; 
     } 

    } 
} 
+0

似乎線路[1] =的String.Empty –

+1

沒有,因爲在'.RemoveEmptyEntries'。我認爲'lines.Length == 1' –

+0

我同意@HenkHolterman。例如,如果在'text'字符串中有一個-------'\ r \ n \ r \ n'(注意空格) – Pikoh

回答

1

嘗試通過

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

取代

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

此外,您必須確定text包含至少32個非空行,每行有32個字符。如果更改Split不能幫助你,那麼這是text變量的問題。

從你的描述似乎沒有lines[1];

你所有的線都放在lines[0]; - 你可以看到它的第二圖像

+0

這可能與Split有關,但這只是一個整體變化。 –

+0

它沒有幫助。說無效的論據。 –

+0

@EhsanUlHaq,我可以從你那裏得到文本變量的值嗎?在你的第二行圖片[0]有很多行,可能問題在那裏?行[0]應該是圖像的第一行,行[1] - 第二... –