Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C#

Convert the image to float[][][]/float[][,]/T[][][]/T[][,] data , fast and safety

Rate me:
Please Sign up or sign in to vote.
4.56/5 (18 votes)
18 May 2012CPOL4 min read 204.8K   1.9K   32   18
I support a group methods to load the image to double[,]/double[][,]/T[][,], that could help to manipulate the image

This provide a way to convert the image to float[][][]/T[][][] data fast and safe.

Background 

In the image process field, there is an obstacle between image data and algorithms. In the lab, the mathematician and algorithm engineer are good at matrix processing. For those scientist, the image is just a matrix. So they need not to understand the Bitmap Class. Further more, the mathematical tools such as MathNet.Numerics is design to deal with double[] or T[]data type. 

For example, the GDI+ could enlarge a image easily, but if you want to use some advance interpolation method to calculate the interpolated pixel value, the GDI+ seems helpless.

So, here I support this library to convert the Image to float[][][]/float[][,]/T[][][]/T[][,] type. And the algorithm engineers could focus on the algorithm, not "how to load the image correctly". 

Meanwhile, sometimes, I found some low efficiency code in our lab, such as GetPixel() and SetPixel(). So I think I need to provide a solution for connect Matrix and Image. 

Overview 

This class library do two things: 1. Convert image to Array. 2. Convert Array to Image.

Generally, the image could be classified in two type: a) monochromatic image; b) chromatic image. 

A  monochromatic image could be defined with a 2D matrix: float[Width][Height]/float[Width,Height] or T[Width][Height]/T[Width,Height].

A  chromatic image could be defined with a 3D matrix:  <code>float[R/G/B][Width][Height]/[R/G/B]float[Width,Height] or T[R/G/B][Width][Height]/T[R/G/B][Width,Height].  

Here I support those 4 data type. 

Using the code 

After building the single cs file (DoubleToImageMethod.cs), you will get a DLL file. Add a reference for this DLL. 

C#
string fileName = "test.bmp";
float[][][] image;
//here we god the image matrix
image= DoubleToImageMethods.LoadFromFile(fileName);
// ...Do something with this matrix, for example, pattern recognize 

 
Now you've three bands from test.bmp.  

image[0] is the red band, image[1] is the green band, image[2] is the blue band. Maybe you could do something with the image. And after you have finished your job, you should save the data: 

C#
<pre lang="cs">// then save the matrix as a Bmp image file
DoubleToImageMethods.SaveToBmpLinear(image, "test.bmp");  

Then you got your result. 

 

Sometimes, you need other Data Type, eg. you need a more accurate value than float, such as double. You can use this way to load the image: 

C#
string fileName = "test.bmp"; string fileName = "test.bmp";
double[][][] image;
//here we god the image matrix, and here request the double[][][] data type
image= DoubleToImageMethods.LoadFromFile<double>(fileName);
// ...Do something with this matrix  

Methods Details  

There are three methods: 

1. Load the image matrix from the file 

the v2.0 unified the load method, the way to load image are show blow. It load the image file as T[][][]/T[][,] matrix. 

a.

C#
T[][][]  LoadFromFile<T>(string fileName) 

and 

C#
T[][,] LoadFromFile2DArray<T>(string fileName) 

I defined the PixelFormat.Format24bppRgb, so I always get the RGB band of the image, even if the image file is 8bit gray scale image. If you load the monochromatic image file, the 3 bands has the same value.

2. Save the T[][] /T[,]/ T[][,] /T[][][]data to the image file

a

C#
void SaveToBmpLinear<T>(T[][,] image, string fileName) 
void SaveToBmpLinear<T>(T[][][] image, string fileName)   

save the T[][,]/T[][][] image to RGB chrome 24bit bmp type image file. Before saving, it will stretch the data to 0 -255. It could promote the image's contrast.

b.

C#
void SaveToBmpLinear<T>(T[,] image, string fileName) 
void SaveToBmpLinear<T>(T[][] image, string fileName) 

save the T[,]/T[][] image to RGB chrome 24bit bmp type image file. the red, green, blue value are the same.So, it looks like a 8bit monochrome image, but it is a real 24bit bmp file. Before saving, it will stretch the data to 0 -255. It could promote the image's contrast.

c

C#
SaveToBmpNoLinear<T>(T[,] image, string fileName) 
SaveToBmpNoLinear<T>(T[][] image, string fileName)   

save the T[][]/T[,] image to RGB chrome 24bit bmp type image file. the red, green, blue value are the same.So, it looks like a 8bit monochrome image, but it is a real 24bit bmp file. But it won't stretch the color range.

d

C#
SaveToBmpNoLinear<T>(T[][,] image, string fileName) 
SaveToBmpNoLinear<T>(T[][][] image, string fileName)  

save the T[][,]/T[][][] image to RGB chrome 24bit bmp type image file.

e.

C#
Image ToImageLinear<T>(T[,] image)
Image ToImageLinear<T>(T[][] image)  

