Как конвертировать Matlab в C #, когда выходной массив? - PullRequest
1 голос
/ 12 июня 2019

я пытаюсь взять алгоритм прибоя в matlab и преобразовать его в c #.Алгоритм в Matlab возвращает массив координат.Размер массива составляет [10,4].В C # я написал код, который не возвращает нужную информацию в массиве.Я что-то пропустил, когда преобразовал этот код?

    private static void Main(string[] args)
    {
        Bitmap img1 = new Bitmap(Image.FromFile(@"C:\Users\cbencham\source\repos\3.jpg"));
        Bitmap img2 = new Bitmap(Image.FromFile(@"C:\Users\cbencham\source\repos\4.jpg"));

        //Get image dimensions
        int width = img1.Width;
        int height = img1.Height;
        //Declare the double array of grayscale values to be read from "bitmap"
        double[,] im1 = 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 = img1.GetPixel(i, j);
                double b = pixelColor.GetBrightness(); //the Brightness component
                                                       //Note that rows in C# correspond to columns in MWarray
                im1.SetValue(b, i, j);
            }
        }
        //Get image dimensions
        width = img2.Width;
        height = img2.Height;
        //Declare the double array of grayscale values to be read from "bitmap"
        double[,] im2 = new double[width, height];
        //Loop to read the data from the Bitmap image into the double array
        for (i = 0; i < width; i++)
        {
            for (j = 0; j < height; j++)
            {
                Color pixelColor = img2.GetPixel(i, j);
                double b = pixelColor.GetBrightness(); //the Brightness component
                                                       //Note that rows in C# correspond to columns in MWarray
                im2.SetValue(b, i, j);
            }
        }

        MLApp.MLApp matlab = new MLApp.MLApp();
        matlab.Execute(@"cd C:\Users\cbencham\source\repos");
        object result = null;
        matlab.Feval("surf", 1, out result, im1, im2);

        // TODO: convert result to double Array [10,4]


        for (i = 0; i < 10; i++)
        {
            for (j = 0; j < 4; j++)
            {
                Console.Write(arr[i, j]);
            }
            Console.WriteLine();
        }
        Console.ReadLine();

    }

}

}

1 Ответ

0 голосов
/ 13 июня 2019

Вы не можете преобразовать массив объектов в двойной массив. Но для вашей цели вам не нужно. Вам просто нужно привести его к объектному массиву и распечатать его содержимое.

var arr = result as object[,];

или

if (result is object[,] arr)
{    
    for (i = 0; i < arr.GetLength(0); i++)
    {
        for (j = 0; j < arr.GetLength(1); j++)
        {
            Console.Write(arr[i, j]);
        }
        Console.WriteLine();
    }
    Console.ReadLine();
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...