/usr/include/OTB-5.8/otbMorphologicalPyramidSegmenter.txx is in libotb-dev 5.8.0+dfsg-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /*=========================================================================
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 otbMorphologicalPyramidSegmenter_txx
#define otbMorphologicalPyramidSegmenter_txx
#include "otbMorphologicalPyramidSegmenter.h"
#include "otbImage.h"
#include "itkUnaryFunctorImageFilter.h"
#include "itkConnectedThresholdImageFilter.h"
#include "itkConnectedComponentImageFilter.h"
#include "itkRelabelComponentImageFilter.h"
#include "itkThresholdImageFilter.h"
#include "itkMultiplyImageFilter.h"
#include "itkCastImageFilter.h"
#include "itkInvertIntensityImageFilter.h"
#include "itkRescaleIntensityImageFilter.h"
#include "otbThresholdImageToPointSetFilter.h"
#include "itkScalarImageToHistogramGenerator.h"
#include "itkMinimumMaximumImageCalculator.h"
namespace otb
{
namespace MorphologicalPyramid
{
/**
* Constructor
*/
template <class TInputImage, class TOutputImage>
Segmenter<TInputImage, TOutputImage>
::Segmenter()
{
this->SetNumberOfRequiredInputs(2);
m_SeedsQuantile = 0.9;
m_MinimumObjectSize = 10;
m_SegmentDarkDetailsBool = false;
m_NumberOfObjects = 0;
m_ConnectedThresholdQuantile = 0.9;
}
/**
* Set the details image.
* \param detailsImage The details image from the morphological pyramid
*/
template <class TInputImage, class TOutputImage>
void
Segmenter<TInputImage, TOutputImage>
::SetDetailsImage(const InputImageType * detailsImage)
{
this->SetNthInput(0, const_cast<TInputImage *>(detailsImage));
}
/**
* Set the details image.
* \return detailsImage The input details image.
*/
template <class TInputImage, class TOutputImage>
typename Segmenter<TInputImage, TOutputImage>::InputImageType *
Segmenter<TInputImage, TOutputImage>
::GetDetailsImage(void)
{
return const_cast<InputImageType *>(this->GetInput(0));
}
/**
* Set the original image.
* \param originalImage The original image to segment.
*/
template <class TInputImage, class TOutputImage>
void
Segmenter<TInputImage, TOutputImage>
::SetOriginalImage(const InputImageType * originalImage)
{
this->SetNthInput(1, const_cast<TInputImage *>(originalImage));
}
/**
* Get the original image.
* \return originalImage The original image to segment.
*/
template <class TInputImage, class TOutputImage>
typename Segmenter<TInputImage, TOutputImage>::InputImageType *
Segmenter<TInputImage, TOutputImage>
::GetOriginalImage(void)
{
return const_cast<InputImageType *>(this->GetInput(1));
}
/**
* Configure the input datas.
*/
template <class TInputImage, class TOutputImage>
void
Segmenter<TInputImage, TOutputImage>
::GenerateInputRequestedRegion()
{
// call the superclass' implementation of this method
Superclass::GenerateInputRequestedRegion();
// get pointers to the inputs
InputImagePointerType detailsPtr =
const_cast<InputImageType *>(this->GetInput(0));
InputImagePointerType origPtr =
const_cast<InputImageType *>(this->GetInput(1));
if (!detailsPtr || !origPtr)
{
return;
}
// We need to
// configure the inputs such that all the data is available.
detailsPtr->SetRequestedRegion(detailsPtr->GetLargestPossibleRegion());
origPtr->SetRequestedRegion(origPtr->GetLargestPossibleRegion());
}
/**
* Configure the output data
*/
template <class TInputImage, class TOutputImage>
void
Segmenter<TInputImage, TOutputImage>
::EnlargeOutputRequestedRegion(void)
{
this->GetOutput()
->SetRequestedRegion(this->GetOutput()->GetLargestPossibleRegion());
}
/**
* Main computation method
*/
template <class TInputImage, class TOutputImage>
void
Segmenter<TInputImage, TOutputImage>
::GenerateData()
{
// Input images pointers
InputImagePointerType details = this->GetDetailsImage();
InputImagePointerType original = this->GetOriginalImage();
// Typedefs for details image enhancement
typedef double FloatPixelType;
typedef otb::Image<FloatPixelType, InputImageType::ImageDimension> FloatImageType;
typedef itk::InvertIntensityImageFilter<InputImageType, InputImageType> InvertFilterType;
typedef itk::MultiplyImageFilter<FloatImageType, FloatImageType, InputImageType> MultiplyFilterType;
typedef itk::CastImageFilter<InputImageType, FloatImageType> CastImageFilterType;
typedef itk::MinimumMaximumImageCalculator<InputImageType> MinMaxCalculatorType;
//Typedefs for seeds extraction
typedef itk::PointSet<InputPixelType, InputImageType::ImageDimension> PointSetType;
typedef otb::ThresholdImageToPointSetFilter<InputImageType, PointSetType> PointSetFilterType;
typedef typename PointSetType::PointsContainer::Iterator PointSetIteratorType;
// Typedefs for segmentation
typedef itk::ConnectedThresholdImageFilter<InputImageType, InputImageType> ConnectedFilterType;
typedef itk::ConnectedComponentImageFilter<InputImageType, OutputImageType> LabelFilterType;
typedef itk::RelabelComponentImageFilter<OutputImageType, OutputImageType> RelabelFilterType;
typedef itk::ThresholdImageFilter<OutputImageType> ThresholdFilterType;
// Typedefs for statistics computation
typedef itk::Statistics::ScalarImageToHistogramGenerator<InputImageType> HistGeneratorType;
/////////////////////////////////////
//// Details image enhancement //////
/////////////////////////////////////
// Filters instantiation
typename InvertFilterType::Pointer invert;
typename CastImageFilterType::Pointer cast1 = CastImageFilterType::New();
typename CastImageFilterType::Pointer cast2 = CastImageFilterType::New();
typename MultiplyFilterType::Pointer mult = MultiplyFilterType::New();
typename MinMaxCalculatorType::Pointer minMax = MinMaxCalculatorType::New();
// Pipeline connection
cast1->SetInput(details);
minMax->SetImage(original);
minMax->ComputeMaximum();
// If we want to segment darker detail, the original image must have its itensity inverted
if (m_SegmentDarkDetailsBool)
{
invert = InvertFilterType::New();
invert->SetInput(original);
invert->SetMaximum(minMax->GetMaximum());
cast2->SetInput(invert->GetOutput());
}
else
{
cast2->SetInput(original);
}
mult->SetInput1(cast1->GetOutput());
mult->SetInput2(cast2->GetOutput());
mult->Update();
/////////////////////////////////////
//// Thresholds computation /////////
/////////////////////////////////////
// Filter instantiation
typename HistGeneratorType::Pointer histogram = HistGeneratorType::New();
// Seeds Threshold is computed from the quantile
histogram->SetInput(details);
histogram->SetNumberOfBins(255);
histogram->SetMarginalScale(10.0);
histogram->Compute();
InputPixelType pointSetThreshold =
static_cast<InputPixelType>(histogram->GetOutput()->Quantile(0, m_SeedsQuantile));
// Segmentation Threshold is computed from the quantile
histogram = HistGeneratorType::New();
histogram->SetInput(mult->GetOutput());
histogram->SetNumberOfBins(255);
histogram->SetMarginalScale(10.0);
histogram->Compute();
InputPixelType connectedThresholdValue =
static_cast<InputPixelType>(histogram->GetOutput()->Quantile(0, m_ConnectedThresholdQuantile));
/////////////////////////////////////
//// Seeds extraction ///////////////
/////////////////////////////////////
typename PointSetFilterType::Pointer pointSetFilter = PointSetFilterType::New();
pointSetFilter->SetInput(0, details);
pointSetFilter->SetLowerThreshold(pointSetThreshold);
pointSetFilter->Update();
/////////////////////////////////////
//// Segmentation ///////////////////
/////////////////////////////////////
// Filters instantiation
typename ConnectedFilterType::Pointer connectedThreshold = ConnectedFilterType::New();
typename LabelFilterType::Pointer labeler = LabelFilterType::New();
typename RelabelFilterType::Pointer relabeler = RelabelFilterType::New();
typename ThresholdFilterType::Pointer threshold = ThresholdFilterType::New();
//Passing seeds to the connected filter
connectedThreshold = ConnectedFilterType::New();
connectedThreshold->ClearSeeds();
connectedThreshold->SetInput(mult->GetOutput());
PointSetIteratorType it = pointSetFilter->GetOutput()->GetPoints()->Begin();
while (it != pointSetFilter->GetOutput()->GetPoints()->End())
{
typename OutputImageType::IndexType index;
index[0] = static_cast<long int>(it.Value()[0]);
index[1] = static_cast<long int>(it.Value()[1]);
connectedThreshold->AddSeed(index);
++it;
}
// segmentation
connectedThreshold->SetLower(connectedThresholdValue);
// labelling
labeler = LabelFilterType::New();
relabeler = RelabelFilterType::New();
labeler->SetInput(connectedThreshold->GetOutput());
relabeler->SetInput(labeler->GetOutput());
relabeler->SetMinimumObjectSize(m_MinimumObjectSize);
relabeler->Update();
// In some cases it might happen that the whole extent of the image is segmented as a single region.
// Since this is not desirable, we test this case here to avoid it.
threshold = ThresholdFilterType::New();
threshold->SetInput(relabeler->GetOutput());
OutputPixelType num = 0;
if (relabeler->GetNumberOfObjects() == 1)
{
unsigned int surface = mult->GetOutput()->GetLargestPossibleRegion().GetSize()[0]
* mult->GetOutput()->GetLargestPossibleRegion().GetSize()[1];
if (relabeler->GetSizeOfObjectsInPixels()[0] == surface)
{
num = 0;
}
else
{
num = 1;
}
}
else
{
num = static_cast<OutputPixelType>(relabeler->GetNumberOfObjects());
}
threshold->ThresholdOutside(0, num);
// Output connection
threshold->GraftOutput(this->GetOutput());
threshold->Update();
this->GraftOutput(threshold->GetOutput());
m_NumberOfObjects = num;
}
/**
* PrintSelf method
*/
template <class TInputImage, class TOutputImage>
void
Segmenter<TInputImage, TOutputImage>
::PrintSelf(std::ostream& os, itk::Indent indent) const
{
Superclass::PrintSelf(os, indent);
}
} // End namespace MorphologicalPyramid
} // End namespace otb
#endif
|