Romi-知行合一

轻轻的风轻轻的梦,轻轻的晨晨昏昏, 淡淡的云淡淡的泪,淡淡的年年岁岁。
  博客园  :: 首页  :: 新随笔  :: 订阅 订阅  :: 管理

GDAL 遥感图像处理后的数据保存为图像文件的实现方法

Posted on 2013-09-18 19:54  romi  阅读(8861)  评论(1编辑  收藏  举报

在遥感图像处理中,GDAL库不仅能读取和处理大部分的遥感图像数据,而且还能够实现图像处理后将数据保存为图像的功能。

 本文就详细介绍如何将内存中的图像数据保存为.tif格式。

 首先,遥感数据处理完,保存在一维数组data中,图像的宽和高为width和height,图像有三个波段。在保存时要使图像的每一行对其,保证位数为32的倍数

 先上实现的代码,源程序如下:

 1 int bytesPerLine=(width*24+31)/8;//字节对齐  
 2 unsigned char *data=new unsigned char[bytesPerLine*height];  
 3   
 4 //图像处理......  
 5   
 6 GDALAllRegister();//注册数据集  
 7 GDALDriver *poDriver;  
 8     GDALDataset *BiomassDataset;  
 9     poDriver = GetGDALDriverManager()->GetDriverByName("Gtiff"); 
10  
11 const char *output_file="D:\xxxx";  
12 
13 BiomassDataset=poDriver->Create(output_file,width,height,3,GDT_Byte, NULL);  
14 
15 int panBandMap [3]= {1,2,3};  
16 BiomassDataset->RasterIO(GF_Write,0,0,width,height,data_show_,width,height,GDT_Byte,3,panBandMap,3,bytesPerLine,1);  
17   
18 GDALClose(BiomassDataset);  
19 BiomassDataset=NULL;  
20   
21   
22 delete []data;  
23 data=NULL;  

这里关键的就是Create和RasterIO两个函数

 Create函数的功能为创建一个文件,创建成功就返回一个GDALDataSet类指针对象,然后再使用这个指针对象调用RasterIO向文件内写数据。Create函数的原型:

GDALDataset * GDALDriver::Create    (   const char *    pszFilename,  
int     nXSize,  
int     nYSize,  
int     nBands,  
GDALDataType    eType,  
char **     papszOptions       
)     

Parameters:

  pszFilename  the name of the dataset to create. UTF-8 encoded.
  nXSize  width of created raster in pixels.
  nYSize  height of created raster in pixels.
  nBands  number of bands.
  eType  type of raster.
  papszOptions  list of driver specific control parameters.

 

 

 

 

 

 

需要注意的是nXSize和nXSize均是值像素的个数,而非字节个数,所以程序中是width和height。文件创建好了,里面数据是空的,下面就往文件里面写图像数据

 RasterIO是GDALDataSet类的方法,功能是写数据,函数原型如下:

 

CPLErr GDALDataset::RasterIO    (   GDALRWFlag  eRWFlag,  
int     nXOff,  
int     nYOff,  
int     nXSize,  
int     nYSize,  
void *  pData,  
int     nBufXSize,  
int     nBufYSize,  
GDALDataType    eBufType,  
int     nBandCount,  
int *   panBandMap,  
int     nPixelSpace,  
int     nLineSpace,  
int     nBandSpace     
)     

 Parameters:

  eRWFlag  Either GF_Read to read a region of data, or GF_Write to write a region of data.
  nXOff  The pixel offset to the top left corner of the region of the band to be accessed. This would be zero to start from the left side.
  nYOff  The line offset to the top left corner of the region of the band to be accessed. This would be zero to start from the top.
  nXSize  The width of the region of the band to be accessed in pixels.
  nYSize  The height of the region of the band to be accessed in lines.
  pData  The buffer into which the data should be read, or from which it should be written. This buffer must contain at least nBufXSize * nBufYSize * nBandCount words of type eBufType. It is organized in left to right,top to bottom pixel order. Spacing is controlled by the nPixelSpace, and nLineSpace parameters.
  nBufXSize  the width of the buffer image into which the desired region is to be read, or from which it is to be written.
  nBufYSize  the height of the buffer image into which the desired region is to be read, or from which it is to be written.
  eBufType  the type of the pixel values in the pData data buffer. The pixel values will automatically be translated to/from the GDALRasterBand data type as needed.
  nBandCount  the number of bands being read or written.
  panBandMap  the list of nBandCount band numbers being read/written. Note band numbers are 1 based. This may be NULL to select the first nBandCount bands.
  nPixelSpace  The byte offset from the start of one pixel value in pData to the start of the next pixel value within a scanline. If defaulted (0) the size of the datatype eBufType is used.
  nLineSpace  The byte offset from the start of one scanline in pData to the start of the next. If defaulted (0) the size of the datatype eBufType * nBufXSize is used.
  nBandSpace  the byte offset from the start of one bands data to the start of the next. If defaulted (0) the value will be nLineSpace * nBufYSize implying band sequential organization of the data buffer.

这里的图像数据宽度和高度均是指图像像素点的个数,所以都是width和height。要特别注意最后四个参数。

int* panBandMap:波段的排列顺序,比如RGB,BGR等

int nPixelSpace:写入时相邻像素间的字节数大小,这里每个像素三个波段,因此为3

int nLineSpace:图像相邻行见字节数的大小,这里一行又bytePerLine个字节,因此为bytePerLine

int nBandSpace:写入时相邻波段见字节大小,这里写入的波段都是紧挨着的,因此为1