2016-06-29 51 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;