[英]How to convert Bitmap to MWArray(Matlab Array) in .NET
I wrote an algorithm on Matlab. 我在Matlab上写了一个算法。 Hence I wanna use it on .Net.
因此,我想在.Net上使用它。 I perfectly converted .m file to .dll for using it on .Net with Matlab Library Compiler.
我将.m文件完美转换为.dll,以便通过Matlab库编译器在.Net上使用它。 First, I tried converting Matlab function to .dll without any parameter and it worked well on .Net.
首先,我尝试将Matlab函数转换为不带任何参数的.dll,并且在.Net上运行良好。 However, when I wanna use that function with parameter, I'm getting some error below.
但是,当我想将该函数与参数一起使用时,下面出现一些错误。 The parameter is basically image.
该参数基本上是图像。 I call the function on Matlab like this
I = imread('xxx.jpg'); Detect(I);
我像这样在Matlab上调用函数
I = imread('xxx.jpg'); Detect(I);
I = imread('xxx.jpg'); Detect(I);
So my c# code like this 所以我的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);
}
}
I used the same image that I called xxx.jpg which is 5312 x 2988. But I'm getting this error below. 我使用了我称为xxx.jpg的相同图像,即5312 x2988。但是我在下面遇到此错误。
... 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.
The important thing is, it says "Index exceeds matrix dimensions", is it true way converting Bitmap to MWArray? 重要的是,它说“索引超出矩阵尺寸”,将位图转换为MWArray的方法是否正确? What is the problem?
问题是什么?
I realised that rows in C# correspond to columns in MWarray. 我意识到C#中的行与MWarray中的列相对应。 So I change small things on the code.
因此,我在代码上更改了一些小东西。
Instead of double[,] bnew = new double[width, height];
代替
double[,] bnew = new double[width, height];
I use it double[,] bnew = new double[height, width];
我用它
double[,] bnew = new double[height, width];
and instead of bnew.SetValue(b, i, j);
而不是
bnew.SetValue(b, i, j);
I use it bnew.SetValue(b, j, i);
我用它
bnew.SetValue(b, j, i);
Someone may use the whole code about Bitmap to MWArray below 有人可能会在下面将有关位图的整个代码用于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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.