归一化是图像数据预处理的常用手段,然而Tiff图像存在多/单波段类型,且数据格式多样化,这为数据处理带来了不少的麻烦。为了便于后期的工作,笔者使用C++语言基于GDAL外部库处理Tiff图像,实现Tiff图像的归一化功能。
然而,GByte或GDT_Int16等数据类型归一化为原有数据类型后,存在像素值存在只为0或1的现象,不具有研究意义。因此,笔者进一步改良了算法,将转化后的数据统一为GDT_Float32或GDT_Float64,更具实际意义。
效果:
代码如下:
//指定转化为GDT_Float32或GDT_Float64
//initialTiff:待处理图像路径 newTiff:输出图像路径 DoubleorFloat:输出图像数据类型
输出图像数据类型
void normalizedtoDoubleorFloatTiff(std::string initialTiff, std::string newTiff, GDALDataType DoubleorFloat)
{
if (DoubleorFloat != GDT_Float32 && DoubleorFloat != GDT_Float64)
{
cout << "Only support GDT_Float32 or GDT_Float64";
return;
}
GDALAllRegister();
//设置支持中文路径
CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
const char * pszFile = initialTiff.c_str();
GDALDataset *poDataset = (GDALDataset*)GDALOpen(pszFile, GA_ReadOnly);//使用只读方式打开图像
if (!poDataset)
{
printf("File: %s不能打开!\n", pszFile);
}
//仿射参数
double padfTransform0[6];
if (poDataset->GetGeoTransform(padfTransform0) == CE_Failure)
{
printf("获取仿射变换参数失败");
}
int iImgSizeX0 = poDataset->GetRasterXSize();
int iImgSizeY0 = poDataset->GetRasterYSize();
int nCount = poDataset->GetRasterCount(); //影像的波段数
GDALDataType gdal_data_type = poDataset->GetRasterBand(1)->GetRasterDataType();//获取栅格类型
double dnodata = poDataset->GetRasterBand(1)->GetNoDataValue();//获取空值对应大小
int *panBandMap = new int[nCount];
for (int i = 0; i < nCount; i++)
{
panBandMap[i] = i + 1;
}
switch (gdal_data_type)
{
case GDT_Byte:
{
unsigned char *pUC_afScanblock1 = new unsigned char[iImgSizeX0*iImgSizeY0 * nCount];
double *_pdValue = new double[iImgSizeX0*iImgSizeY0 * nCount];
float *_pfValue = new float[iImgSizeX0*iImgSizeY0 * nCount];
poDataset->RasterIO(GF_Read, 0, 0, iImgSizeX0, iImgSizeY0, pUC_afScanblock1, iImgSizeX0, iImgSizeY0, gdal_data_type, nCount, panBandMap, 0, 0, 0);
vector<double>vMax;
for (int i = 0; i < nCount; i++)
{
//获取每个波段最大值
double dMax = -999999999999999;
for (int j = 0; j < iImgSizeX0*iImgSizeY0; j++)
{
_pdValue[i*iImgSizeX0*iImgSizeY0 + j] = (double)pUC_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j];
_pfValue[i*iImgSizeX0*iImgSizeY0 + j] = (float)pUC_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j];
if (dMax < (double)pUC_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] && abs((double)pUC_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] - dnodata)>0.000001)
{
dMax = (double)pUC_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j];
}
}
vMax.push_back(dMax);
}
for (int i = 0; i < nCount; i++)
{
for (int j = 0; j < iImgSizeX0*iImgSizeY0; j++)
{
if (abs((double)pUC_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] - dnodata) > 0.000001)
{
_pdValue[i*iImgSizeX0*iImgSizeY0 + j] = (double)pUC_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] / vMax[i];
_pfValue[i*iImgSizeX0*iImgSizeY0 + j] = (float)pUC_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] / vMax[i];
}
}
}
vMax.clear();
if (DoubleorFloat == GDT_Float32)
{
//写入新的tif
const char* pszDstFilename = newTiff.c_str();
GDALDriver* poDriver = GetGDALDriverManager()->GetDriverByName("GTiff");
GDALDataset* poDstDS = poDriver->Create(pszDstFilename, iImgSizeX0, iImgSizeY0, nCount, GDT_Float32, NULL);
poDstDS->SetProjection(poDataset->GetProjectionRef());//给它设置投影
poDstDS->SetGeoTransform(padfTransform0);//给设置空间转换的六参数
for (int i = 0; i < nCount; i++)
{
poDstDS->GetRasterBand(i + 1)->SetNoDataValue(dnodata);//将空值设置为“无数据值”
}
//保存全波段影像
poDstDS->RasterIO(GF_Write, 0, 0, iImgSizeX0, iImgSizeY0, _pfValue, iImgSizeX0, iImgSizeY0, GDT_Float32, nCount, panBandMap, 0, 0, 0);
//释放内存
GDALClose(poDstDS);
GDALClose(poDataset);
}
else if (DoubleorFloat == GDT_Float64)
{
//写入新的tif
const char* pszDstFilename = newTiff.c_str();
GDALDriver* poDriver = GetGDALDriverManager()->GetDriverByName("GTiff");
GDALDataset* poDstDS = poDriver->Create(pszDstFilename, iImgSizeX0, iImgSizeY0, nCount, GDT_Float64, NULL);
poDstDS->SetProjection(poDataset->GetProjectionRef());//给它设置投影
poDstDS->SetGeoTransform(padfTransform0);//给设置空间转换的六参数
for (int i = 0; i < nCount; i++)
{
poDstDS->GetRasterBand(i + 1)->SetNoDataValue(dnodata);//将空值设置为“无数据值”
}
//保存全波段影像
poDstDS->RasterIO(GF_Write, 0, 0, iImgSizeX0, iImgSizeY0, _pdValue, iImgSizeX0, iImgSizeY0, GDT_Float64, nCount, panBandMap, 0, 0, 0);
//释放内存
GDALClose(poDstDS);
GDALClose(poDataset);
}
delete[]_pdValue;
delete[]_pfValue;
delete[]pUC_afScanblock1;
break;
}
case GDT_UInt16:
{
//unsigned short *pafScanblock1;
unsigned short *pUS_afScanblock1 = new unsigned short[iImgSizeX0*iImgSizeY0 * nCount];
double *_pdValue = new double[iImgSizeX0*iImgSizeY0 * nCount];
float *_pfValue = new float[iImgSizeX0*iImgSizeY0 * nCount];
poDataset->RasterIO(GF_Read, 0, 0, iImgSizeX0, iImgSizeY0, pUS_afScanblock1, iImgSizeX0, iImgSizeY0, gdal_data_type, nCount, panBandMap, 0, 0, 0);
vector<double>vMax;
for (int i = 0; i < nCount; i++)
{
//获取每个波段最大值
double dMax = -999999999999999;
for (int j = 0; j < iImgSizeX0*iImgSizeY0; j++)
{
_pdValue[i*iImgSizeX0*iImgSizeY0 + j] = (double)pUS_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j];
_pfValue[i*iImgSizeX0*iImgSizeY0 + j] = (float)pUS_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j];
if (dMax < (double)pUS_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] && abs((double)pUS_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] - dnodata)>0.000001)
{
dMax = (double)pUS_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j];
}
}
vMax.push_back(dMax);
}
for (int i = 0; i < nCount; i++)
{
for (int j = 0; j < iImgSizeX0*iImgSizeY0; j++)
{
if (abs((double)pUS_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] - dnodata) > 0.000001)
{
_pfValue[i*iImgSizeX0*iImgSizeY0 + j] = (float)pUS_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] / vMax[i];
_pdValue[i*iImgSizeX0*iImgSizeY0 + j] = (double)pUS_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] / vMax[i];
}
}
}
vMax.clear();
if (DoubleorFloat == GDT_Float32)
{
//写入新的tif
const char* pszDstFilename = newTiff.c_str();
GDALDriver* poDriver = GetGDALDriverManager()->GetDriverByName("GTiff");
GDALDataset* poDstDS = poDriver->Create(pszDstFilename, iImgSizeX0, iImgSizeY0, nCount, GDT_Float32, NULL);
poDstDS->SetProjection(poDataset->GetProjectionRef());//给它设置投影
poDstDS->SetGeoTransform(padfTransform0);//给设置空间转换的六参数
for (int i = 0; i < nCount; i++)
{
poDstDS->GetRasterBand(i + 1)->SetNoDataValue(dnodata);//将空值设置为“无数据值”
}
//保存全波段影像
poDstDS->RasterIO(GF_Write, 0, 0, iImgSizeX0, iImgSizeY0, _pfValue, iImgSizeX0, iImgSizeY0, GDT_Float32, nCount, panBandMap, 0, 0, 0);
//释放内存
GDALClose(poDstDS);
GDALClose(poDataset);
}
else if (DoubleorFloat == GDT_Float64)
{
//写入新的tif
const char* pszDstFilename = newTiff.c_str();
GDALDriver* poDriver = GetGDALDriverManager()->GetDriverByName("GTiff");
GDALDataset* poDstDS = poDriver->Create(pszDstFilename, iImgSizeX0, iImgSizeY0, nCount, GDT_Float64, NULL);
poDstDS->SetProjection(poDataset->GetProjectionRef());//给它设置投影
poDstDS->SetGeoTransform(padfTransform0);//给设置空间转换的六参数
for (int i = 0; i < nCount; i++)
{
poDstDS->GetRasterBand(i + 1)->SetNoDataValue(dnodata);//将空值设置为“无数据值”
}
//保存全波段影像
poDstDS->RasterIO(GF_Write, 0, 0, iImgSizeX0, iImgSizeY0, _pdValue, iImgSizeX0, iImgSizeY0, GDT_Float64, nCount, panBandMap, 0, 0, 0);
//释放内存
GDALClose(poDstDS);
GDALClose(poDataset);
}
delete[]_pdValue;
delete[]_pfValue;
delete[]pUS_afScanblock1;
break;
}
case GDT_Int16:
{
// short int *pafScanblock1;
short int*pSI_afScanblock1 = new short int[iImgSizeX0*iImgSizeY0 * nCount];
double *_pdValue = new double[iImgSizeX0*iImgSizeY0 * nCount];
float *_pfValue = new float[iImgSizeX0*iImgSizeY0 * nCount];
poDataset->RasterIO(GF_Read, 0, 0, iImgSizeX0, iImgSizeY0, pSI_afScanblock1, iImgSizeX0, iImgSizeY0, gdal_data_type, nCount, panBandMap, 0, 0, 0);
vector<double>vMax;
for (int i = 0; i < nCount; i++)
{
//获取每个波段最大值
double dMax = -999999999999999;
for (int j = 0; j < iImgSizeX0*iImgSizeY0; j++)
{
_pdValue[i*iImgSizeX0*iImgSizeY0 + j] = (double)pSI_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j];
_pfValue[i*iImgSizeX0*iImgSizeY0 + j] = (float)pSI_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j];
if (dMax < (double)pSI_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] && abs((double)pSI_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] - dnodata)>0.000001)
{
dMax = (double)pSI_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j];
}
}
vMax.push_back(dMax);
}
for (int i = 0; i < nCount; i++)
{
for (int j = 0; j < iImgSizeX0*iImgSizeY0; j++)
{
if (abs((double)pSI_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] - dnodata) > 0.000001)
{
_pfValue[i*iImgSizeX0*iImgSizeY0 + j] = (float)pSI_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] / vMax[i];
_pdValue[i*iImgSizeX0*iImgSizeY0 + j] = (double)pSI_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] / vMax[i];
}
}
}
vMax.clear();
if (DoubleorFloat == GDT_Float32)
{
//写入新的tif
const char* pszDstFilename = newTiff.c_str();
GDALDriver* poDriver = GetGDALDriverManager()->GetDriverByName("GTiff");
GDALDataset* poDstDS = poDriver->Create(pszDstFilename, iImgSizeX0, iImgSizeY0, nCount, GDT_Float32, NULL);
poDstDS->SetProjection(poDataset->GetProjectionRef());//给它设置投影
poDstDS->SetGeoTransform(padfTransform0);//给设置空间转换的六参数
for (int i = 0; i < nCount; i++)
{
poDstDS->GetRasterBand(i + 1)->SetNoDataValue(dnodata);//将空值设置为“无数据值”
}
//保存全波段影像
poDstDS->RasterIO(GF_Write, 0, 0, iImgSizeX0, iImgSizeY0, _pfValue, iImgSizeX0, iImgSizeY0, GDT_Float32, nCount, panBandMap, 0, 0, 0);
//释放内存
GDALClose(poDstDS);
GDALClose(poDataset);
}
else if (DoubleorFloat == GDT_Float64)
{
//写入新的tif
const char* pszDstFilename = newTiff.c_str();
GDALDriver* poDriver = GetGDALDriverManager()->GetDriverByName("GTiff");
GDALDataset* poDstDS = poDriver->Create(pszDstFilename, iImgSizeX0, iImgSizeY0, nCount, GDT_Float64, NULL);
poDstDS->SetProjection(poDataset->GetProjectionRef());//给它设置投影
poDstDS->SetGeoTransform(padfTransform0);//给设置空间转换的六参数
for (int i = 0; i < nCount; i++)
{
poDstDS->GetRasterBand(i + 1)->SetNoDataValue(dnodata);//将空值设置为“无数据值”
}
//保存全波段影像
poDstDS->RasterIO(GF_Write, 0, 0, iImgSizeX0, iImgSizeY0, _pdValue, iImgSizeX0, iImgSizeY0, GDT_Float64, nCount, panBandMap, 0, 0, 0);
//释放内存
GDALClose(poDstDS);
GDALClose(poDataset);
}
delete[]_pdValue;
delete[]_pfValue;
delete[]pSI_afScanblock1;
break;
}
case GDT_UInt32:
{
// unsigned long *pafScanblock1;
unsigned long*pUL_afScanblock1 = new unsigned long[iImgSizeX0*iImgSizeY0 * nCount];
double *_pdValue = new double[iImgSizeX0*iImgSizeY0 * nCount];
float *_pfValue = new float[iImgSizeX0*iImgSizeY0 * nCount];
poDataset->RasterIO(GF_Read, 0, 0, iImgSizeX0, iImgSizeY0, pUL_afScanblock1, iImgSizeX0, iImgSizeY0, gdal_data_type, nCount, panBandMap, 0, 0, 0);
vector<double>vMax;
for (int i = 0; i < nCount; i++)
{
//获取每个波段最大值
double dMax = -999999999999999;
for (int j = 0; j < iImgSizeX0*iImgSizeY0; j++)
{
_pdValue[i*iImgSizeX0*iImgSizeY0 + j] = (double)pUL_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j];
_pfValue[i*iImgSizeX0*iImgSizeY0 + j] = (float)pUL_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j];
if (dMax < (double)pUL_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] && abs((double)pUL_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] - dnodata)>0.000001)
{
dMax = (double)pUL_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j];
}
}
vMax.push_back(dMax);
}
for (int i = 0; i < nCount; i++)
{
for (int j = 0; j < iImgSizeX0*iImgSizeY0; j++)
{
if (abs((double)pUL_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] - dnodata) > 0.000001)
{
_pfValue[i*iImgSizeX0*iImgSizeY0 + j] = (float)pUL_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] / vMax[i];
_pdValue[i*iImgSizeX0*iImgSizeY0 + j] = (double)pUL_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] / vMax[i];
}
}
}
vMax.clear();
if (DoubleorFloat == GDT_Float32)
{
//写入新的tif
const char* pszDstFilename = newTiff.c_str();
GDALDriver* poDriver = GetGDALDriverManager()->GetDriverByName("GTiff");
GDALDataset* poDstDS = poDriver->Create(pszDstFilename, iImgSizeX0, iImgSizeY0, nCount, GDT_Float32, NULL);
poDstDS->SetProjection(poDataset->GetProjectionRef());//给它设置投影
poDstDS->SetGeoTransform(padfTransform0);//给设置空间转换的六参数
for (int i = 0; i < nCount; i++)
{
poDstDS->GetRasterBand(i + 1)->SetNoDataValue(dnodata);//将空值设置为“无数据值”
}
//保存全波段影像
poDstDS->RasterIO(GF_Write, 0, 0, iImgSizeX0, iImgSizeY0, _pfValue, iImgSizeX0, iImgSizeY0, GDT_Float32, nCount, panBandMap, 0, 0, 0);
//释放内存
GDALClose(poDstDS);
GDALClose(poDataset);
}
else if (DoubleorFloat == GDT_Float64)
{
//写入新的tif
const char* pszDstFilename = newTiff.c_str();
GDALDriver* poDriver = GetGDALDriverManager()->GetDriverByName("GTiff");
GDALDataset* poDstDS = poDriver->Create(pszDstFilename, iImgSizeX0, iImgSizeY0, nCount, GDT_Float64, NULL);
poDstDS->SetProjection(poDataset->GetProjectionRef());//给它设置投影
poDstDS->SetGeoTransform(padfTransform0);//给设置空间转换的六参数
for (int i = 0; i < nCount; i++)
{
poDstDS->GetRasterBand(i + 1)->SetNoDataValue(dnodata);//将空值设置为“无数据值”
}
//保存全波段影像
poDstDS->RasterIO(GF_Write, 0, 0, iImgSizeX0, iImgSizeY0, _pdValue, iImgSizeX0, iImgSizeY0, GDT_Float64, nCount, panBandMap, 0, 0, 0);
//释放内存
GDALClose(poDstDS);
GDALClose(poDataset);
}
delete[]_pdValue;
delete[]_pfValue;
delete[]pUL_afScanblock1;
break;
}
case GDT_Int32:
{
// long *pafScanblock1;
long *pL_afScanblock1 = new long[iImgSizeX0*iImgSizeY0 * nCount];
double *_pdValue = new double[iImgSizeX0*iImgSizeY0 * nCount];
float *_pfValue = new float[iImgSizeX0*iImgSizeY0 * nCount];
poDataset->RasterIO(GF_Read, 0, 0, iImgSizeX0, iImgSizeY0, pL_afScanblock1, iImgSizeX0, iImgSizeY0, gdal_data_type, nCount, panBandMap, 0, 0, 0);
vector<double>vMax;
for (int i = 0; i < nCount; i++)
{
//获取每个波段最大值
double dMax = -999999999999999;
for (int j = 0; j < iImgSizeX0*iImgSizeY0; j++)
{
_pdValue[i*iImgSizeX0*iImgSizeY0 + j] = (double)pL_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j];
_pfValue[i*iImgSizeX0*iImgSizeY0 + j] = (float)pL_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j];
if (dMax < (double)pL_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] && abs((double)pL_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] - dnodata)>0.000001)
{
dMax = (double)pL_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j];
}
}
vMax.push_back(dMax);
}
for (int i = 0; i < nCount; i++)
{
for (int j = 0; j < iImgSizeX0*iImgSizeY0; j++)
{
if (abs((double)pL_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] - dnodata) > 0.000001)
{
_pfValue[i*iImgSizeX0*iImgSizeY0 + j] = (float)pL_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] / vMax[i];
_pdValue[i*iImgSizeX0*iImgSizeY0 + j] = (double)pL_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] / vMax[i];
}
}
}
vMax.clear();
if (DoubleorFloat == GDT_Float32)
{
//写入新的tif
const char* pszDstFilename = newTiff.c_str();
GDALDriver* poDriver = GetGDALDriverManager()->GetDriverByName("GTiff");
GDALDataset* poDstDS = poDriver->Create(pszDstFilename, iImgSizeX0, iImgSizeY0, nCount, GDT_Float32, NULL);
poDstDS->SetProjection(poDataset->GetProjectionRef());//给它设置投影
poDstDS->SetGeoTransform(padfTransform0);//给设置空间转换的六参数
for (int i = 0; i < nCount; i++)
{
poDstDS->GetRasterBand(i + 1)->SetNoDataValue(dnodata);//将空值设置为“无数据值”
}
//保存全波段影像
poDstDS->RasterIO(GF_Write, 0, 0, iImgSizeX0, iImgSizeY0, _pfValue, iImgSizeX0, iImgSizeY0, GDT_Float32, nCount, panBandMap, 0, 0, 0);
//释放内存
GDALClose(poDstDS);
GDALClose(poDataset);
}
else if (DoubleorFloat == GDT_Float64)
{
//写入新的tif
const char* pszDstFilename = newTiff.c_str();
GDALDriver* poDriver = GetGDALDriverManager()->GetDriverByName("GTiff");
GDALDataset* poDstDS = poDriver->Create(pszDstFilename, iImgSizeX0, iImgSizeY0, nCount, GDT_Float64, NULL);
poDstDS->SetProjection(poDataset->GetProjectionRef());//给它设置投影
poDstDS->SetGeoTransform(padfTransform0);//给设置空间转换的六参数
for (int i = 0; i < nCount; i++)
{
poDstDS->GetRasterBand(i + 1)->SetNoDataValue(dnodata);//将空值设置为“无数据值”
}
//保存全波段影像
poDstDS->RasterIO(GF_Write, 0, 0, iImgSizeX0, iImgSizeY0, _pdValue, iImgSizeX0, iImgSizeY0, GDT_Float64, nCount, panBandMap, 0, 0, 0);
//释放内存
GDALClose(poDstDS);
GDALClose(poDataset);
}
delete[]_pdValue;
delete[]_pfValue;
delete[]pL_afScanblock1;
break;
}
case GDT_Float32:
{
float *pF_afScanblock1 = new float[iImgSizeX0*iImgSizeY0 * nCount];
double *_pdValue = new double[iImgSizeX0*iImgSizeY0 * nCount];
float *_pfValue = new float[iImgSizeX0*iImgSizeY0 * nCount];
poDataset->RasterIO(GF_Read, 0, 0, iImgSizeX0, iImgSizeY0, pF_afScanblock1, iImgSizeX0, iImgSizeY0, gdal_data_type, nCount, panBandMap, 0, 0, 0);
vector<double>vMax;
for (int i = 0; i < nCount; i++)
{
//获取每个波段最大值
double dMax = -999999999999999;
for (int j = 0; j < iImgSizeX0*iImgSizeY0; j++)
{
_pdValue[i*iImgSizeX0*iImgSizeY0 + j] = (double)pF_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j];
_pfValue[i*iImgSizeX0*iImgSizeY0 + j] = (float)pF_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j];
if (dMax < (double)pF_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] && abs((double)pF_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] - dnodata)>0.000001)
{
dMax = (double)pF_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j];
}
}
vMax.push_back(dMax);
}
for (int i = 0; i < nCount; i++)
{
for (int j = 0; j < iImgSizeX0*iImgSizeY0; j++)
{
double aa = abs((double)pF_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] - dnodata);
if (abs((double)pF_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] - dnodata) > 0.000001&&pF_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] >-3.4028229999999999e+37)
{
_pfValue[i*iImgSizeX0*iImgSizeY0 + j] = (float)pF_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] / vMax[i];
_pdValue[i*iImgSizeX0*iImgSizeY0 + j] = (double)pF_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] / vMax[i];
}
if (pF_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] < -3.4028229999999999e+37)
{
//对极小nodata进行替换处理
dnodata = -9999999999;
_pfValue[i*iImgSizeX0*iImgSizeY0 + j] = -9999999999;
_pdValue[i*iImgSizeX0*iImgSizeY0 + j] = -9999999999;
}
}
}
vMax.clear();
if (DoubleorFloat == GDT_Float32)
{
//写入新的tif
const char* pszDstFilename = newTiff.c_str();
GDALDriver* poDriver = GetGDALDriverManager()->GetDriverByName("GTiff");
GDALDataset* poDstDS = poDriver->Create(pszDstFilename, iImgSizeX0, iImgSizeY0, nCount, GDT_Float32, NULL);
poDstDS->SetProjection(poDataset->GetProjectionRef());//给它设置投影
poDstDS->SetGeoTransform(padfTransform0);//给设置空间转换的六参数
for (int i = 0; i < nCount; i++)
{
poDstDS->GetRasterBand(i + 1)->SetNoDataValue(dnodata);//将空值设置为“无数据值”
}
//保存全波段影像
poDstDS->RasterIO(GF_Write, 0, 0, iImgSizeX0, iImgSizeY0, _pfValue, iImgSizeX0, iImgSizeY0, GDT_Float32, nCount, panBandMap, 0, 0, 0);
//释放内存
GDALClose(poDstDS);
GDALClose(poDataset);
}
else if (DoubleorFloat == GDT_Float64)
{
//写入新的tif
const char* pszDstFilename = newTiff.c_str();
GDALDriver* poDriver = GetGDALDriverManager()->GetDriverByName("GTiff");
GDALDataset* poDstDS = poDriver->Create(pszDstFilename, iImgSizeX0, iImgSizeY0, nCount, GDT_Float64, NULL);
poDstDS->SetProjection(poDataset->GetProjectionRef());//给它设置投影
poDstDS->SetGeoTransform(padfTransform0);//给设置空间转换的六参数
for (int i = 0; i < nCount; i++)
{
poDstDS->GetRasterBand(i + 1)->SetNoDataValue(dnodata);//将空值设置为“无数据值”
}
//保存全波段影像
poDstDS->RasterIO(GF_Write, 0, 0, iImgSizeX0, iImgSizeY0, _pdValue, iImgSizeX0, iImgSizeY0, GDT_Float64, nCount, panBandMap, 0, 0, 0);
//释放内存
GDALClose(poDstDS);
GDALClose(poDataset);
}
delete[]_pdValue;
delete[]_pfValue;
delete[]pF_afScanblock1;
break;
}
case GDT_Float64:
{
// double *pafScanblock1;
double *pD_afScanblock1 = new double[iImgSizeX0*iImgSizeY0 *nCount];
double *_pdValue = new double[iImgSizeX0*iImgSizeY0 * nCount];
float *_pfValue = new float[iImgSizeX0*iImgSizeY0 * nCount];
poDataset->RasterIO(GF_Read, 0, 0, iImgSizeX0, iImgSizeY0, pD_afScanblock1, iImgSizeX0, iImgSizeY0, gdal_data_type, nCount, panBandMap, 0, 0, 0);
vector<double>vMax;
for (int i = 0; i < nCount; i++)
{
//获取每个波段最大值
double dMax = -999999999999999;
for (int j = 0; j < iImgSizeX0*iImgSizeY0; j++)
{
_pdValue[i*iImgSizeX0*iImgSizeY0 + j] = (double)pD_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j];
_pfValue[i*iImgSizeX0*iImgSizeY0 + j] = (float)pD_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j];
if (dMax < (double)pD_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] && abs((double)pD_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] - dnodata)>0.000001)
{
dMax = (double)pD_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j];
}
}
vMax.push_back(dMax);
}
for (int i = 0; i < nCount; i++)
{
for (int j = 0; j < iImgSizeX0*iImgSizeY0; j++)
{
if (abs((double)pD_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] - dnodata) > 0.000001&&pD_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] > -3.4028229999999999e+37)
{
_pfValue[i*iImgSizeX0*iImgSizeY0 + j] = (float)pD_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] / vMax[i];
_pdValue[i*iImgSizeX0*iImgSizeY0 + j] = (double)pD_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] / vMax[i];
}
if (pD_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] < -3.4028229999999999e+37)
{
//对极小nodata进行替换处理
dnodata = -9999999999;
_pfValue[i*iImgSizeX0*iImgSizeY0 + j] = -9999999999;
_pdValue[i*iImgSizeX0*iImgSizeY0 + j] = -9999999999;
}
}
}
vMax.clear();
if (DoubleorFloat == GDT_Float32)
{
//写入新的tif
const char* pszDstFilename = newTiff.c_str();
GDALDriver* poDriver = GetGDALDriverManager()->GetDriverByName("GTiff");
GDALDataset* poDstDS = poDriver->Create(pszDstFilename, iImgSizeX0, iImgSizeY0, nCount, GDT_Float32, NULL);
poDstDS->SetProjection(poDataset->GetProjectionRef());//给它设置投影
poDstDS->SetGeoTransform(padfTransform0);//给设置空间转换的六参数
for (int i = 0; i < nCount; i++)
{
poDstDS->GetRasterBand(i + 1)->SetNoDataValue(dnodata);//将空值设置为“无数据值”
}
//保存全波段影像
poDstDS->RasterIO(GF_Write, 0, 0, iImgSizeX0, iImgSizeY0, _pfValue, iImgSizeX0, iImgSizeY0, GDT_Float32, nCount, panBandMap, 0, 0, 0);
//释放内存
GDALClose(poDstDS);
GDALClose(poDataset);
}
else if (DoubleorFloat == GDT_Float64)
{
//写入新的tif
const char* pszDstFilename = newTiff.c_str();
GDALDriver* poDriver = GetGDALDriverManager()->GetDriverByName("GTiff");
GDALDataset* poDstDS = poDriver->Create(pszDstFilename, iImgSizeX0, iImgSizeY0, nCount, GDT_Float64, NULL);
poDstDS->SetProjection(poDataset->GetProjectionRef());//给它设置投影
poDstDS->SetGeoTransform(padfTransform0);//给设置空间转换的六参数
for (int i = 0; i < nCount; i++)
{
poDstDS->GetRasterBand(i + 1)->SetNoDataValue(dnodata);//将空值设置为“无数据值”
}
//保存全波段影像
poDstDS->RasterIO(GF_Write, 0, 0, iImgSizeX0, iImgSizeY0, _pdValue, iImgSizeX0, iImgSizeY0, GDT_Float64, nCount, panBandMap, 0, 0, 0);
//释放内存
GDALClose(poDstDS);
GDALClose(poDataset);
}
delete[]_pdValue;
delete[]_pfValue;
delete[]pD_afScanblock1;
break;
}
default: break;
}
delete[]panBandMap;
}