Use C # GDAL to read the complex image

Source: Internet
Author: User

Although the C # version of GDAL has not been exported to many algorithm interfaces, the interfaces in reading and writing data are basically completely exported. The ReadRaster and WriteRaster methods are used to read and write data. At the same time, the two methods are overloaded. You can directly read common data types without specifying the data type. However, it is a bit complicated for the plural type. The following is a simple description of how GDAL reads complex data.

We know that when using GDAL to read data, we use the ReadRaster function. This function is overloaded with six. The function declaration is as follows. Take Dataset ReadRaster as an example, the ReadRaster in the Band class is similar to this.

OSGeo.GDAL.Dataset.ReadRaster(System.Int32, System.Int32, System.Int32, System.Int32, System.Byte[], System.Int32, System.Int32, System.Int32, System.Int32[], System.Int32, System.Int32, System.Int32);OSGeo.GDAL.Dataset.ReadRaster(System.Int32, System.Int32, System.Int32, System.Int32, System.Int16[], System.Int32, System.Int32, System.Int32, System.Int32[], System.Int32, System.Int32, System.Int32);OSGeo.GDAL.Dataset.ReadRaster(System.Int32, System.Int32, System.Int32, System.Int32, System.Int32[], System.Int32, System.Int32, System.Int32, System.Int32[], System.Int32, System.Int32, System.Int32);OSGeo.GDAL.Dataset.ReadRaster(System.Int32, System.Int32, System.Int32, System.Int32, System.Single[], System.Int32, System.Int32, System.Int32, System.Int32[], System.Int32, System.Int32, System.Int32);OSGeo.GDAL.Dataset.ReadRaster(System.Int32, System.Int32, System.Int32, System.Int32, System.Double[], System.Int32, System.Int32, System.Int32, System.Int32[], System.Int32, System.Int32, System.Int32);OSGeo.GDAL.Dataset.ReadRaster(System.Int32, System.Int32, System.Int32, System.Int32, System.IntPtr, System.Int32, System.Int32, OSGeo.GDAL.DataType, System.Int32, System.Int32[], System.Int32, System.Int32, System.Int32);
The declarations of the first five functions are basically the same. Except for the fifth parameter, they are Byte [], Int16 [], Int32 [], Single [], and Double []. The corresponding values are 8U, 16U \ 16 S, 32U \ 32 S, 32F, and 64F. If the image data is common data (except the complex image), these five functions can fully meet all requirements. However, if the image is a complex image (the pixel value of the image is composed of a complex number, such as an image after Fourier transformation), then, the above five functions are a little helpless, and we need the sixth function to be used for emergency.

The statement of the sixth function is exactly the same as that of the C \ C ++ statement. You should be familiar with this interface when using C \ C ++. This function can be used instead of the preceding five functions. However, when it is troublesome, there will be more input parameters. First, let's look at an example. Use a common method to read 8 U data in a single band (A Landsat5 data ). Read 100x100 data from the data and directly use the Byte interface of ReadRaster.

