简体   繁体   中英

How to convert Bitmap to MWArray(Matlab Array) in .NET

I wrote an algorithm on Matlab. Hence I wanna use it on .Net. I perfectly converted .m file to .dll for using it on .Net with Matlab Library Compiler. First, I tried converting Matlab function to .dll without any parameter and it worked well on .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); I = imread('xxx.jpg'); Detect(I); So my c# code like this

    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.

... 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? What is the problem?

I realised that rows in C# correspond to columns in MWarray. So I change small things on the code.

Instead of double[,] bnew = new double[width, height]; I use it double[,] bnew = new double[height, width];

and instead of bnew.SetValue(b, i, j); I use it bnew.SetValue(b, j, i);

Someone may use the whole code about Bitmap to MWArray below

        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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM