1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
|
/*=========================================================================
Program: ORFEO Toolbox
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
See OTBCopyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef otbImageClassificationFilter_txx
#define otbImageClassificationFilter_txx
#include "otbImageClassificationFilter.h"
#include "itkImageRegionIterator.h"
#include "itkProgressReporter.h"
namespace otb
{
/**
* Constructor
*/
template <class TInputImage, class TOutputImage, class TMaskImage>
ImageClassificationFilter<TInputImage, TOutputImage, TMaskImage>
::ImageClassificationFilter()
{
this->SetNumberOfIndexedInputs(2);
this->SetNumberOfRequiredInputs(1);
m_DefaultLabel = itk::NumericTraits<LabelType>::ZeroValue();
this->SetNumberOfRequiredOutputs(2);
this->SetNthOutput(0,TOutputImage::New());
this->SetNthOutput(1,ConfidenceImageType::New());
m_UseConfidenceMap = false;
m_BatchMode = true;
}
template <class TInputImage, class TOutputImage, class TMaskImage>
void
ImageClassificationFilter<TInputImage, TOutputImage, TMaskImage>
::SetInputMask(const MaskImageType * mask)
{
this->itk::ProcessObject::SetNthInput(1, const_cast<MaskImageType *>(mask));
}
template <class TInputImage, class TOutputImage, class TMaskImage>
const typename ImageClassificationFilter<TInputImage, TOutputImage, TMaskImage>
::MaskImageType *
ImageClassificationFilter<TInputImage, TOutputImage, TMaskImage>
::GetInputMask()
{
if (this->GetNumberOfInputs() < 2)
{
return ITK_NULLPTR;
}
return static_cast<const MaskImageType *>(this->itk::ProcessObject::GetInput(1));
}
template <class TInputImage, class TOutputImage, class TMaskImage>
typename ImageClassificationFilter<TInputImage, TOutputImage, TMaskImage>
::ConfidenceImageType *
ImageClassificationFilter<TInputImage, TOutputImage, TMaskImage>
::GetOutputConfidence()
{
if (this->GetNumberOfOutputs() < 2)
{
return ITK_NULLPTR;
}
return static_cast<ConfidenceImageType *>(this->itk::ProcessObject::GetOutput(1));
}
template <class TInputImage, class TOutputImage, class TMaskImage>
void
ImageClassificationFilter<TInputImage, TOutputImage, TMaskImage>
::BeforeThreadedGenerateData()
{
if (!m_Model)
{
itkGenericExceptionMacro(<< "No model for classification");
}
if(m_BatchMode)
{
#ifdef _OPENMP
// OpenMP will take care of threading
this->SetNumberOfThreads(1);
#endif
}
}
template <class TInputImage, class TOutputImage, class TMaskImage>
void
ImageClassificationFilter<TInputImage, TOutputImage, TMaskImage>
::ClassicThreadedGenerateData(const OutputImageRegionType& outputRegionForThread, itk::ThreadIdType threadId)
{
// Get the input pointers
InputImageConstPointerType inputPtr = this->GetInput();
MaskImageConstPointerType inputMaskPtr = this->GetInputMask();
OutputImagePointerType outputPtr = this->GetOutput();
ConfidenceImagePointerType confidencePtr = this->GetOutputConfidence();
// Progress reporting
itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
// Define iterators
typedef itk::ImageRegionConstIterator<InputImageType> InputIteratorType;
typedef itk::ImageRegionConstIterator<MaskImageType> MaskIteratorType;
typedef itk::ImageRegionIterator<OutputImageType> OutputIteratorType;
typedef itk::ImageRegionIterator<ConfidenceImageType> ConfidenceMapIteratorType;
InputIteratorType inIt(inputPtr, outputRegionForThread);
OutputIteratorType outIt(outputPtr, outputRegionForThread);
// Eventually iterate on masks
MaskIteratorType maskIt;
if (inputMaskPtr)
{
maskIt = MaskIteratorType(inputMaskPtr, outputRegionForThread);
maskIt.GoToBegin();
}
// setup iterator for confidence map
bool computeConfidenceMap(m_UseConfidenceMap && m_Model->HasConfidenceIndex() && !m_Model->GetRegressionMode());
ConfidenceMapIteratorType confidenceIt;
if (computeConfidenceMap)
{
confidenceIt = ConfidenceMapIteratorType(confidencePtr,outputRegionForThread);
confidenceIt.GoToBegin();
}
bool validPoint = true;
double confidenceIndex = 0.0;
// Walk the part of the image
for (inIt.GoToBegin(), outIt.GoToBegin(); !inIt.IsAtEnd() && !outIt.IsAtEnd(); ++inIt, ++outIt)
{
// Check pixel validity
if (inputMaskPtr)
{
validPoint = maskIt.Get() > 0;
++maskIt;
}
// If point is valid
if (validPoint)
{
// Classifify
if (computeConfidenceMap)
{
outIt.Set(m_Model->Predict(inIt.Get(),&confidenceIndex)[0]);
}
else
{
outIt.Set(m_Model->Predict(inIt.Get())[0]);
}
}
else
{
// else, set default value
outIt.Set(m_DefaultLabel);
confidenceIndex = 0.0;
}
if (computeConfidenceMap)
{
confidenceIt.Set(confidenceIndex);
++confidenceIt;
}
progress.CompletedPixel();
}
}
template <class TInputImage, class TOutputImage, class TMaskImage>
void
ImageClassificationFilter<TInputImage, TOutputImage, TMaskImage>
::BatchThreadedGenerateData(const OutputImageRegionType& outputRegionForThread, itk::ThreadIdType threadId)
{
bool computeConfidenceMap(m_UseConfidenceMap && m_Model->HasConfidenceIndex()
&& !m_Model->GetRegressionMode());
// Get the input pointers
InputImageConstPointerType inputPtr = this->GetInput();
MaskImageConstPointerType inputMaskPtr = this->GetInputMask();
OutputImagePointerType outputPtr = this->GetOutput();
ConfidenceImagePointerType confidencePtr = this->GetOutputConfidence();
// Progress reporting
itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
// Define iterators
typedef itk::ImageRegionConstIterator<InputImageType> InputIteratorType;
typedef itk::ImageRegionConstIterator<MaskImageType> MaskIteratorType;
typedef itk::ImageRegionIterator<OutputImageType> OutputIteratorType;
typedef itk::ImageRegionIterator<ConfidenceImageType> ConfidenceMapIteratorType;
InputIteratorType inIt(inputPtr, outputRegionForThread);
OutputIteratorType outIt(outputPtr, outputRegionForThread);
MaskIteratorType maskIt;
if (inputMaskPtr)
{
maskIt = MaskIteratorType(inputMaskPtr, outputRegionForThread);
maskIt.GoToBegin();
}
// typedef typename ModelType::InputValueType InputValueType;
typedef typename ModelType::InputSampleType InputSampleType;
typedef typename ModelType::InputListSampleType InputListSampleType;
typedef typename ModelType::TargetValueType TargetValueType;
// typedef typename ModelType::TargetSampleType TargetSampleType;
typedef typename ModelType::TargetListSampleType TargetListSampleType;
// typedef typename ModelType::ConfidenceValueType ConfidenceValueType;
// typedef typename ModelType::ConfidenceSampleType ConfidenceSampleType;
typedef typename ModelType::ConfidenceListSampleType ConfidenceListSampleType;
typename InputListSampleType::Pointer samples = InputListSampleType::New();
unsigned int num_features = inputPtr->GetNumberOfComponentsPerPixel();
samples->SetMeasurementVectorSize(num_features);
InputSampleType sample(num_features);
// Fill the samples
bool validPoint = true;
for (inIt.GoToBegin(); !inIt.IsAtEnd(); ++inIt)
{
// Check pixel validity
if (inputMaskPtr)
{
validPoint = maskIt.Get() > 0;
++maskIt;
}
if(validPoint)
{
typename InputImageType::PixelType pix = inIt.Get();
for(size_t feat=0; feat<num_features; ++feat)
{
sample[feat]=pix[feat];
}
samples->PushBack(sample);
}
}
//Make the batch prediction
typename TargetListSampleType::Pointer labels;
typename ConfidenceListSampleType::Pointer confidences;
if(computeConfidenceMap)
confidences = ConfidenceListSampleType::New();
// This call is threadsafe
labels = m_Model->PredictBatch(samples,confidences);
// Set the output values
ConfidenceMapIteratorType confidenceIt;
if (computeConfidenceMap)
{
confidenceIt = ConfidenceMapIteratorType(confidencePtr,outputRegionForThread);
confidenceIt.GoToBegin();
}
typename TargetListSampleType::ConstIterator labIt = labels->Begin();
maskIt.GoToBegin();
for (outIt.GoToBegin(); labIt!=labels->End() && !outIt.IsAtEnd();
++outIt)
{
double confidenceIndex = 0.0;
TargetValueType labelValue(m_DefaultLabel);
if (inputMaskPtr)
{
validPoint = maskIt.Get() > 0;
++maskIt;
}
if (validPoint)
{
labelValue = labIt.GetMeasurementVector()[0];
if(computeConfidenceMap)
{
confidenceIndex = confidences->GetMeasurementVector(labIt.GetInstanceIdentifier())[0];
}
++labIt;
}
outIt.Set(labelValue);
if(computeConfidenceMap)
{
confidenceIt.Set(confidenceIndex);
++confidenceIt;
}
progress.CompletedPixel();
}
}
template <class TInputImage, class TOutputImage, class TMaskImage>
void
ImageClassificationFilter<TInputImage, TOutputImage, TMaskImage>
::ThreadedGenerateData(const OutputImageRegionType& outputRegionForThread, itk::ThreadIdType threadId)
{
if(m_BatchMode)
{
this->BatchThreadedGenerateData(outputRegionForThread, threadId);
}
else
{
this->ClassicThreadedGenerateData(outputRegionForThread, threadId);
}
}
/**
* PrintSelf Method
*/
template <class TInputImage, class TOutputImage, class TMaskImage>
void
ImageClassificationFilter<TInputImage, TOutputImage, TMaskImage>
::PrintSelf(std::ostream& os, itk::Indent indent) const
{
Superclass::PrintSelf(os, indent);
}
} // End namespace otb
#endif
|