首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在.NET中将位图转换为MWArray(Matlab Array)

问如何在.NET中将位图转换为MWArray(Matlab Array)
EN

Stack Overflow用户
提问于 2016-06-29 10:46:24
回答 1查看 696关注 0票数 0

我在Matlab上写了一个算法。因此,我想在.Net上使用它。我完美地将.m文件转换为.dll,以便使用Matlab Library Compiler在.Net上使用它。首先,我尝试在没有任何参数的情况下将Matlab函数转换为.dll,它在.Net上工作得很好。然而,当我想使用带参数函数时,我得到了一些下面的错误。这个参数基本上就是image。我像这样调用Matlab上的函数,所以我的c#代码就像这样

代码语言:javascript
复制
    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。但是我在下面得到了这个错误。

代码语言:javascript
复制
... 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的方式吗?有什么问题吗?

EN

回答 1

Stack Overflow用户

发布于 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的全部代码

代码语言:javascript
复制
        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;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38089511

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档