Commit 7b09c9b7 authored by Lozac'h Loic's avatar Lozac'h Loic
Browse files

Merge master into dev-hassan

Conflicts:
	app/otbInvertSARModel.cxx
parents 17e6df1c 06400cf9
......@@ -5,6 +5,11 @@
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.python.pydev.PyDevBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
<triggers>clean,full,incremental,</triggers>
......@@ -23,5 +28,6 @@
<nature>org.eclipse.cdt.core.ccnature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
<nature>org.python.pydev.pythonNature</nature>
</natures>
</projectDescription>
......@@ -11,7 +11,7 @@
<provider copy-of="extension" id="org.eclipse.cdt.managedbuilder.core.GCCBuildCommandParser"/>
<provider class="org.eclipse.cdt.managedbuilder.language.settings.providers.GCCBuiltinSpecsDetector" console="false" env-hash="-598394472140321343" id="org.eclipse.cdt.managedbuilder.core.GCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
<provider class="org.eclipse.cdt.managedbuilder.language.settings.providers.GCCBuiltinSpecsDetector" console="false" env-hash="941863880402804919" id="org.eclipse.cdt.managedbuilder.core.GCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
<language-scope id="org.eclipse.cdt.core.gcc"/>
......
......@@ -22,8 +22,7 @@ if(OTB_USE_TENSORFLOW)
find_library(TENSORFLOW_CC_LIB NAMES libtensorflow_cc)
find_library(TENSORFLOW_FRAMEWORK_LIB NAMES libtensorflow_framework)
set(OTBTensorflow_SYSTEM_INCLUDE_DIRS ${tensorflow_include_dir})
set(OTBTensorflow_LIBRARIES ${TENSORFLOW_CC_LIB} ${TENSORFLOW_FRAMEWORK_LIB})
set(TENSORFLOW_LIBS "${TENSORFLOW_CC_LIB}" "${TENSORFLOW_FRAMEWORK_LIB}")
else()
message("Tensorflow support disabled")
......
......@@ -3,7 +3,10 @@ OTB_CREATE_APPLICATION(NAME InvertSARModel
LINK_LIBRARIES ${${otb-module}_LIBRARIES}
)
OTB_CREATE_APPLICATION(NAME ApplyCsvLabelsFile
SOURCES otbApplyCsvLabelsFile.cxx
LINK_LIBRARIES ${${otb-module}_LIBRARIES}
)
OTB_CREATE_APPLICATION(NAME SoilMoistureLULCMask
SOURCES otbSoilMoistureLULCMask.cxx
LINK_LIBRARIES ${${otb-module}_LIBRARIES}
......
#include "otbWrapperApplication.h"
#include "otbWrapperApplicationFactory.h"
#include "itkFixedArray.h"
#include "itkObjectFactory.h"
// Mapper
#include "otbHumidityFunctors.h"
#include "itkUnaryFunctorImageFilter.h"
// Export csv
#include <boost/algorithm/string.hpp>
#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <iterator>
#include <string>
#include <algorithm>
#include <math.h>
namespace otb
{
namespace Wrapper
{
class ApplyCsvLabelsFile : public Application
{
public:
typedef ApplyCsvLabelsFile Self;
typedef Application Superclass;
typedef itk::SmartPointer<Self> Pointer;
itkNewMacro(Self);
itkTypeMacro(ApplyCsvLabelsFile, Application);
typedef std::map<UInt32ImageType::PixelType, FloatImageType::PixelType> OutputMapType;
/** Soil moisture mapping */
typedef otb::Functor::MapperFunctor<UInt32ImageType::PixelType, FloatImageType::PixelType, OutputMapType> MapperFunctorType;
typedef itk::UnaryFunctorImageFilter<UInt32ImageType, FloatImageType, MapperFunctorType> MoistMapFilterType;
private:
void DoInit()
{
SetName("ApplyCsvLabelsFile");
SetDescription("Apply csv results from otbInvertSARModel on Labels image");
// Documentation
SetDocLimitations("None");
SetDocAuthors("Loic Lozach");
SetDocSeeAlso(" ");
// Input images
AddParameter(ParameterType_InputFilename, "incsv", "Input CSV File");
AddParameter(ParameterType_InputImage, "inlabels", "Input labels image");
// Output image
AddParameter(ParameterType_OutputImage, "out", "Output image");
SetParameterDescription ("out", "The output image is the estimated soil moisture");
}
void DoUpdateParameters()
{
}
/*
* Do the work
*/
void DoExecute()
{
std::ifstream file(this->GetParameterString("incsv"));
OutputMapType dataList;
UInt32ImageType::PixelType label;
FloatImageType::PixelType moist;
std::string line = "";
// Iterate through each line and split the content using delimeter, pass header
int i=0;
while (getline(file, line))
{
i+=1;
if(i==1)
continue;
std::vector<std::string> vec;
boost::algorithm::split(vec, line, boost::is_any_of(";"));
label = std::stoi(vec[0]);
moist = std::stof(vec[5]);
dataList.insert({label,moist});
}
// Close the File
file.close();
otbAppLogINFO("Exporting to raster file");
// Produce the map
//moistMap[0] = 0 ;
m_MapFilter = MoistMapFilterType::New();
m_MapFilter->SetInput(GetParameterUInt32Image("inlabels"));
m_MapFilter->GetFunctor().SetMap(dataList);
SetParameterOutputImage("out", m_MapFilter->GetOutput());
}
// Create a nice map
MoistMapFilterType::Pointer m_MapFilter;
};
}
}
OTB_APPLICATION_EXPORT(otb::Wrapper::ApplyCsvLabelsFile)
......@@ -5,7 +5,8 @@
//Stack
#include "otbTensorflowSource.h"
// Stats
#include "otbStreamingStatisticsMapFromLabelImageFilter.h"
//#include "otbStreamingStatisticsMapFromLabelImageFilter.h"
#include "otbStreamingStatisticsMapFromLabelImageFilter3.h"
// Tensorflow stuff
#include "tensorflow/core/public/session.h"
......@@ -47,7 +48,7 @@ public:
typedef TensorflowSource<FloatVectorImageType> TFSourceType;
/** Statistics */
typedef otb::StreamingStatisticsMapFromLabelImageFilter<FloatVectorImageType, UInt32ImageType> StatsFilterType;
typedef otb::StreamingStatisticsMapFromLabelImageFilter3<FloatVectorImageType, UInt32ImageType> StatsFilterType;
typedef itk::ImageRegionIterator<FloatVectorImageType> IteratorType;
typedef itk::ImageRegionConstIterator<FloatVectorImageType> ConstIteratorType;
......@@ -193,18 +194,20 @@ private:
//get count for each label
StatsFilterType::LabelPopulationMapType countValues = m_StatsFilter->GetLabelPopulationMap();
StatsFilterType::PixelValueMapType validRatioMap = m_StatsFilter->GetValidPixelsRatioMap();
// for (const auto& entry: meanValues)
// std::cout << "Id:" << entry.first << ", value=" << entry.second << std::endl;
// std::cout << "Id:105826152, value=" << meanValues.at(105826152) << std::endl;
// std::cout << "Id:105826152, validratio=" << validRatioMap.at(105826152) << std::endl;
// std::cout << "Id:105826152, pixparcelle=" << countValues.at(105826152) << std::endl;
// Put VV in dB
std::vector<long> wronglabels;
for (auto& entry: meanValues)
{
//Hassan filtre en trop
//countValues.at(entry.first) < 20 or
if (entry.second[0] == 0 or entry.second[1] == 0 or entry.second[2] == 0
double nbsarpix = countValues.at(entry.first)*validRatioMap.at(entry.first)[2];
if (validRatioMap.at(entry.first)[2] < 0.3 or entry.second[0] == 0 or entry.second[1] == 0 or entry.second[2] == 0
or isnan(entry.second[0]) or isnan(entry.second[1]) or isnan(entry.second[2])){
wronglabels.push_back(entry.first);
}else{
......@@ -222,7 +225,12 @@ private:
if (wronglabels.size() > 0)
RemoveNoDataLabelFromMap<StatsFilterType::PixelValueMapType>(meanValues,wronglabels);
//remove label stats in map
// for (auto& entry: meanValues)
// {
// entry.second.SetSize(3,false);
//
// }
// Invert the model
......
......@@ -56,7 +56,7 @@ private:
//
// // Output image
this->AddParameter(ParameterType_OutputImage, "out", "Output image");
SetParameterDescription ("out", "Mean image over the time serie");
SetParameterDescription ("out", "Mean image over the time serie blabla test");
}
......
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: otbStreamingStatisticsMapFromLabelImageFilter2.h
* Author: otbuser
*
* Created on January 14, 2021, 12:54 AM
*/
#ifndef OTBSTREAMINGSTATISTICSMAPFROMLABELIMAGEFILTER2_H
#define OTBSTREAMINGSTATISTICSMAPFROMLABELIMAGEFILTER2_H
#include "otbPersistentImageFilter.h"
#include "itkNumericTraits.h"
#include "itkArray.h"
#include "itkSimpleDataObjectDecorator.h"
#include "otbPersistentFilterStreamingDecorator.h"
#include <unordered_map>
namespace otb
{
template <class TInputVectorImage, class TLabelImage>
class ITK_EXPORT PersistentStreamingStatisticsMapFromLabelImageFilter2 : public PersistentStreamingStatisticsMapFromLabelImageFilter<TInputVectorImage, TInputVectorImage>
{
public:
/** Standard Self typedef */
typedef PersistentStreamingStatisticsMapFromLabelImageFilter2 Self;
typedef PersistentStreamingStatisticsMapFromLabelImageFilter<TInputVectorImage, TInputVectorImage> Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
/** Method for creation through the object factory. */
itkNewMacro(Self);
/** Runtime information support. */
itkTypeMacro(PersistentStreamingStatisticsMapFromLabelImageFilter2, PersistentStreamingStatisticsMapFromLabelImageFilter);
typedef typename LabelImageType::PixelType LabelPixelType;
typedef itk::VariableLengthVector<double> RealVectorValidPixelsRatioType;
typedef std::unordered_map<LabelPixelType, RealVectorValidPixelsRatioType> VectorValidPixelsRatioMapType;
/** Return the ratio between valid pixels for each band and pixels in each label */
VectorValidPixelsRatioMapType GetValidPixelsRatioMap() const;
void Synthetize(void) override;
private:
VectorValidPixelsRatioMapType m_CountPixel;
};
template <class TInputVectorImage, class TLabelImage>
class ITK_EXPORT StreamingStatisticsMapFromLabelImageFilter2
: public PersistentFilterStreamingDecorator<PersistentStreamingStatisticsMapFromLabelImageFilter2<TInputVectorImage, TLabelImage>>
{
public:
/** Standard Self typedef */
typedef StreamingStatisticsMapFromLabelImageFilter2 Self;
typedef PersistentFilterStreamingDecorator<PersistentStreamingStatisticsMapFromLabelImageFilter2<TInputVectorImage, TLabelImage>> Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
/** Type macro */
itkNewMacro(Self);
/** Creation through object factory macro */
itkTypeMacro(StreamingStatisticsMapFromLabelImageFilter2, StreamingStatisticsMapFromLabelImageFilter);
typedef typename Superclass::FilterType::VectorValidPixelsRatioMapType VectorValidPixelsRatioMapType;
/** Return the ratio between valid pixels for each band and pixels in each label */
VectorValidPixelsRatioMapType GetValidPixelsRatioMap() const
{
return this->GetFilter()->GetValidPixelsRatioMap();
}
};
}
#ifndef OTB_MANUAL_INSTANTIATION
#include "otbStreamingStatisticsMapFromLabelImageFilter2.hxx"
#endif
#endif /* OTBSTREAMINGSTATISTICSMAPFROMLABELIMAGEFILTER2_H */
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: otbStreamingStatisticsMapFromLabelImageFilter2.hxx
* Author: otbuser
*
* Created on January 14, 2021, 12:54 AM
*/
#ifndef OTBSTREAMINGSTATISTICSMAPFROMLABELIMAGEFILTER2_HXX
#define OTBSTREAMINGSTATISTICSMAPFROMLABELIMAGEFILTER2_HXX
#include "otbStreamingStatisticsMapFromLabelImageFilter2.h"
namespace otb
{
template <class TInputVectorImage, class TLabelImage>
typename PersistentStreamingStatisticsMapFromLabelImageFilter2<TInputVectorImage, TLabelImage>::VectorValidPixelsRatioMapType
PersistentStreamingStatisticsMapFromLabelImageFilter2<TInputVectorImage, TLabelImage>::GetValidPixelsRatioMap() const
{
return m_CountPixel;
}
template <class TInputVectorImage, class TLabelImage>
void PersistentStreamingStatisticsMapFromLabelImageFilter2<TInputVectorImage, TLabelImage>::Synthetize()
{
// Update temporary accumulator
AccumulatorMapType outputAcc;
auto endAcc = outputAcc.end();
for (auto const& threadAccMap : m_AccumulatorMaps)
{
for (auto const& it : threadAccMap)
{
auto label = it.first;
auto itAcc = outputAcc.find(label);
if (itAcc == endAcc)
{
outputAcc.emplace(label, it.second);
}
else
{
itAcc->second.Update(it.second);
}
}
}
// Publish output maps
for (auto& it : outputAcc)
{
const LabelPixelType label = it.first;
const auto& bandCount = it.second.GetBandCount();
const auto& sum = it.second.GetSum();
const auto& sqSum = it.second.GetSqSum();
// Count
m_LabelPopulation[label] = it.second.GetCount();
// Mean & stdev
RealVectorPixelType mean(sum);
RealVectorPixelType std(sqSum);
RealVectorValidPixelsRatioType vectorcount;
vectorcount.SetSize(mean.GetSize());
for (unsigned int band = 0; band < mean.GetSize(); band++)
{
// Number of valid pixels in band
auto count = bandCount[band];
// Ratio of valid pixels
vectorcount[band] = count/it.second.GetCount();
// Mean
mean[band] /= count;
// Unbiased standard deviation (not sure unbiased is useful here)
const double variance = (sqSum[band] - (sum[band] * mean[band])) / (count - 1);
std[band] = std::sqrt(variance);
}
m_CountPixel[label] = vectorcount;
m_MeanRadiometricValue[label] = mean;
m_StDevRadiometricValue[label] = std;
// Min & max
m_MinRadiometricValue[label] = it.second.GetMin();
m_MaxRadiometricValue[label] = it.second.GetMax();
}
}
}
#endif /* OTBSTREAMINGSTATISTICSMAPFROMLABELIMAGEFILTER2_HXX */
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: otbStreamingStatisticsMapFromLabelImageFilter2.h
* Author: otbuser
*
* Created on January 14, 2021, 12:54 AM
*/
#ifndef otbStreamingStatisticsMapFromLabelImageFilter3_h
#define otbStreamingStatisticsMapFromLabelImageFilter3_h
#include "otbPersistentImageFilter.h"
#include "itkNumericTraits.h"
#include "itkArray.h"
#include "itkSimpleDataObjectDecorator.h"
#include "otbPersistentFilterStreamingDecorator.h"
#include <unordered_map>
namespace otb
{
/** \class StatisticsMapAccumulator
* \brief Holds statistics for each label of a label image
*
* Intended to store and update the following statistics:
* -count
* -sum of values
* -sum of squared values
* -min
* -max
*
* TODO:
* -Better architecture?
* -Enrich with other statistics?
* -Move this class in a dedicated source to enable its use by other otbStatistics stuff?
*
* \ingroup OTBStatistics
*/
template <class TRealVectorPixelType>
class StatisticsAccumulator3
{
public:
typedef typename TRealVectorPixelType::ValueType RealValueType;
typedef uint64_t PixelCountType;
typedef itk::VariableLengthVector<PixelCountType> PixelCountVectorType;
// Constructor (default)
StatisticsAccumulator3() : m_Count(), m_NoDataValue(), m_UseNoDataValue()
{
}
// Constructor (initialize the accumulator with the given pixel)
StatisticsAccumulator3(RealValueType noDataValue, bool useNoDataValue, const TRealVectorPixelType& pixel)
: m_NoDataValue(noDataValue), m_Count(1), m_UseNoDataValue(useNoDataValue)
{
m_Count = 1;
m_Sum = pixel;
m_Min = pixel;
m_Max = pixel;
m_SqSum = pixel;
m_BandCount.SetSize(pixel.GetSize());
for (unsigned int band = 0; band < m_SqSum.GetSize(); band++)
{
auto val = pixel[band];
if (!m_UseNoDataValue || val != m_NoDataValue)
{
m_BandCount[band] = 1;
m_SqSum[band] *= m_SqSum[band];
}
else
{
m_BandCount[band] = 0;
}
}
}
// Function update (pixel)
void Update(const TRealVectorPixelType& pixel)
{
m_Count++;
const unsigned int nBands = pixel.GetSize();
for (unsigned int band = 0; band < nBands; band++)
{
const RealValueType value = pixel[band];
const RealValueType sqValue = value * value;
if(!m_UseNoDataValue || value != m_NoDataValue)
{
UpdateValues(1, value, sqValue, value, value, m_BandCount[band], m_Sum[band], m_SqSum[band], m_Min[band],
m_Max[band]);
}
}
}
// Function update (self)
void Update(const StatisticsAccumulator3& other)
{
m_Count += other.m_Count;
const unsigned int nBands = other.m_Sum.GetSize();
for (unsigned int band = 0; band < nBands; band++)
{
UpdateValues(other.m_BandCount[band], other.m_Sum[band], other.m_SqSum[band], other.m_Min[band], other.m_Max[band], m_BandCount[band], m_Sum[band],
m_SqSum[band], m_Min[band], m_Max[band]);
}
}
// Accessors
itkGetMacro(BandCount, PixelCountVectorType);
itkGetMacro(Sum, TRealVectorPixelType);
itkGetMacro(SqSum, TRealVectorPixelType);
itkGetMacro(Min, TRealVectorPixelType);
itkGetMacro(Max, TRealVectorPixelType);
itkGetMacro(Count, double);
private:
void UpdateValues(PixelCountType otherCount, RealValueType otherSum, RealValueType otherSqSum, RealValueType otherMin, RealValueType otherMax,
PixelCountType& count, RealValueType& sum, RealValueType& sqSum, RealValueType& min, RealValueType& max)
{
count += otherCount;
sum += otherSum;
sqSum += otherSqSum;
if (otherMin < min)
min = otherMin;
if (otherMax > max)
max = otherMax;
}
protected:
PixelCountVectorType m_BandCount;
TRealVectorPixelType m_Sum;
TRealVectorPixelType m_SqSum;
TRealVectorPixelType m_Min;
TRealVectorPixelType m_Max;
RealValueType m_NoDataValue;
PixelCountType m_Count;
bool m_UseNoDataValue;
};
/** \class PersistentStreamingStatisticsMapFromLabelImageFilter
* \brief Computes mean radiometric value for each label of a label image, based on a support VectorImage
*
* This filter persists its temporary data. It means that if you Update it n times on n different
* requested regions, the output statistics will be the statitics of the whole set of n regions.
*
* To reset the temporary data, one should call the Reset() function.
*
* To get the statistics once the regions have been processed via the pipeline, use the Synthetize() method.
*
*
* \sa StreamingStatisticsMapFromLabelImageFilter
* \ingroup Streamed
* \ingroup Multithreaded
* \ingroup MathematicalStatisticsImageFilters
*
* \ingroup OTBStatistics
*/
template <class TInputVectorImage, class TLabelImage>
class ITK_EXPORT PersistentStreamingStatisticsMapFromLabelImageFilter3 : public PersistentImageFilter<TInputVectorImage, TInputVectorImage>
{
public:
/** Standard Self typedef */
typedef PersistentStreamingStatisticsMapFromLabelImageFilter3 Self;
typedef PersistentImageFilter<TInputVectorImage, TInputVectorImage> Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
/** Method for creation through the object factory. */