在遙感圖像處理中,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