GDAL 遥感图像处理后的数据保存为图像文件的实现方法
2013-09-18 19:54
621 查看
在遥感图像处理中,GDAL库不仅能读取和处理大部分的遥感图像数据,而且还能够实现图像处理后将数据保存为图像的功能。
本文就详细介绍如何将内存中的图像数据保存为.tif格式。
首先,遥感数据处理完,保存在一维数组data中,图像的宽和高为width和height,图像有三个波段。在保存时要使图像的每一行对其,保证位数为32的倍数
先上实现的代码,源程序如下:
这里关键的就是Create和RasterIO两个函数
Create函数的功能为创建一个文件,创建成功就返回一个GDALDataSet类指针对象,然后再使用这个指针对象调用RasterIO向文件内写数据。Create函数的原型:
Parameters:
需要注意的是nXSize和nXSize均是值像素的个数,而非字节个数,所以程序中是width和height。文件创建好了,里面数据是空的,下面就往文件里面写图像数据
RasterIO是GDALDataSet类的方法,功能是写数据,函数原型如下:
Parameters:
这里的图像数据宽度和高度均是指图像像素点的个数,所以都是width和height。要特别注意最后四个参数。
int* panBandMap:波段的排列顺序,比如RGB,BGR等
int nPixelSpace:写入时相邻像素间的字节数大小,这里每个像素三个波段,因此为3
int nLineSpace:图像相邻行见字节数的大小,这里一行又bytePerLine个字节,因此为bytePerLine
int nBandSpace:写入时相邻波段见字节大小,这里写入的波段都是紧挨着的,因此为1
本文就详细介绍如何将内存中的图像数据保存为.tif格式。
首先,遥感数据处理完,保存在一维数组data中,图像的宽和高为width和height,图像有三个波段。在保存时要使图像的每一行对其,保证位数为32的倍数
先上实现的代码,源程序如下:
int bytesPerLine=(width*24+31)/8;//字节对齐 unsigned char *data=new unsigned char[bytesPerLine*height]; //图像处理...... GDALAllRegister();//注册数据集 GDALDriver *poDriver; GDALDataset *BiomassDataset; poDriver = GetGDALDriverManager()->GetDriverByName("Gtiff"); const char *output_file="D:\xxxx"; BiomassDataset=poDriver->Create(output_file,width,height,3,GDT_Byte, NULL); int panBandMap [3]= {1,2,3}; BiomassDataset->RasterIO(GF_Write,0,0,width,height,data_show_,width,height,GDT_Byte,3,panBandMap,3,bytesPerLine,1); GDALClose(BiomassDataset); BiomassDataset=NULL; delete []data; 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. |
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. |
int* panBandMap:波段的排列顺序,比如RGB,BGR等
int nPixelSpace:写入时相邻像素间的字节数大小,这里每个像素三个波段,因此为3
int nLineSpace:图像相邻行见字节数的大小,这里一行又bytePerLine个字节,因此为bytePerLine
int nBandSpace:写入时相邻波段见字节大小,这里写入的波段都是紧挨着的,因此为1
相关文章推荐
- 算法第三次作业(1. 排序。对文件 largeW.txt(下载链接)中的数据,编程实现冒泡排序(方法名:bubbleSort) 与 归并排序(mergeSort),把排序后的结果分别保存到largeW
- MFC 实现保存数据为excel文件格式用那种方法好?
- matlab批处理多个文件数据及保存多个输出图像的方法及例子
- Java实现从数据库导出大量数据记录并保存到文件的方法(转)
- Java实现从数据库导出大量数据记录并保存到文件的方法
- c#实现数据同步的方法(使用文件监控对象filesystemwatcher)
- 数据库保存数据的两种实现方法
- 一种图像索引的查找表实现方法(VisualSFM点云数据存储基准从物方到像方的转换)
- MSSQL 监控数据/日志文件增长实现方法
- kettle实现文本文件数据抽取方法
- php实现编辑和保存文件的方法
- python使用PyGame绘制图像并保存为图片文件的方法
- 获取本地路径的方法,在沙河路径中保存图片 ,将保存文件到沙河路径下 把图片转换成NSData类型的数据来保存文件
- Python数据处理-将数据保存为txt、csv等文件格式方法
- php实现将wav文件转换成图像文件并在页面中显示的方法
- PHP实现格式化文件数据大小显示的方法
- C++读取WAV音频文件的头部数据的实现方法
- JSP实现远程文件下载保存到服务器指定目录中的方法
- java web中servlet实现post方法上传多文件数据接收
- Opengl绘制数组数据与文件数据的方法与Nvidia opengl sdk辅助实现