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 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
|
/*
* Copyright (C) 2005-2017 Centre National d'Etudes Spatiales (CNES)
*
* This file is part of Orfeo Toolbox
*
* https://www.orfeo-toolbox.org/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef otbComputeHistoFilter_txx
#define otbComputeHistoFilter_txx
#include "otbComputeHistoFilter.h"
#include <limits>
namespace otb
{
template <class TInputImage, class TOutputImage >
ComputeHistoFilter < TInputImage , TOutputImage >
::ComputeHistoFilter()
{
this->SetNumberOfRequiredOutputs(2);
this->SetNthOutput( 0, this->MakeOutput(0) );
this->SetNthOutput( 1, this->MakeOutput(1) );
m_Min = std::numeric_limits< InputPixelType >::quiet_NaN();
m_Max = std::numeric_limits< InputPixelType >::quiet_NaN();
m_NoDataFlag = false;
m_NoData = std::numeric_limits< InputPixelType >::quiet_NaN();
m_NbBin = 256;
m_Threshold = -1;
m_ThumbSize.Fill(0);
m_ValidThreads = 1;
m_Step = -1;
}
template <class TInputImage, class TOutputImage >
itk::ProcessObject::DataObjectPointer
ComputeHistoFilter < TInputImage , TOutputImage >
::MakeOutput(itk::ProcessObject::DataObjectPointerArraySizeType idx)
{
itk::DataObject::Pointer output;
switch ( idx )
{
case 0:
output = ( OutputImageType::New() ).GetPointer();
break;
case 1:
output = ( OutputImageType::New() ).GetPointer();
break;
default:
std::cerr << "No output " << idx << std::endl;
output = NULL;
break;
}
return output;
}
template <class TInputImage, class TOutputImage >
itk::ProcessObject::DataObjectPointer
ComputeHistoFilter < TInputImage , TOutputImage >
::MakeOutput(const itk::ProcessObject::DataObjectIdentifierType & name)
{
return Superclass::MakeOutput( name );
}
template <class TInputImage, class TOutputImage >
void ComputeHistoFilter < TInputImage , TOutputImage >
::GenerateInputRequestedRegion()
{
typename Superclass::InputImagePointer inputPtr (
const_cast<InputImageType *>( this->GetInput() ) );
inputPtr->SetRequestedRegion( this->GetOutput()->GetRequestedRegion() );
if ( inputPtr->GetRequestedRegion().GetNumberOfPixels() == 0 )
{
inputPtr->SetRequestedRegionToLargestPossibleRegion();
}
}
template <class TInputImage, class TOutputImage >
void ComputeHistoFilter < TInputImage , TOutputImage >
::GenerateOutputInformation()
{
Superclass::GenerateOutputInformation();
typename InputImageType::ConstPointer input ( this->GetInput() );
typename OutputImageType::Pointer output ( this->GetHistoOutput() );
typename OutputImageType::Pointer outImage ( this->GetOutput() );
typename InputImageType::RegionType inputLargestRegion (
input->GetLargestPossibleRegion());
outImage->SetLargestPossibleRegion( inputLargestRegion );
typename OutputImageType::IndexType start;
typename OutputImageType::SizeType size;
start.Fill(0);
assert( m_ThumbSize[0] != 0 );
assert( m_ThumbSize[1] != 0 );
//TODO throw error if 0
size[0] = std::ceil( inputLargestRegion.GetSize()[0] /
static_cast< double > ( m_ThumbSize[0] ) );
size[1] = std::ceil( inputLargestRegion.GetSize()[1] /
static_cast< double > ( m_ThumbSize[1] ) );
typename OutputImageType::RegionType region;
region.SetSize(size);
region.SetIndex(start);
output->SetNumberOfComponentsPerPixel(m_NbBin);
output->SetLargestPossibleRegion(region);
typename InputImageType::SpacingType inputSpacing ( input->GetSignedSpacing() );
typename InputImageType::PointType inputOrigin ( input->GetOrigin() );
typename OutputImageType::SpacingType histoSpacing ;
histoSpacing[0] = inputSpacing[0] * m_ThumbSize[0] ;
histoSpacing[1] = inputSpacing[1] * m_ThumbSize[1] ;
output->SetSignedSpacing( histoSpacing ) ;
typename OutputImageType::PointType histoOrigin ;
histoOrigin[0] = histoSpacing[0] / 2 + inputOrigin[0] - inputSpacing[0] / 2 ;
histoOrigin[1] = histoSpacing[1] / 2 + inputOrigin[1] - inputSpacing[1] / 2 ;
output->SetOrigin( histoOrigin );
}
template <class TInputImage, class TOutputImage >
void ComputeHistoFilter < TInputImage , TOutputImage >
::GenerateOutputRequestedRegion( itk::DataObject * itkNotUsed(output) )
{
if ( GetHistoOutput()->GetRequestedRegion().GetNumberOfPixels() == 0 )
{
GetHistoOutput()->SetRequestedRegionToLargestPossibleRegion();
}
typename OutputImageType::Pointer outImage ( this->GetOutput() );
SetRequestedRegion( outImage );
}
template <class TInputImage, class TOutputImage >
void ComputeHistoFilter < TInputImage , TOutputImage >
::GenerateData()
{
this->AllocateOutputs();
// Set up the multithreaded processing
typename itk::ImageSource<OutputImageType>::ThreadStruct str;
str.Filter = this;
// Get the output pointer
const OutputImageType * outputPtr ( this->GetOutput() );
const itk::ImageRegionSplitterBase * splitter (
this->GetImageRegionSplitter() );
m_ValidThreads =
splitter->GetNumberOfSplits( outputPtr->GetRequestedRegion() ,
this->GetNumberOfThreads() );
this->BeforeThreadedGenerateData();
this->GetMultiThreader()->SetNumberOfThreads( m_ValidThreads );
this->GetMultiThreader()->SetSingleMethod(this->ThreaderCallback, &str);
// multithread the execution
this->GetMultiThreader()->SingleMethodExecute();
this->AfterThreadedGenerateData();
}
template <class TInputImage, class TOutputImage >
void ComputeHistoFilter < TInputImage , TOutputImage >
::BeforeThreadedGenerateData()
{
// Initializing output
typename OutputImageType::Pointer output ( this->GetHistoOutput() );
typename OutputImageType::PixelType zeroPixel(m_NbBin) ;
zeroPixel.Fill(0);
output->FillBuffer( zeroPixel );
// Initializing shared variable with thread number parameter
SizeType outSize ( output->GetRequestedRegion().GetSize() );
m_HistoThread.resize( m_ValidThreads * outSize[0] * outSize[1] );
m_HistoThread.shrink_to_fit();
std::fill( m_HistoThread.begin() , m_HistoThread.end() , zeroPixel );
m_Step = static_cast<double>( m_Max - m_Min ) \
/ static_cast<double>( m_NbBin -1 );
}
template <class TInputImage, class TOutputImage >
void ComputeHistoFilter < TInputImage , TOutputImage >
::ThreadedGenerateData(const OutputImageRegionType & outputRegionForThread ,
itk::ThreadIdType threadId )
{
assert(m_Step>0);
// TODO throw error
// itk::ProgressReporter progress(this , threadId ,
// outputRegionForThread.GetNumberOfPixels() );
typename InputImageType::ConstPointer input ( this->GetInput() );
typename OutputImageType::Pointer output ( this->GetHistoOutput() );
OutputImageRegionType histoRegion (
GetHistoOutput()->GetRequestedRegion() );
SizeType outSize ( histoRegion.GetSize() );
IndexType outIndex ( histoRegion.GetIndex() );
typename InputImageType::RegionType region;
unsigned int threadIndex ( threadId * outSize[0] * outSize[1] ) , pixel(0) ;
for ( unsigned int nthHisto = 0 ;
nthHisto < outSize[0] * outSize[1] ; nthHisto++ )
{
IndexType start;
start[0] = ( outIndex[0] + nthHisto % outSize[0] ) * m_ThumbSize[0];
start[1] = ( outIndex[1] + nthHisto / outSize[0] ) * m_ThumbSize[1];
region.SetSize( m_ThumbSize );
region.SetIndex(start);
if ( !region.Crop( outputRegionForThread ) )
continue;
typename itk::ImageRegionConstIterator < InputImageType >
it( input , region );
InputPixelType currentPixel(0);
for ( it.GoToBegin() ; !it.IsAtEnd() ; ++it )
{
currentPixel = it.Get();
if( ( currentPixel == m_NoData && m_NoDataFlag ) ||
currentPixel > m_Max || currentPixel < m_Min )
continue;
pixel = static_cast< unsigned int >(
std::round( ( currentPixel - m_Min ) / m_Step ) );
++m_HistoThread[threadIndex + nthHisto][pixel];
}
}
}
template <class TInputImage, class TOutputImage >
void ComputeHistoFilter < TInputImage , TOutputImage >
::AfterThreadedGenerateData()
{
typename OutputImageType::Pointer output ( this->GetHistoOutput() );
typename itk::ImageRegionIterator < OutputImageType >
oit( output , output->GetRequestedRegion() );
OutputImageRegionType histoRegion (
GetHistoOutput()->GetRequestedRegion() );
SizeType outSize ( histoRegion.GetSize() );
IndexType outIndex ( histoRegion.GetIndex() );
unsigned int agreg(0) , total(0) ;
for ( oit.GoToBegin() ; !oit.IsAtEnd() ; ++oit )
{
total = 0;
for ( unsigned int i = 0 ; i < m_NbBin ; i++ )
{
agreg = 0;
for ( unsigned int threadId = 0 ; threadId < m_ValidThreads ; threadId++ )
{
agreg += m_HistoThread[ threadId * outSize[0] * outSize[1]
+ ( oit.GetIndex()[1] - outIndex[1] ) * outSize[0]
+ ( ( oit.GetIndex()[0] - outIndex[0] ) ) ][i] ;
}
oit.Get()[i] = agreg;
total += agreg;
}
if ( m_Threshold > 0 )
ApplyThreshold( oit , total );
}
}
template <class TInputImage, class TOutputImage >
void ComputeHistoFilter < TInputImage , TOutputImage >
::ApplyThreshold( typename itk::ImageRegionIterator < OutputImageType > oit ,
unsigned int total )
{
unsigned int rest(0);
unsigned int height ( static_cast<unsigned int>(
m_Threshold * ( total / m_NbBin ) ) );
// Do i need to static_cast in a an assignation?
// Warning!!!! Need to handle out of bound int!!!
for( unsigned int i = 0 ; i < m_NbBin ; i++ )
{
if ( static_cast<unsigned int>( oit.Get()[i] ) > height )
{
rest += oit.Get()[i] - height ;
oit.Get()[i] = height ;
}
}
height = rest / m_NbBin;
rest = rest % m_NbBin;
for( unsigned int i = 0 ; i < m_NbBin ; i++ )
{
oit.Get()[i] += height ;
if ( i > (m_NbBin - rest)/2 && i <= (m_NbBin - rest)/2 + rest )
{
++oit.Get()[i];
}
}
}
template <class TInputImage, class TOutputImage >
typename TOutputImage::Pointer ComputeHistoFilter < TInputImage , TOutputImage >
::GetHistoOutput()
{
assert( this->itk::ProcessObject::GetOutput( 1 ) );
return dynamic_cast< TOutputImage * >(
this->itk::ProcessObject::GetOutput(1) );
}
template <class TInputImage, class TOutputImage >
void ComputeHistoFilter < TInputImage , TOutputImage >
::SetRequestedRegion( itk::ImageBase<2> * image )
{
OutputImageRegionType histoRegion (
GetHistoOutput()->GetRequestedRegion() );
IndexType start ;
start[0] = histoRegion.GetIndex()[0] * m_ThumbSize[0];
start[1] = histoRegion.GetIndex()[1] * m_ThumbSize[1];
SizeType size ;
size[0] = histoRegion.GetSize()[0] * m_ThumbSize[0];
size[1] = histoRegion.GetSize()[1] * m_ThumbSize[1];
typename OutputImageType::RegionType outputRequestedRegion;
outputRequestedRegion.SetIndex( start );
outputRequestedRegion.SetSize( size );
outputRequestedRegion.Crop( image->GetLargestPossibleRegion() );
image->SetRequestedRegion( outputRequestedRegion );
}
template <class TInputImage , class TOutputImage >
void ComputeHistoFilter < TInputImage , TOutputImage >
::PrintSelf(std::ostream& os, itk::Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "Is no data activated: " << m_NoDataFlag << std::endl;
os << indent << "No Data: " << m_NoData << std::endl;
os << indent << "Minimum: " << m_Min << std::endl;
os << indent << "Maximum: " << m_Max << std::endl;
os << indent << "Step: " << m_Step << std::endl;
os << indent << "Number of bin: " << m_NbBin << std::endl;
os << indent << "Thumbnail size: " << m_ThumbSize << std::endl;
os << indent << "Threshold value: " << m_Threshold << std::endl;
}
} // End namespace otb
#endif
|