我在Matlab上写了一个算法。因此,我想在.Net上使用它。我完美地将.m文件转换为.dll,以便使用Matlab Library Compiler在.Net上使用它。首先,我尝试在没有任何参数的情况下将Matlab函数转换为.dll,它在.Net上工作得很好。然而,当我想使用带参数函数时,我得到了一些下面的错误。这个参数基本上就是image。我像这样调用Matlab上的函数,所以我的c#代码就像这样
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的图像相同,即5312x2988。但是我在下面得到了这个错误。
... 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的方式吗?有什么问题吗?
发布于 2016-07-01 08:33:38
我意识到C#中的行对应于MWarray中的列。所以我在代码上做了一些小改动。
我使用double[,] bnew = new double[height, width];而不是double[,] bnew = new double[width, height];
我用bnew.SetValue(b, j, i);代替了bnew.SetValue(b, i, j);
有人可能会使用下面的位图到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;https://stackoverflow.com/questions/38089511
复制相似问题