归一化是图像数据预处理的常用手段,然而Tiff图像存在多/单波段类型,且数据格式多样化,这为数据处理带来了不少的麻烦。为了便于后期的工作,笔者使用C++语言基于GDAL外部库处理Tiff图像,实现Tiff图像的归一化功能。
直接上代码:
#include "gdal_priv.h"
#include "ogrsf_frmts.h"
#include <iostream>
using namespace alglib;
using namespace std;
void normalizedTiff(std::string initialTiff, std::string newTiff)
{
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];
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++)
{
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)
{
pUC_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] = pUC_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] / vMax[i];
}
}
}
vMax.clear();
const char* pszDstFilename = newTiff.c_str();
GDALDriver* poDriver = GetGDALDriverManager()->GetDriverByName("GTiff");
GDALDataset* poDstDS = poDriver->Create(pszDstFilename, iImgSizeX0, iImgSizeY0, nCount, gdal_data_type, 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, pUC_afScanblock1, iImgSizeX0, iImgSizeY0, gdal_data_type, nCount, panBandMap, 0, 0, 0);
GDALClose(poDstDS);
GDALClose(poDataset);
delete[]pUC_afScanblock1;
break;
}
case GDT_UInt16:
{
unsigned short *pUS_afScanblock1 = new unsigned short[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++)
{
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)
{
pUS_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] = pUS_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] / vMax[i];
}
}
}
vMax.clear();
const char* pszDstFilename = newTiff.c_str();
GDALDriver* poDriver = GetGDALDriverManager()->GetDriverByName("GTiff");
GDALDataset* poDstDS = poDriver->Create(pszDstFilename, iImgSizeX0, iImgSizeY0, nCount, gdal_data_type, 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, pUS_afScanblock1, iImgSizeX0, iImgSizeY0, gdal_data_type, nCount, panBandMap, 0, 0, 0);
GDALClose(poDstDS);
GDALClose(poDataset);
delete[]pUS_afScanblock1;
break;
}
case GDT_Int16:
{
short int*pSI_afScanblock1 = new short int[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++)
{
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)
{
pSI_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] = pSI_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] / vMax[i];
}
}
}
vMax.clear();
const char* pszDstFilename = newTiff.c_str();
GDALDriver* poDriver = GetGDALDriverManager()->GetDriverByName("GTiff");
GDALDataset* poDstDS = poDriver->Create(pszDstFilename, iImgSizeX0, iImgSizeY0, nCount, gdal_data_type, 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, pSI_afScanblock1, iImgSizeX0, iImgSizeY0, gdal_data_type, nCount, panBandMap, 0, 0, 0);
GDALClose(poDstDS);
GDALClose(poDataset);
delete[]pSI_afScanblock1;
break;
}
case GDT_UInt32:
{
unsigned long*pUL_afScanblock1 = new unsigned long[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++)
{
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)
{
pUL_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] = pUL_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] / vMax[i];
}
}
}
vMax.clear();
const char* pszDstFilename = newTiff.c_str();
GDALDriver* poDriver = GetGDALDriverManager()->GetDriverByName("GTiff");
GDALDataset* poDstDS = poDriver->Create(pszDstFilename, iImgSizeX0, iImgSizeY0, nCount, gdal_data_type, 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, pUL_afScanblock1, iImgSizeX0, iImgSizeY0, gdal_data_type, nCount, panBandMap, 0, 0, 0);
GDALClose(poDstDS);
GDALClose(poDataset);
delete[]pUL_afScanblock1;
break;
}
case GDT_Int32:
{
long *pL_afScanblock1 = new long[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++)
{
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)
{
pL_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] = pL_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] / vMax[i];
}
}
}
vMax.clear();
const char* pszDstFilename = newTiff.c_str();
GDALDriver* poDriver = GetGDALDriverManager()->GetDriverByName("GTiff");
GDALDataset* poDstDS = poDriver->Create(pszDstFilename, iImgSizeX0, iImgSizeY0, nCount, gdal_data_type, 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, pL_afScanblock1, iImgSizeX0, iImgSizeY0, gdal_data_type, nCount, panBandMap, 0, 0, 0);
GDALClose(poDstDS);
GDALClose(poDataset);
delete[]pL_afScanblock1;
break;
}
case GDT_Float32:
{
float *pF_afScanblock1 = 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++)
{
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++)
{
if (abs((double)pF_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] - dnodata) > 0.000001)
{
pF_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] = pF_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] / vMax[i];
}
}
}
vMax.clear();
const char* pszDstFilename = newTiff.c_str();
GDALDriver* poDriver = GetGDALDriverManager()->GetDriverByName("GTiff");
GDALDataset* poDstDS = poDriver->Create(pszDstFilename, iImgSizeX0, iImgSizeY0, nCount, gdal_data_type, 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, pF_afScanblock1, iImgSizeX0, iImgSizeY0, gdal_data_type, nCount, panBandMap, 0, 0, 0);
GDALClose(poDstDS);
GDALClose(poDataset);
delete[]pF_afScanblock1;
break;
}
case GDT_Float64:
{
double *pD_afScanblock1 = new double[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++)
{
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] = pD_afScanblock1[i*iImgSizeX0*iImgSizeY0 + j] / vMax[i];
}
}
}
vMax.clear();
const char* pszDstFilename = newTiff.c_str();
GDALDriver* poDriver = GetGDALDriverManager()->GetDriverByName("GTiff");
GDALDataset* poDstDS = poDriver->Create(pszDstFilename, iImgSizeX0, iImgSizeY0, nCount, gdal_data_type, 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, pD_afScanblock1, iImgSizeX0, iImgSizeY0, gdal_data_type, nCount, panBandMap, 0, 0, 0);
GDALClose(poDstDS);
GDALClose(poDataset);
delete[]pD_afScanblock1;
break;
}
default: break;
}
delete[]panBandMap;
}
|