2016-06-29 53 views
0

我在Matlab上編寫了一個算法。因此我想在.Net上使用它。我完美地將.m文件轉換爲.dll,以便在Matlab庫編譯器上使用.Net。首先,我嘗試將Matlab函數轉換爲.dll而沒有任何參數,並且它在.Net上運行良好。但是,當我想用​​參數使用該函數時,下面會出現一些錯誤。該參數基本上是圖像。我調用該函數基於Matlab這樣I = imread('xxx.jpg'); Detect(I);所以我的C#這樣的如何在.NET中將位圖轉換爲MWArray(Matlab陣列)

static void Main(string[] args) 
    { 
     DetectDots detectDots = null; 
     Bitmap bitmap = new Bitmap("xxx.jpg"); 

     //Get image dimensions 
     int width = bitmap.Width; 
     int height = bitmap.Height; 
     //Declare the double array of grayscale values to be read from "bitmap" 
     double[,] bnew = new double[width, height]; 
     //Loop to read the data from the Bitmap image into the double array 
     int i, j; 
     for (i = 0; i < width; i++) 
     { 
      for (j = 0; j < height; j++) 
      { 
       Color pixelColor = bitmap.GetPixel(i, j); 
       double b = pixelColor.GetBrightness(); //the Brightness component 

       bnew.SetValue(b, i, j); 
      } 
     } 

     MWNumericArray arr = bnew; 
     try 
     { 
      detectDots = new DetectDots(); 

      detectDots.Detect(arr); 
      Console.ReadLine(); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.Message); 
     } 
    } 

代碼我使用的是被稱爲XXX.JPG是5312 X 2988 但我發現了下面這個錯誤相同的圖像。

... MWMCR::EvaluateFunction error ... 
Index exceeds matrix dimensions. 
Error in => DetectDots.m at line 12. 

... Matlab M-code Stack Trace ... 
    at 
file C:\Users\TAHAME~1\AppData\Local\Temp\tahameral\mcrCache9.0\MTM_220\MTM\DetectDots.m, name DetectDots, line 12. 

重要的是,它說「索引超過矩陣尺寸」,它是真正的方式將位圖轉換爲MWArray?問題是什麼?

回答

0

我意識到C#中的行對應於MWarray中的列。所以我改變了代碼上的小東西。

代替double[,] bnew = new double[width, height];我用它double[,] bnew = new double[height, width];

,取而代之的bnew.SetValue(b, i, j);我用它bnew.SetValue(b, j, i);

有人可能會使用整個代碼關於位至MWArray下面

 Bitmap bitmap = new Bitmap("001-2.bmp"); 

     //Get image dimensions 
     int width = bitmap.Width; 
     int height = bitmap.Height; 
     //Declare the double array of grayscale values to be read from "bitmap" 
     double[,] bnew = new double[height, width]; 

     //Loop to read the data from the Bitmap image into the double array 
     int i, j; 
     for (i = 0; i < width; i++) 
     { 
      for (j = 0; j < height; j++) 
      { 
       Color pixelColor = bitmap.GetPixel(i, j); 
       double b = pixelColor.GetBrightness(); //the Brightness component 

       //Note that rows in C# correspond to columns in MWarray 
       bnew.SetValue(b, j, i); 
      } 
     } 

     MWNumericArray arr = bnew;