23 #include "base/Optionpk.h" 24 #include "imageclasses/ImgReaderGdal.h" 25 #include "imageclasses/ImgWriterGdal.h" 29 int main(
int argc,
char **argv) {
33 Optionpk<double> minRef_opt(
"minref",
"minref",
"Sets minimum for histogram of reference image");
34 Optionpk<double> maxRef_opt(
"maxref",
"maxref",
"Sets maximum for histogram of reference image");
35 Optionpk<double> minInput_opt(
"mininput",
"mininput",
"Sets minimum for histogram of input image");
36 Optionpk<double> maxInput_opt(
"maxinput",
"maxinput",
"Sets maximum for histogram of input image");
37 Optionpk<double> nodata_opt(
"nodata",
"nodata",
"Sets no data value(s) for calculations (nodata values in input image)");
42 Optionpk<short> nbin_opt(
"nbin",
"nbin",
"Number of bins used in histogram. Use 0 for all input values as integers",0);
43 Optionpk<string> otype_opt(
"ot",
"otype",
"Data type for output image ({Byte/Int16/UInt16/UInt32/Int32/Float32/Float64/CInt16/CInt32/CFloat32/CFloat64}). Empty string: inherit type from input image",
"");
44 Optionpk<string> oformat_opt(
"of",
"oformat",
"Output image format (see also gdal_translate). Empty string: inherit from input image");
45 Optionpk<string> option_opt(
"co",
"co",
"Creation option for output file. Multiple options can be specified.");
50 doProcess=input_opt.retrieveOption(argc,argv);
51 reference_opt.retrieveOption(argc,argv);
52 output_opt.retrieveOption(argc,argv);
53 minRef_opt.retrieveOption(argc,argv);
54 maxRef_opt.retrieveOption(argc,argv);
55 minInput_opt.retrieveOption(argc,argv);
56 maxInput_opt.retrieveOption(argc,argv);
57 nodata_opt.retrieveOption(argc,argv);
58 method_opt.retrieveOption(argc,argv);
59 nbin_opt.retrieveOption(argc,argv);
60 otype_opt.retrieveOption(argc,argv);
61 oformat_opt.retrieveOption(argc,argv);
62 option_opt.retrieveOption(argc,argv);
63 verbose_opt.retrieveOption(argc,argv);
65 catch(
string predefinedString){
66 std::cout << predefinedString << std::endl;
70 std::cout <<
"short option -h shows basic options only, use long option --help to show all options" << std::endl;
77 assert(input_opt.size());
78 inputImg.
open(input_opt[0]);
79 for(
int inodata=0;inodata<nodata_opt.size();++inodata){
86 GDALDataType theType=GDT_Unknown;
88 cout <<
"possible output data types: ";
89 for(
int iType = 0; iType < GDT_TypeCount; ++iType){
91 cout <<
" " << GDALGetDataTypeName((GDALDataType)iType);
92 if( GDALGetDataTypeName((GDALDataType)iType) != NULL
93 && EQUAL(GDALGetDataTypeName((GDALDataType)iType),
94 otype_opt[0].c_str()))
95 theType=(GDALDataType) iType;
97 if(theType==GDT_Unknown)
101 std::cout << std::endl <<
"Output pixel type: " << GDALGetDataTypeName(theType) << endl;
104 if(oformat_opt.size())
105 imageType=oformat_opt[0];
107 if(option_opt.findSubstring(
"INTERLEAVE=")==option_opt.end()){
108 string theInterleave=
"INTERLEAVE=";
110 option_opt.push_back(theInterleave);
113 assert(output_opt.size());
115 std::cout <<
"opening output image " << output_opt[0] << std::endl;
118 catch(
string errorstring){
119 cout << errorstring << endl;
123 if(method_opt[0]==
"histmatch"){
124 assert(reference_opt.size());
125 referenceImg.
open(reference_opt[0]);
126 assert(nband==referenceImg.
nrOfBand());
127 for(
int inodata=0;inodata<nodata_opt.size();++inodata){
132 const char* pszMessage;
133 void* pProgressArg=NULL;
134 GDALProgressFunc pfnProgress=GDALTermProgress;
136 pfnProgress(progress,pszMessage,pProgressArg);
137 for(
int iband=0;iband<nband;++iband){
139 unsigned int nbinRef=nbin_opt[0];
140 unsigned int nbinInput=nbin_opt[0];
141 std::vector<double> histRef(nbinRef);
142 std::vector<double> histInput(nbinInput);
143 double minValueRef=0;
144 double maxValueRef=0;
145 double minValueInput=0;
146 double maxValueInput=0;
147 if(minRef_opt.size())
148 minValueRef=minRef_opt[0];
149 if(maxRef_opt.size())
150 maxValueRef=maxRef_opt[0];
151 if(minInput_opt.size())
152 minValueInput=minInput_opt[0];
153 if(maxInput_opt.size())
154 maxValueInput=maxInput_opt[0];
155 unsigned long int nsampleRef=referenceImg.
getHistogram(histRef,minValueRef,maxValueRef,nbinRef,iband);
156 unsigned long int nsampleInput=inputImg.
getHistogram(histInput,minValueInput,maxValueInput,nbinInput,iband);
158 for(
unsigned int bin=0;bin<nbinRef;++bin){
159 histRef[bin]+=100.0*
static_cast<double>(histRef[bin])/static_cast<double>(nsampleRef);
161 for(
unsigned int bin=0;bin<nbinInput;++bin)
162 histInput[bin]+=100.0*static_cast<double>(histInput[bin])/
static_cast<double>(nsampleInput);
164 vector<double> lineBuffer(inputImg.
nrOfCol());
165 for(
int irow=0;irow<inputImg.
nrOfRow();++irow){
166 inputImg.
readData(lineBuffer,GDT_Float64, irow, iband);
167 for(
int icol=0;icol<inputImg.
nrOfCol();++icol){
169 int inputBin=
static_cast<int>(
static_cast<double>(lineBuffer[icol]-minValueInput)/(maxValueInput-minValueInput)*(histInput.size()-1));
174 for(ibin=0;ibin<histRef.size();++ibin){
175 if(histRef[ibin]>histInput[inputBin])
180 lineBuffer[icol]=(maxValueRef-minValueRef)/(histRef.size()-1)*(ibin)+minValueRef;
183 outputImg.
writeData(lineBuffer,GDT_Float64,irow,iband);
185 progress+=(outputImg.
nrOfRow()*iband);
189 pfnProgress(progress,pszMessage,pProgressArg);
195 if(method_opt[0]==
"histmatch")
196 referenceImg.
close();
int nrOfBand(void) const
Get the number of bands of this dataset.
GDALDataType getDataType(int band=0) const
Get the GDAL datatype for this dataset.
double getHistogram(std::vector< double > &histvector, double &min, double &max, unsigned int &nbin, int theBand=0, bool kde=false)
Calculate the image histogram for a specific band using a defined number of bins and constrained by a...
void close(void)
Set the memory (in MB) to cache a number of rows in memory.
int nrOfRow(void) const
Get the number of rows of this dataset.
void readData(T &value, int col, int row, int band=0)
Read a single pixel cell value at a specific column and row for a specific band (all indices start co...
std::string getImageType() const
Get the image type (implemented as the driver description)
bool writeData(T &value, int col, int row, int band=0)
Write a single pixel cell value at a specific column and row for a specific band (all indices start c...
int pushNoDataValue(double noDataValue)
Push a no data value for this dataset.
void open(const std::string &filename, const ImgReaderGdal &imgSrc, const std::vector< std::string > &options=std::vector< std::string >())
Open an image for writing, copying image attributes from a source image. Image is directly written to...
std::string getInterleave() const
Get the band coding (interleave)
void close(void)
Close the image.
CPLErr GDALSetNoDataValue(double noDataValue, int band=0)
Set the GDAL (internal) no data value for this data set. Only a single no data value per band is supp...
void open(const std::string &filename, const GDALAccess &readMode=GA_ReadOnly)
Open an image.
int nrOfCol(void) const
Get the number of columns of this dataset.