Static void ReadRaster () {string strFile = @ "D: \ Data \ landsat \ etp139r26_5t19900902 \ p139r26_5t19900902_nn1.tif"; OSGeo. GDAL. gdal. allRegister (); OSGeo. GDAL. dataset ds = OSGeo. GDAL. gdal. open (strFile, OSGeo. GDAL. access. GA_ReadOnly); int [] bandmap = new int [1]; bandmap [0] = 1; // read Byte [] data = new Byte [100*100]; OSGeo. GDAL. CPLErr err = ds. readRaster (3000,200 0, 100,100, data, 100,100, 1, bandmap, 0, 0, 0); // output the read data for (int I = 0; I <100; I ++) {for (int j = 0; j <100; j ++) {Console. writeLine ("{0}", ddata [I * 100 + j]) ;}} Console. writeLine ("done \ n ");}
Let's take a look at how to use the sixth interface to complete the above functions. The Code is as follows:

Static void ReadRaster () {string strFile = @ "D: \ Data \ landsat \ etp139r26_5t19900902 \ p139r26_5t19900902_nn1.tif"; OSGeo. GDAL. gdal. allRegister (); OSGeo. GDAL. dataset ds = OSGeo. GDAL. gdal. open (strFile, OSGeo. GDAL. access. GA_ReadOnly); int [] bandmap = new int [1]; bandmap [0] = 1; // read Byte [] data = new Byte [100*100]; // construct an IntPtr with a size of 100 × 100 ByteIntPtr = Marshal. allocCoTaskMem (data. length); // specifies the type of data to be read. GDAL. CPLErr err = ds. readRaster (3000,200 0, 100,100, ptr, 100,100, OSGeo. GDAL. dataType. GDT_Byte, 1, bandmap, 0, 0, 0); // copy the read data to Marshal. copy (ptr, data, 0, data. length); // output the read data for (int I = 0; I <100; I ++) {for (int j = 0; j <100; j ++) {Console. writeLine ("{0}", ddata [I * 100 + j]) ;}} Console. writeLine ("done \ n ");}
From the code above, we can see that when using the sixth function, an IntPtr type is required, use the AllocCoTaskMem function in the Marshal class to allocate memory space (I don't know what the C # statement is. C \ C ++ should be used to allocate space ). Note that the allocated size is measured in bytes. Next, you need to specify the data type when calling ReadRaster. After calling, the pixel values should all be stored in this IntPtr, then, use the Copy function in the Marshal class to Copy the data in IntPtr to the Byte array data.

The code above shows the general usage of the sixth ReadRaster. Next, we use this method to read an image of the plural type. If there is no plural image, ENVI can be used to open an image at will, and then there is a FFT under the Transform menu to perform Fourier transformation of the opened data. The output result is a plural image. Here, I use FFT to convert the Landsat data. Use the following code.

Static void ReadRaster () {string strFile = @ "D: \ Data \ landsat \ etp139r26_5t19900902 \ p139r26_5t19900902_nn1_fft.tif"; OSGeo. GDAL. gdal. allRegister (); OSGeo. GDAL. dataset ds = OSGeo. GDAL. gdal. open (strFile, OSGeo. GDAL. access. GA_ReadOnly); int [] bandmap = new int [1]; bandmap [0] = 1; // read the complex type. Because a complex number is composed of two data types, therefore, to read a pixel of 100x100, it must be 2 times the size of the common image. double [] data = new double [100*100*2]; // construct an IntPtr, the size is 100 × 100 × 2 double, and a double is 4 bytes. Therefore, the following length must be multiplied by 8 IntPtr ptr = Marshal. allocCoTaskMem (data. length * 8); // specifies the type of data to be read, OSGeo. GDAL. CPLErr err = ds. readRaster (0, 0,100,100, ptr, 100,100, OSGeo. GDAL. dataType. GDT_CFloat64, 1, bandmap, 0, 0, 0); // copy the read data to Marshal. copy (ptr, data, 0, data. length * 8); // output int in1 = 0; for (int I = 0; I <100; I ++) {for (int j = 0; j <100; j ++) {Console. writeLine ("({0} * I + {1})", ddata [in1 ++], ddata [in1 ++]) ;}} Console. writeLine ("done \ n ");}
In the code above, a plural number is composed of real and virtual parts, so a pixel value corresponds to two pixel values of a common image, therefore, to read 100x100 data, you need to allocate 2 times the size of a common image. In addition, it is the same as reading a common image. However, when the pixel value is obtained at the end, the pixel values of the complex image are stored sequentially, that is, the real part, the virtual part, the real part, and the virtual part... in this order. The first five pixel values of the code above are shown in:


Use the ENVI software to view the first five pixel values of the image, as shown in (the red box in the figure is the pixel value). You can see that the read real and virtual parts are exactly the same. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD48cD48aW1nIHNyYz0 = "http://www.2cto.com/uploadfile/Collfiles/20140219/20140219093641418.jpg" alt = ""/>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.