save the T[,]/T[][] to System.Drawing.Image type, with color stretch, 24bit RGB image, and seems like 8bit GrayScale Image; 

f

Image ToImageLinear<T>(T[][,] image)
Image ToImageLinear<T>(T[][][] image)   

save the T[][,]/T[][][] to System.Drawing.Image type

3.Convert Image to the T[][,]/T[][][] 

a

T[][][] LoadFromImage<T>(Image img)  

load Image data to T[R/G/B][Width][Height]. 

b

T[][,] LoadFromFile2DArray<T>(Image img) 

load Image data to T[R/G/B][Width,Height].  

Key Ways

I found that Marshal.Copy() is a kind class to solve this problem. 

ImageToDouble/keyWays.jpg

When I load or save data with bmp or jpg file, I need the BitmapData, and Marshal.Copy(), it support me a fast and safe way to load or save data.

Code Efficiency 

I use the Parallel.For() to fill the matrix, it obviously promote the program efficiency when load big image file(large than 5000*3000 pixels).

C#
Parallel.For(0, imgHeight, (int i) =>
           {
               for (int j = 0; j < imgExtentWidth; j += 3)
               {
                   if (j < imgWidth * 3)
                   {
                       linlizeImg[i * imgExtentWidth + j] = arrayData[2][j / 3][i];
                       linlizeImg[i * imgExtentWidth + j + 1] = arrayData[1][j / 3][i];
                       linlizeImg[i * imgExtentWidth + j + 2] = arrayData[0][j / 3][i];
                   }
                   else
                   {
                       linlizeImg[i * imgExtentWidth + j] = linlizeImg[i * imgExtentWidth + j - 1];
                   }
               }
           });

History  

  • 2014 Aug 24, version 2.1. Simplified the entire code, removed all the cloned code, and support BIG image such as 8000*8000 or even larger~
  • 2012 Apr 13, Update to .net 4.0, re-factor the  entire project, use parallel features in net40  
  • 2009 Oct 23, Update the description on the web.  
  • 2009 May 23, Updated, provide Template ways to convert the ImageData. Revised some bugs in rgbImage methods
  • 2009 May 10, Updated, revised some bugs in RGB image
  • 2009 March 11, Updated, revised 2 bugs in RGB image.. sorry.

I will try my best to debug and make it faster than before

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
China China
Satellites' Image Processing
Include:
Imaging Progress Simulation
Multispectral Image Processing
Distributed System

Comments and Discussions

 
QuestionHow to treat large image? Pin
BOLLILAND CHEN13-Aug-14 19:58
professionalBOLLILAND CHEN13-Aug-14 19:58 
AnswerRe: How to treat large image? Pin
mibxue23-Aug-14 8:40
professionalmibxue23-Aug-14 8:40 
AnswerRe: How to treat large image? Pin
mibxue23-Aug-14 8:43
professionalmibxue23-Aug-14 8:43 
AnswerRe: How to treat large image? Pin
mibxue26-Aug-14 4:54
professionalmibxue26-Aug-14 4:54 
The new version 2.1 is now online, it supports the big image~ enjoy~
GeneralMy vote of 5 Pin
Chris Maunder1-Dec-13 21:23
cofounderChris Maunder1-Dec-13 21:23 
GeneralMy vote of 4 Pin
JiangNanDeXue21-Sep-11 19:04
JiangNanDeXue21-Sep-11 19:04 
General呃...友情踩一下... Pin
chaircat26-Oct-09 16:18
chaircat26-Oct-09 16:18 
Generalgood article ! Pin
BillWoodruff23-Oct-09 19:49
professionalBillWoodruff23-Oct-09 19:49 
Generalgood job Pin
Rozis23-Oct-09 14:31
Rozis23-Oct-09 14:31 
GeneralRe: good job Pin
mibxue23-Oct-09 16:56
professionalmibxue23-Oct-09 16:56 
GeneralMy vote of 1 Pin
Toli Cuturicu6-Oct-09 10:39
Toli Cuturicu6-Oct-09 10:39 
GeneralRe: My vote of 1 Pin
mibxue23-Oct-09 1:04
professionalmibxue23-Oct-09 1:04 
General很好,很强大:) Pin
qinshaohua6-Aug-09 18:58
qinshaohua6-Aug-09 18:58 
GeneralLarge Resuls File Pin
Laserson22-May-09 21:16
Laserson22-May-09 21:16 
GeneralRe: Large Resuls File Pin
mibxue23-May-09 1:16
professionalmibxue23-May-09 1:16 
GeneralRe: Large Resuls File Pin
Rozis23-Oct-09 14:36
Rozis23-Oct-09 14:36 
GeneralRe: Large Resuls File Pin
mibxue23-Oct-09 16:55
professionalmibxue23-Oct-09 16:55 
GeneralNice Pin
stixoffire22-May-09 4:04
stixoffire22-May-09 4:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.