我在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?問題是什麼?