ENVI顯示GDAL建立GeoTiff檔案的一個問題及其思考
阿新 • • 發佈:2019-01-10
作者:朱金燦
使用gdal建立一個100*100的紅色的geotiff影象,程式碼如下:
#include <assert.h> #include <string> #include <gdal.h> #include <gdal_priv.h> #include <gdalwarper.h> int _tmain(int argc, _TCHAR* argv[]) { // 用於支援中文路徑 CPLSetConfigOption(_T("GDAL_FILENAME_IS_UTF8"),_T("NO")); GDALAllRegister(); GDALDriver* pDriver = GetGDALDriverManager()->GetDriverByName(_T("GTiff")); if(NULL!=pDriver) { // 建立的geotif檔案路徑 std::string strFileFullPath = "D:\\1.tif"; // 下面是預設選項 char** papszOptions = NULL; papszOptions = CSLSetNameValue( papszOptions, "INTERLEAVE", "BAND" ); //bsq int nImgWidth = 100; // 影象寬度 int nImgHeight = 100; // 影象高度 //建立位浮點數的geotif影象 GDALDataset* pCreateDataset = pDriver->Create(strFileFullPath.c_str(),nImgWidth,nImgHeight,3,GDT_Byte,papszOptions); if(pCreateDataset != NULL) { // 定義仿射變換引數 double dblGeoTransform[6] = {0}; /* dblGeoTransform[0] --- 影象左上角的橫座標 dblGeoTransform[1] --- 單個像元寬度,使用影象x軸範圍/影象寬度得到 dblGeoTransform[2] --- 0.0 dblGeoTransform[3] --- 影象左上角的縱座標 dblGeoTransform[4] --- 0.0 dblGeoTransform[5] --- 單個像元高度,使用影象y軸範圍/影象高度得到 */ dblGeoTransform[0] = 116.0; // 東經度 dblGeoTransform[1] = (120.0-116.0)/static_cast<double>(nImgWidth); // 影象的地理範圍為東經度到東經度 dblGeoTransform[3] = 39.0; // 北緯度 dblGeoTransform[5] = (37.0-39.0)/static_cast<double>(nImgHeight);// 影象的地理範圍為北緯度到北緯度 CPLErr err = CE_None; if(CE_None == pCreateDataset->SetGeoTransform(dblGeoTransform)) { // 設定經緯度座標 std::string strWkt = "GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0],UNIT[\"degree\",0.0174532925199433],AUTHORITY[\"EPSG\",\"4326\"]]"; if(CE_None == pCreateDataset->SetProjection(strWkt.c_str())) { GDALRasterBand* poBand = NULL; poBand = pCreateDataset->GetRasterBand(1); unsigned char* pcBuffer = new unsigned char[nImgWidth*nImgHeight]; memset(pcBuffer,255,nImgWidth*nImgHeight*sizeof(unsigned char)); assert(NULL!=poBand); // 將第一波段的值全部設為.0 err = poBand->RasterIO(GF_Write,0,0,nImgWidth,nImgHeight,pcBuffer,nImgWidth,nImgHeight,GDT_Byte,0,0); // GF_Read } } GDALClose(pCreateDataset); } } GDALDestroyDriverManager(); getchar(); return 0; }
用ENVI ZOOM開啟時居然是全黑,如下圖:
開始百思不得其解,後來大致明白了,ENVI初始顯示是作了2%線性拉伸的,就是對影象DN值分佈在2%和98%之間的做線性拉伸,由於所有波段的最大值和最小值都一樣,沒有拉伸空間,那麼就是保留初始化值0,所以就顯示為0。
就是如果不進行拉伸的話,是可以正確顯示的,如下圖:
參考: