在遙感圖像處理中,GDAL庫不只能讀取和處理大部分的遙感圖像數據,並且還可以實現圖像處理後將數據保存爲圖像的功能。html
本文就詳細介紹如何將內存中的圖像數據保存爲.tif格式。web
首先,遙感數據處理完,保存在一維數組data中,圖像的寬和高爲width和height,圖像有三個波段。在保存時要使圖像的每一行對其,保證位數爲32的倍數數組
先上實現的代碼,源程序以下:ide
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函數的原型:spa
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。文件建立好了,裏面數據是空的,下面就往文件裏面寫圖像數據code
RasterIO是GDALDataSet類的方法,功能是寫數據,函數原型以下:orm
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:htm
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