Convert .NET Image to 1/3D double array
I have a requirement that convert a .net image to 3D(or 1D) double array for MATLAB use, but didn't find any related article about this. So I reference this to do that.
This is my code(image to 1D array):
Bitmap bitmap = new Bitmap(image);
double[] imagearray = new double[image.Height * image.Width * image.Height * 3];
int index = 0;
//In matlab matrix, row first
for (int i = 0; i < bitmap.Width; i++) //column
{
for (int j = 0; j < bitmap.Height; j++, index++) //row
{
Color pixelColor = bitmap.GetPixel(i, j); //getpixel(x,y)
imagearray[index] = pixelColor.R;
imagearray[index] = pixelColor.G;
imagearray[index] = pixelColor.B;
}
}
This is my code(image to 3D array):
Bitmap bitmap = new Bitmap(image);
double[,,] imagearray = new double[image.Height, image.Width, 3];
//return;
//In matlab matrix, row first
for (int i=0; i < bitmap.Width; i++) //column
{
for (int j = 0; j < bitmap.Height; j++) //row
{
Color pixelColor = bitmap.GetPixel(i, j); //getpixel(x,y)
imagearray[j, i, 0] = pixelColor.R;
imagearray[j, i, 1] = pixelColor.G;
imagearray[j, i, 2] = pixelColor.B;
}
}
So, you can convert image to n-D array at the same way.