Commit f3d25eb8 authored by Christophe Palmann's avatar Christophe Palmann

BandMathX app: can take expr or cst from txt files. importContext (from...

BandMathX app: can take expr or cst from txt files. importContext (from otbBandMathImageFilterX) improved
parent 325152ca
......@@ -134,9 +134,20 @@ private:
AddRAMParameter();
AddParameter(ParameterType_String, "exp", "Expression");
AddParameter(ParameterType_StringList, "exp", "Expression");
SetParameterDescription("exp",
"The mathematical expression to apply.");
MandatoryOff("exp");
AddParameter(ParameterType_String, "incontext", "Import context");
SetParameterDescription("incontext",
"A txt file containing user's constants and expressions.");
MandatoryOff("incontext");
AddParameter(ParameterType_String, "outcontext", "Export context");
SetParameterDescription("outcontext",
"A txt file containing user's constants and expressions.");
MandatoryOff("outcontext");
// Doc example parameter settings
SetDocExampleParameterValue("il", "verySmallFSATSW_r.tif verySmallFSATSW_nir.tif verySmallFSATSW.tif");
......@@ -158,14 +169,19 @@ private:
const unsigned int nbImages = inList->Size();
if (nbImages == 0)
{
itkExceptionMacro("No input Image set...; please set at least one input image");
}
{
itkExceptionMacro("No input Image set...; please set at least one input image");
}
if ( (!HasUserValue("exp")) && (!HasUserValue("incontext")) )
{
itkExceptionMacro("No expression set...; please set at least one one expression");
}
//m_ChannelExtractorList = ExtractROIFilterListType::New();
m_Filter = BandMathImageFilterType::New();
m_Filter = BandMathImageFilterType::New();
for (unsigned int i = 0; i < nbImages; i++)
{
FloatVectorImageType::Pointer currentImage = inList->GetNthElement(i);
......@@ -179,10 +195,22 @@ private:
}
m_Filter->SetExpression(GetParameterString("exp"));
if ( (HasUserValue("exp")) && (IsParameterEnabled("exp")) )
{
std::vector<std::string> stringList = GetParameterStringList("exp");
for(int s=0; s<stringList.size(); s++)
m_Filter->SetExpression(stringList[s]);
}
if ( (HasUserValue("incontext")) && (IsParameterEnabled("incontext")) )
m_Filter->importContext(GetParameterString("incontext"));
// Set the output image
SetParameterOutputImage("out", m_Filter->GetOutput());
if ( (HasUserValue("outcontext")) && (IsParameterEnabled("outcontext")) )
m_Filter->exportContext(GetParameterString("outcontext"));
}
BandMathImageFilterType::Pointer m_Filter;
......
......@@ -296,13 +296,13 @@ void BandMathImageFilterX<TImage>
template< typename TImage >
void BandMathImageFilterX<TImage>
::exportContext(const std::string& filename)
::exportContext(const std::string& filename)
{
std::vector< std::string > vectI,vectF,vectM, vectFinal;
for(int i=0; i<m_VAllowedVarNameAddedByUser.size(); i++)
{
{
std::ostringstream iss;
std::string str;
......@@ -310,12 +310,12 @@ void BandMathImageFilterX<TImage>
{
case 'i':
iss << "#I " << m_VAllowedVarNameAddedByUser[i].name << " " << m_VAllowedVarNameAddedByUser[i].value.GetInteger();
str=iss.str();
str=iss.str();
vectI.push_back(str);
break;
case 'f':
iss << "#F " << m_VAllowedVarNameAddedByUser[i].name << " " << m_VAllowedVarNameAddedByUser[i].value.GetFloat();
str=iss.str();
iss << "#F " << m_VAllowedVarNameAddedByUser[i].name << " " << m_VAllowedVarNameAddedByUser[i].value.GetFloat();
str=iss.str();
vectF.push_back(str);
break;
case 'c':
......@@ -328,23 +328,23 @@ void BandMathImageFilterX<TImage>
iss << " " << m_VAllowedVarNameAddedByUser[i].value.At(k,0);
for(int p=1; p<m_VAllowedVarNameAddedByUser[i].value.GetCols(); p++)
iss << " , " << m_VAllowedVarNameAddedByUser[i].value.At(k,p);
iss << ";";
iss << " ;";
}
str=iss.str();
str=iss.str();
str.erase(str.size()-1);
str.push_back('}');
vectM.push_back(str);
break;
}
}
}
// Sorrting : I F M and E at the end
for(int i=0; i<vectI.size(); ++i)
for(int i=0;i<vectI.size();++i)
vectFinal.push_back(vectI[i]);
for(int i=0; i<vectF.size(); ++i)
for(int i=0;i<vectF.size();++i)
vectFinal.push_back(vectF[i]);
for(int i=0; i<vectM.size(); ++i)
for(int i=0;i<vectM.size();++i)
vectFinal.push_back(vectM[i]);
for(int i=0; i < m_Expression.size(); ++i)
{
......@@ -355,100 +355,118 @@ void BandMathImageFilterX<TImage>
}
std::ofstream exportFile(filename.c_str(), std::ios::out | std::ios::trunc);
if(exportFile)
if(exportFile)
{
for(int i=0; i<vectFinal.size(); ++i)
exportFile << vectFinal[i] << std::endl;
exportFile.close();
}
else
else
itkExceptionMacro(<< "Could not open " << filename << "." << std::endl);
}
template< typename TImage >
void BandMathImageFilterX<TImage>
::importContext(const std::string& filename)
::importContext(const std::string& filename)
{
std::ifstream importFile(filename.c_str(), std::ios::in);
std::string line,sub,name,matrixdef;
int pos,pos2,lineID=0;
std::string wholeline,line,sub,name,matrixdef;
int pos,pos2,lineID=0,nbSuccesses=0;
double value;
if(importFile)
if(importFile)
{
while(std::getline(importFile,line))
while(std::getline(importFile,wholeline))
{
lineID++;
if ( (line[0] == '#') && ((line[1] == 'I') || (line[1] == 'i') || (line[1] == 'F') || (line[1] == 'f')) )
{
pos = line.find_first_not_of(' ',2);
pos = wholeline.find_first_not_of(' ');
if (pos != std::string::npos)
{
line = wholeline.substr(pos);
if (pos == std::string::npos)
itkExceptionMacro(<< "In file '"<< filename << "', line " << lineID << " : please, set the name and the value of the constant." << std::endl);
if ( (line[0] == '#') && ((line[1] == 'I') || (line[1] == 'i') || (line[1] == 'F') || (line[1] == 'f')) )
{
pos = line.find_first_not_of(' ',2);
sub = line.substr(pos);
if (pos == std::string::npos)
itkExceptionMacro(<< "In file '"<< filename << "', line " << lineID << " : please, set the name and the value of the constant." << std::endl);
pos = sub.find_first_of(' ');
name = sub.substr(0,pos);
if (sub.find_first_of('{',pos) != std::string::npos)
itkExceptionMacro(<< "In file '"<< filename << "', line " << lineID
<< " : symbol #F found, but find vector/matrix definition. Please, set an integer or a float number." << std::endl);
sub = line.substr(pos);
if (sub.find_first_not_of(' ',pos) == std::string::npos )
itkExceptionMacro(<< "In file '"<< filename << "', line " << lineID << " : please, set the value of the constant." << std::endl)
pos = sub.find_first_of(' ');
name = sub.substr(0,pos);
if (sub.find_first_of('{',pos) != std::string::npos)
itkExceptionMacro(<< "In file '"<< filename << "', line " << lineID
<< " : symbol #F found, but find vector/matrix definition. Please, set an integer or a float number." << std::endl);
std::istringstream iss( sub.substr(pos) );
iss >> value;
SetConstant(name,value);
if (sub.find_first_not_of(' ',pos) == std::string::npos )
itkExceptionMacro(<< "In file '"<< filename << "', line " << lineID << " : please, set the value of the constant." << std::endl)
}
else if ( (line[0] == '#') && ((line[1] == 'M') || (line[1] == 'm')) )
{
std::istringstream iss( sub.substr(pos) );
iss >> value;
SetConstant(name,value);
nbSuccesses++;
pos = line.find_first_not_of(' ',2);
}
else if ( (line[0] == '#') && ((line[1] == 'M') || (line[1] == 'm')) )
{
if (pos == std::string::npos)
itkExceptionMacro(<< "In file '"<< filename << "', line " << lineID << " : please, set the name and the definition of the vector/matrix." << std::endl);
pos = line.find_first_not_of(' ',2);
std::string sub = line.substr(pos);
if (pos == std::string::npos)
itkExceptionMacro(<< "In file '"<< filename << "', line " << lineID << " : please, set the name and the definition of the vector/matrix." << std::endl);
pos = sub.find_first_of(' ');
name = sub.substr(0,pos);
pos2 = sub.find_first_of('{');
if (pos2 != std::string::npos)
matrixdef = sub.substr(pos2);
else
itkExceptionMacro(<< "In file '"<< filename << "', line " << lineID << " : symbol #M found, but couldn't not find vector/matrix definition." << std::endl);
std::string sub = line.substr(pos);
SetMatrix(name,matrixdef);
pos = sub.find_first_of(' ');
name = sub.substr(0,pos);
pos2 = sub.find_first_of('{');
if (pos2 != std::string::npos)
matrixdef = sub.substr(pos2);
else
itkExceptionMacro(<< "In file '"<< filename << "', line " << lineID << " : symbol #M found, but couldn't not find vector/matrix definition." << std::endl);
}
else if ( (line[0] == '#') && ((line[1] == 'E') || (line[1] == 'e')) )
{
pos = line.find_first_not_of(' ',2);
SetMatrix(name,matrixdef);
nbSuccesses++;
if (pos == std::string::npos)
itkExceptionMacro(<< "In file '"<< filename << "', line " << lineID << " : symbol #E found, but couldn't not find any expression." << std::endl);
}
else if ( (line[0] == '#') && ((line[1] == 'E') || (line[1] == 'e')) )
{
pos = line.find_first_not_of(' ',2);
sub = line.substr(pos);
if (pos == std::string::npos)
itkExceptionMacro(<< "In file '"<< filename << "', line " << lineID << " : symbol #E found, but couldn't not find any expression." << std::endl);
SetExpression(sub);
}
sub = line.substr(pos);
}
SetExpression(sub);
nbSuccesses++;
}
}
}//while
importFile.close();
if (nbSuccesses == 0)
itkExceptionMacro(<< "No constant or expression could be set; please, ensure that the file '" << filename << "' is correct." << std::endl);
}
else
else
itkExceptionMacro(<< "Could not open " << filename << "." << std::endl);
}
......@@ -571,7 +589,7 @@ void BandMathImageFilterX<TImage>
m_AImage[i][j].info[t]=m_VVarName[j].info[t];
//bool isAConstant = false;
//bool isAConstant = false;
if ( (m_AImage[i][j].type == 0 ) || (m_AImage[i][j].type == 1) ) // indices (idxX & idxY)
{
......@@ -622,7 +640,7 @@ void BandMathImageFilterX<TImage>
/* if (isAConstant)
m_VParser.at(i)->DefineConst(m_AImage[i][j].name, &(m_AImage[i][j].value));
else
m_VParser.at(i)->DefineVar(m_AImage[i][j].name, &(m_AImage[i][j].value)); */
m_VParser.at(i)->DefineVar(m_AImage[i][j].name, &(m_AImage[i][j].value));*/
initValue += 0.001;
if (initValue>1.0)
......
......@@ -153,7 +153,9 @@ OTB_TEST_APPLICATION(NAME apTvUtBandMathX
${INPUTDATA}/poupees_sub_c2.png
${INPUTDATA}/poupees_sub.png
-out ${TEMP}/apTvUtBandMathXOutput.tif
-exp "cos(im1b1)+im2b1*im3b1-im3b2+ndvi(im3b3,im3b4)"
#-exp "cos(im1b1)+im2b1*im3b1-im3b2+ndvi(im3b3,im3b4)"
-incontext ${INPUTDATA}/apTvUtExportBandMathX.txt
#-outcontext ${TEMP}/apTvUtExportBandMathXTemp.txt
VALID --compare-image ${NOTOL}
${INPUTDATA}/apTvUtBandMathOutput.tif
${TEMP}/apTvUtBandMathXOutput.tif)
......
......@@ -277,8 +277,6 @@ int otbBandMathImageFilterXConv( int itkNotUsed(argc), char* argv [])
//filter->SetExpression("conv(kernel1,imageAb1N3x5,imageAb2N3x5); im2b1^1.1; vcos(canal3); mean(imageAb2N3x3); var(imageAb2N3x3); median(imageAb2N3x3)");
filter->importContext(inputFilename); //Equivalent to three commands above
filter->Update();
//filter->exportContext("/home/cpalmann/Desktop/testBandMathX.txt");
if (filter->GetNumberOfOutputs() != 1)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment