Compare View

switch
from
...
to
 
Commits (5)
app/iota2FeatureExtraction.cxx
... ... @@ -82,6 +82,17 @@ private:
82 82 AddParameter(ParameterType_Empty, "copyinput", "Copy input bands to output image (true/false). Default value is false");
83 83 MandatoryOff("copyinput");
84 84  
  85 + AddParameter(ParameterType_Empty, "relrefl", "Compute relative reflectances (true/false). Default value is false");
  86 + MandatoryOff("relrefl");
  87 +
  88 + AddParameter(ParameterType_Int, "relindex", "Index for the band used as reference reflectance (starting at 1). The red band is used by default");
  89 + MandatoryOff("relindex");
  90 +
  91 + AddParameter(ParameterType_Empty, "keepduplicates", "Keep duplicate relative reflectances (true/false). Default value is false");
  92 + MandatoryOff("keepduplicates");
  93 +
  94 +
  95 +
85 96 AddRAMParameter();
86 97 }
87 98  
... ... @@ -91,46 +102,49 @@ private:
91 102  
92 103 void DoExecute()
93 104 {
94   - auto cpd = 1;
  105 + auto pars = FeatureExtractionFunctorType::Parameters{};
  106 +
  107 + FloatVectorImageType::Pointer inputImage = this->GetParameterImage("in");
  108 + inputImage->UpdateOutputInformation();
  109 + pars.NumberOfInputComponents = inputImage->GetNumberOfComponentsPerPixel();
  110 +
95 111 if(IsParameterEnabled("comp"))
96   - cpd = GetParameterInt("comp");
97   - auto redIndex = 3;
  112 + pars.ComponentsPerDate = GetParameterInt("comp");
98 113 if(IsParameterEnabled("red"))
99   - redIndex = GetParameterInt("red");
100   - auto nirIndex = 4;
  114 + pars.RedIndex = GetParameterInt("red");
101 115 if(IsParameterEnabled("nir"))
102   - nirIndex = GetParameterInt("nir");
103   - auto swirIndex = 5;
  116 + pars.NIRIndex = GetParameterInt("nir");
104 117 if(IsParameterEnabled("swir"))
105   - swirIndex = GetParameterInt("swir");
106   - auto normIndexFactor = float{1000};
  118 + pars.SWIRIndex = GetParameterInt("swir");
107 119 if(IsParameterEnabled("indfact"))
108   - normIndexFactor = GetParameterInt("indfact");
109   - auto noDataValue = float{-10000};
  120 + pars.NormalizedIndexFactor= GetParameterInt("indfact");
110 121 if(IsParameterEnabled("nodata"))
111   - noDataValue = GetParameterInt("nodata");
112   - auto copyInputBands = false;
  122 + pars.NoDataValue= GetParameterInt("nodata");
113 123 if (IsParameterEnabled("copyinput"))
114 124 {
115   - copyInputBands = true;
  125 + pars.CopyInputBands = true;
  126 + }
  127 + if(IsParameterEnabled("relrefl"))
  128 + {
  129 + std::cout << " Relative reflectances \n";
  130 + pars.RelativeReflectances = true;
  131 + if(IsParameterEnabled("keepduplicates"))
  132 + {
  133 + std::cout << " keep duplicates \n";
  134 + pars.RemoveDuplicates = false;
  135 + }
  136 + if(IsParameterEnabled("relindex"))
  137 + {
  138 + pars.ReferenceIndex = GetParameterInt("relindex");
  139 + if(pars.ReferenceIndex > pars.ComponentsPerDate)
  140 + {
  141 + itkExceptionMacro(<<"relindex must be between 1 and the number of components per date\n");
  142 + }
  143 + }
  144 + std::cout << " relative index " << pars.ReferenceIndex << " \n";
116 145 }
117 146  
118   - std::cout << "Copy input is " << copyInputBands << "\n";
119   - FloatVectorImageType::Pointer inputImage = this->GetParameterImage("in");
120   - inputImage->UpdateOutputInformation();
121   - auto nbOfInputBands = inputImage->GetNumberOfComponentsPerPixel();
122   -
123   - auto pars = FeatureExtractionFunctorType::Parameters{};
124   - pars.ComponentsPerDate = cpd;
125   - pars.RedIndex = redIndex;
126   - pars.NIRIndex = nirIndex;
127   - pars.SWIRIndex = swirIndex;
128   - pars.NormalizedIndexFactor = normIndexFactor;
129   - pars.NoDataValue = noDataValue;
130   - pars.NumberOfInputComponents = nbOfInputBands;
131   - pars.CopyInputBands = copyInputBands;
132   -
133   -
  147 +
134 148 auto fef = FeatureExtractionFunctorType(pars);
135 149 m_FeatureExtractionFilter = FeatureExtractionFilterType::New();
136 150 m_FeatureExtractionFilter->SetFunctor(fef);
... ... @@ -144,4 +158,4 @@ private:
144 158 } // end namespace Wrapper
145 159 } // end namespace otb
146 160  
147   -OTB_APPLICATION_EXPORT(otb::Wrapper::iota2FeatureExtraction)
  161 + OTB_APPLICATION_EXPORT(otb::Wrapper::iota2FeatureExtraction)
... ...
include/iota2FeatureExtraction.h
... ... @@ -78,17 +78,17 @@ public:
78 78 using VectorType = std::vector<ValueType>;
79 79  
80 80 struct Parameters {
81   - size_t ComponentsPerDate;
82   - size_t RedIndex;
83   - size_t NIRIndex;
84   - size_t SWIRIndex;
  81 + size_t ComponentsPerDate{1};
  82 + size_t RedIndex{3};
  83 + size_t NIRIndex{4};
  84 + size_t SWIRIndex{5};
85 85 bool RelativeReflectances{false};
86   - size_t ReferenceIndex;
  86 + size_t ReferenceIndex{3};
87 87 bool RemoveDuplicates{true};
88   - ValueType NormalizedIndexFactor;
89   - ValueType NoDataValue;
90   - size_t NumberOfInputComponents;
91   - bool CopyInputBands;
  88 + ValueType NormalizedIndexFactor{1000};
  89 + ValueType NoDataValue{-10000};
  90 + size_t NumberOfInputComponents{1};
  91 + bool CopyInputBands{false};
92 92 };
93 93 FeatureExtractionFunctor() = default;
94 94 FeatureExtractionFunctor(Parameters pars)
... ... @@ -103,9 +103,8 @@ public:
103 103 m_CopyInputBands{pars.CopyInputBands}
104 104 {
105 105 m_NumberOfDates = m_NumberOfInputComponents/m_ComponentsPerDate;
106   - const auto numberOfOutputFeatures = m_NumberOfFeatures-
107   - ((m_RelativeReflectances&&m_RemoveDuplicates)?1:0);
108   - m_NumberOfOutputComponents = ( numberOfOutputFeatures +
  106 + UpdateNumberOfFeatures();
  107 + m_NumberOfOutputComponents = ( m_NumberOfFeatures +
109 108 (m_CopyInputBands?
110 109 m_ComponentsPerDate:0))*m_NumberOfDates;
111 110 const auto max_index_band = std::max({m_RedIndex, m_NIRIndex, m_SWIRIndex});
... ... @@ -148,6 +147,14 @@ public:
148 147 }
149 148  
150 149 protected:
  150 + inline void UpdateNumberOfFeatures()
  151 + {
  152 + if(m_SWIRIndex==0) --m_NumberOfFeatures;
  153 + if((m_RelativeReflectances && m_RemoveDuplicates &&
  154 + (m_ReferenceIndex==m_RedIndex || m_ReferenceIndex==m_NIRIndex)))
  155 + --m_NumberOfFeatures;
  156 + }
  157 +
151 158 inline
152 159 void AddReflectances(const VectorType& inVec, VectorType& outVec)
153 160 {
... ... @@ -209,7 +216,7 @@ protected:
209 216 //compute the features
210 217 const auto red = *(inIt+m_RedIndex-1);
211 218 const auto nir = *(inIt+m_NIRIndex-1);
212   - const auto swir = *(inIt+m_SWIRIndex-1);
  219 + const auto swir = *(inIt+(m_SWIRIndex>0?m_SWIRIndex:1)-1);
213 220 VectorType tmpVec(m_ComponentsPerDate);
214 221 std::transform(inIt, inIt+m_ComponentsPerDate,tmpVec.begin(),
215 222 [](decltype(*inIt)x){ return x*x;});
... ... @@ -218,11 +225,14 @@ protected:
218 225 //append the features
219 226 size_t featureOffset{0};
220 227 //ndvi
221   - AddNormalizedIndexMaybe(nir, red, m_RedIndex, featureOffset,
222   - copyOffset, outVec, date_counter);
  228 + featureOffset = AddNormalizedIndexMaybe(nir, red, m_RedIndex, featureOffset,
  229 + copyOffset, outVec, date_counter);
223 230 //ndwi
224   - AddNormalizedIndexMaybe(swir, nir, m_NIRIndex, featureOffset,
225   - copyOffset, outVec, date_counter);
  231 + if(m_SWIRIndex!=0)
  232 + {
  233 + featureOffset = AddNormalizedIndexMaybe(swir, nir, m_NIRIndex, featureOffset,
  234 + copyOffset, outVec, date_counter);
  235 + }
226 236 outVec[copyOffset+m_NumberOfDates*featureOffset+date_counter] = brightness;
227 237 }
228 238 //move to the next date
... ... @@ -232,17 +242,19 @@ protected:
232 242 }
233 243  
234 244 inline
235   - void AddNormalizedIndexMaybe(ValueType refl, ValueType refrefl,
236   - size_t refindex, size_t& featureOffset,
237   - size_t copyOffset, VectorType& outVec,
238   - size_t date_counter)
  245 + size_t AddNormalizedIndexMaybe(ValueType refl, ValueType refrefl,
  246 + size_t refindex, size_t featureOffset,
  247 + size_t copyOffset, VectorType& outVec,
  248 + size_t date_counter)
239 249 {
240   - if(!m_RemoveDuplicates || m_ReferenceIndex != refindex)
  250 + auto result = featureOffset;
  251 + if(!(m_RemoveDuplicates && m_ReferenceIndex == refindex))
241 252 {
242 253 outVec[copyOffset+m_NumberOfDates*featureOffset+date_counter] =
243 254 normalized_index(refl, refrefl) * m_NormalizedIndexFactor;
244   - ++featureOffset;
  255 + ++result;
245 256 }
  257 + return result;
246 258 }
247 259  
248 260 size_t m_ComponentsPerDate;
... ... @@ -260,7 +272,7 @@ protected:
260 272 bool m_CopyInputBands;
261 273 size_t m_NumberOfOutputComponents;
262 274 size_t m_NumberOfDates;
263   - static constexpr size_t m_NumberOfFeatures = 3;
  275 + size_t m_NumberOfFeatures = 3;
264 276 };
265 277 } // end namespace iota2
266 278  
... ...