18 #ifndef itkBoxUtilities_h
19 #define itkBoxUtilities_h
44 template<
typename TIterator >
49 typename TIterator::OffsetType offset;
50 it->ClearActiveList();
51 if ( !fullyConnected )
56 for (
unsigned int d = 0; d < TIterator::Dimension; ++d )
59 it->ActivateOffset(offset);
67 unsigned int centerIndex = it->GetCenterNeighborhoodIndex();
68 for (
unsigned int d = 0; d < centerIndex; d++ )
70 offset = it->GetOffset(d);
73 for (
unsigned int i = 0; i < TIterator::Dimension; i++ )
83 it->ActivateOffset(offset);
87 it->DeactivateOffset(offset);
92 template<
typename TInputImage,
typename TOutputImage >
95 const TOutputImage *outputImage,
96 typename TInputImage::RegionType inputRegion,
97 typename TOutputImage::RegionType outputRegion,
101 typedef TInputImage InputImageType;
102 typedef typename TInputImage::OffsetType OffsetType;
103 typedef TOutputImage OutputImageType;
104 typedef typename TOutputImage::PixelType OutputPixelType;
109 InputIterator inIt(inputImage, inputRegion);
110 typename TInputImage::SizeType kernelRadius;
111 kernelRadius.Fill(1);
113 NOutputIterator noutIt(kernelRadius, outputImage, outputRegion);
119 noutIt.OverrideBoundaryCondition(&oBC);
129 std::vector< int > Weights;
130 typename NOutputIterator::ConstIterator sIt;
131 for (
typename NOutputIterator::IndexListType::const_iterator idxIt = noutIt.GetActiveIndexList().begin();
132 idxIt != noutIt.GetActiveIndexList().end();
135 OffsetType offset = noutIt.GetOffset(*idxIt);
137 for (
unsigned int k = 0; k < InputImageType::ImageDimension; k++ )
139 if ( offset[k] != 0 )
145 Weights.push_back(w);
148 for ( inIt.GoToBegin(), noutIt.GoToBegin(); !noutIt.IsAtEnd(); ++inIt, ++noutIt )
150 OutputPixelType Sum = 0;
152 for ( k = 0, sIt = noutIt.Begin(); !sIt.IsAtEnd(); ++sIt, ++k )
154 Sum += sIt.Get() * Weights[k];
156 noutIt.SetCenterPixel( Sum + inIt.Get() );
162 template<
typename ImType >
166 typename ImType::SizeType unitradius;
168 NIterator N1( unitradius, im, im->GetRequestedRegion() );
169 unsigned int centerIndex = N1.GetCenterNeighborhoodIndex();
170 typename NIterator::OffsetType offset;
171 std::vector< typename ImType::OffsetType > result;
172 for (
unsigned int d = 0; d < centerIndex * 2 + 1; d++ )
174 offset = N1.GetOffset(d);
177 for (
unsigned int k = 0; k < ImType::ImageDimension; k++ )
179 if ( offset[k] == 0 )
187 result.push_back(offset);
193 template<
typename TInputImage,
typename TOutputImage >
196 TOutputImage *outputImage,
197 typename TInputImage::RegionType inputRegion,
198 typename TOutputImage::RegionType outputRegion,
199 typename TInputImage::SizeType Radius,
203 typedef TInputImage InputImageType;
204 typedef typename TInputImage::RegionType RegionType;
205 typedef typename TInputImage::SizeType SizeType;
206 typedef typename TInputImage::IndexType IndexType;
207 typedef typename TInputImage::OffsetType OffsetType;
208 typedef TOutputImage OutputImageType;
209 typedef typename TOutputImage::PixelType OutputPixelType;
212 typedef typename FaceCalculatorType::FaceListType FaceListType;
213 typedef typename FaceCalculatorType::FaceListType::iterator FaceListTypeIt;
214 FaceCalculatorType faceCalculator;
216 FaceListType faceList;
223 SizeType internalRadius;
224 SizeType RegionLimit;
226 IndexType RegionStart = inputRegion.GetIndex();
227 for (
unsigned int i = 0; i < TInputImage::ImageDimension; i++ )
229 kernelSize[i] = Radius[i] * 2 + 1;
230 internalRadius[i] = Radius[i] + 1;
231 RegionLimit[i] = inputRegion.GetSize()[i] + RegionStart[i] - 1;
236 std::vector< OffsetType > UnitCorners = CornerOffsets< TInputImage >(accImage);
237 std::vector< OffsetType > RealCorners;
238 std::vector< AccPixType > Weights;
240 for (
unsigned int k = 0; k < UnitCorners.size(); k++ )
243 OffsetType ThisCorner;
244 for (
unsigned int i = 0; i < TInputImage::ImageDimension; i++ )
246 prod *= UnitCorners[k][i];
247 if ( UnitCorners[k][i] > 0 )
249 ThisCorner[i] = Radius[i];
253 ThisCorner[i] = -( Radius[i] + 1 );
256 Weights.push_back( (AccPixType)prod );
257 RealCorners.push_back(ThisCorner);
260 faceList = faceCalculator(accImage, outputRegion, internalRadius);
262 for ( fit = faceList.begin(); fit != faceList.end(); ++fit )
264 if ( fit == faceList.begin() )
269 AccPixType pixelscount = 1;
270 for (
unsigned int i = 0; i < TInputImage::ImageDimension; i++ )
272 pixelscount *= (AccPixType)( 2 * Radius[i] + 1 );
278 typedef std::vector< InputIteratorType > CornerItVecType;
279 CornerItVecType CornerItVec;
281 for (
unsigned int k = 0; k < RealCorners.size(); k++ )
283 typename InputImageType::RegionType tReg = ( *fit );
284 tReg.
SetIndex(tReg.GetIndex() + RealCorners[k]);
285 InputIteratorType tempIt(accImage, tReg);
287 CornerItVec.push_back(tempIt);
290 OutputIteratorType oIt(outputImage, *fit);
292 for ( oIt.GoToBegin(); !oIt.IsAtEnd(); ++oIt )
296 for (
unsigned int k = 0; k < CornerItVec.size(); k++ )
298 Sum += Weights[k] * CornerItVec[k].Get();
300 ++( CornerItVec[k] );
302 oIt.Set( static_cast< OutputPixelType >( Sum / pixelscount ) );
310 OutputIteratorType oIt(outputImage, *fit);
312 for ( oIt.GoToBegin(); !oIt.IsAtEnd(); ++oIt )
317 RegionType currentKernelRegion;
318 currentKernelRegion.SetSize(kernelSize);
320 IndexType kernelRegionIdx = oIt.GetIndex();
321 IndexType CentIndex = kernelRegionIdx;
322 for (
unsigned int i = 0; i < TInputImage::ImageDimension; i++ )
324 kernelRegionIdx[i] -= Radius[i];
326 currentKernelRegion.SetIndex(kernelRegionIdx);
327 currentKernelRegion.Crop(inputRegion);
328 OffsetValueType edgepixelscount = currentKernelRegion.GetNumberOfPixels();
338 for (
unsigned int k = 0; k < RealCorners.size(); k++ )
340 IndexType ThisCorner = CentIndex + RealCorners[k];
341 bool IncludeCorner =
true;
342 for (
unsigned int j = 0; j < TInputImage::ImageDimension; j++ )
344 if ( UnitCorners[k][j] > 0 )
347 if ( ThisCorner[j] > static_cast< OffsetValueType >( RegionLimit[j] ) )
355 if ( ThisCorner[j] < RegionStart[j] )
357 IncludeCorner =
false;
364 Sum += accImage->GetPixel(ThisCorner) * Weights[k];
368 oIt.Set( static_cast< OutputPixelType >( Sum / (AccPixType)edgepixelscount ) );
375 template<
typename TInputImage,
typename TOutputImage >
378 TOutputImage *outputImage,
379 typename TInputImage::RegionType inputRegion,
380 typename TOutputImage::RegionType outputRegion,
381 typename TInputImage::SizeType Radius,
385 typedef TInputImage InputImageType;
386 typedef typename TInputImage::RegionType RegionType;
387 typedef typename TInputImage::SizeType SizeType;
388 typedef typename TInputImage::IndexType IndexType;
389 typedef typename TInputImage::OffsetType OffsetType;
390 typedef TOutputImage OutputImageType;
391 typedef typename TOutputImage::PixelType OutputPixelType;
392 typedef typename TInputImage::PixelType InputPixelType;
395 typedef typename FaceCalculatorType::FaceListType FaceListType;
396 typedef typename FaceCalculatorType::FaceListType::iterator FaceListTypeIt;
397 FaceCalculatorType faceCalculator;
399 FaceListType faceList;
406 SizeType internalRadius;
407 SizeType RegionLimit;
408 IndexType RegionStart = inputRegion.GetIndex();
409 for (
unsigned int i = 0; i < TInputImage::ImageDimension; i++ )
411 kernelSize[i] = Radius[i] * 2 + 1;
412 internalRadius[i] = Radius[i] + 1;
413 RegionLimit[i] = inputRegion.GetSize()[i] + RegionStart[i] - 1;
418 std::vector< OffsetType > UnitCorners = CornerOffsets< TInputImage >(accImage);
419 std::vector< OffsetType > RealCorners;
420 std::vector< AccPixType > Weights;
422 for (
unsigned int k = 0; k < UnitCorners.size(); k++ )
425 OffsetType ThisCorner;
426 for (
unsigned int i = 0; i < TInputImage::ImageDimension; i++ )
428 prod *= UnitCorners[k][i];
429 if ( UnitCorners[k][i] > 0 )
431 ThisCorner[i] = Radius[i];
435 ThisCorner[i] = -( Radius[i] + 1 );
438 Weights.push_back( (AccPixType)prod );
439 RealCorners.push_back(ThisCorner);
442 faceList = faceCalculator(accImage, outputRegion, internalRadius);
444 for ( fit = faceList.begin(); fit != faceList.end(); ++fit )
446 if ( fit == faceList.begin() )
451 AccPixType pixelscount = 1;
452 for (
unsigned int i = 0; i < TInputImage::ImageDimension; i++ )
454 pixelscount *= (AccPixType)( 2 * Radius[i] + 1 );
460 typedef std::vector< InputIteratorType > CornerItVecType;
461 CornerItVecType CornerItVec;
463 for (
unsigned int k = 0; k < RealCorners.size(); k++ )
465 typename InputImageType::RegionType tReg = ( *fit );
466 tReg.
SetIndex(tReg.GetIndex() + RealCorners[k]);
467 InputIteratorType tempIt(accImage, tReg);
469 CornerItVec.push_back(tempIt);
472 OutputIteratorType oIt(outputImage, *fit);
474 for ( oIt.GoToBegin(); !oIt.IsAtEnd(); ++oIt )
477 AccPixType SquareSum = 0;
479 for (
unsigned int k = 0; k < CornerItVec.size(); k++ )
481 const InputPixelType & i = CornerItVec[k].Get();
482 Sum += Weights[k] * i[0];
483 SquareSum += Weights[k] * i[1];
485 ++( CornerItVec[k] );
488 oIt.Set( static_cast< OutputPixelType >( std::sqrt( ( SquareSum - Sum * Sum / pixelscount ) / ( pixelscount - 1 ) ) ) );
496 OutputIteratorType oIt(outputImage, *fit);
498 for ( oIt.GoToBegin(); !oIt.IsAtEnd(); ++oIt )
503 RegionType currentKernelRegion;
504 currentKernelRegion.SetSize(kernelSize);
506 IndexType kernelRegionIdx = oIt.GetIndex();
507 IndexType CentIndex = kernelRegionIdx;
508 for (
unsigned int i = 0; i < TInputImage::ImageDimension; i++ )
510 kernelRegionIdx[i] -= Radius[i];
512 currentKernelRegion.SetIndex(kernelRegionIdx);
513 currentKernelRegion.Crop(inputRegion);
514 SizeValueType edgepixelscount = currentKernelRegion.GetNumberOfPixels();
516 AccPixType SquareSum = 0;
525 for (
unsigned int k = 0; k < RealCorners.size(); k++ )
527 IndexType ThisCorner = CentIndex + RealCorners[k];
528 bool IncludeCorner =
true;
529 for (
unsigned int j = 0; j < TInputImage::ImageDimension; j++ )
531 if ( UnitCorners[k][j] > 0 )
534 if ( ThisCorner[j] > static_cast< OffsetValueType >( RegionLimit[j] ) )
542 if ( ThisCorner[j] < RegionStart[j] )
544 IncludeCorner =
false;
551 const InputPixelType & i = accImage->GetPixel(ThisCorner);
552 Sum += Weights[k] * i[0];
553 SquareSum += Weights[k] * i[1];
557 oIt.Set( static_cast< OutputPixelType >( std::sqrt( ( SquareSum - Sum * Sum
558 / edgepixelscount ) / ( edgepixelscount - 1 ) ) ) );
565 template<
typename TInputImage,
typename TOutputImage >
568 TOutputImage *outputImage,
569 typename TInputImage::RegionType inputRegion,
570 typename TOutputImage::RegionType outputRegion,
574 typedef TInputImage InputImageType;
575 typedef typename TInputImage::OffsetType OffsetType;
576 typedef TOutputImage OutputImageType;
577 typedef typename TOutputImage::PixelType OutputPixelType;
578 typedef typename OutputPixelType::ValueType ValueType;
579 typedef typename TInputImage::PixelType InputPixelType;
584 InputIterator inIt(inputImage, inputRegion);
585 typename TInputImage::SizeType kernelRadius;
586 kernelRadius.Fill(1);
588 NOutputIterator noutIt(kernelRadius, outputImage, outputRegion);
594 noutIt.OverrideBoundaryCondition(&oBC);
604 std::vector< int > Weights;
605 typename NOutputIterator::ConstIterator sIt;
606 for (
typename NOutputIterator::IndexListType::const_iterator idxIt = noutIt.GetActiveIndexList().begin();
607 idxIt != noutIt.GetActiveIndexList().end();
610 OffsetType offset = noutIt.GetOffset(*idxIt);
612 for (
unsigned int k = 0; k < InputImageType::ImageDimension; k++ )
614 if ( offset[k] != 0 )
619 Weights.push_back(w);
622 for ( inIt.GoToBegin(), noutIt.GoToBegin(); !noutIt.IsAtEnd(); ++inIt, ++noutIt )
625 ValueType SquareSum = 0;
627 for ( k = 0, sIt = noutIt.Begin(); !sIt.IsAtEnd(); ++sIt, ++k )
629 const OutputPixelType & v = sIt.Get();
630 Sum += v[0] * Weights[k];
631 SquareSum += v[1] * Weights[k];
634 const InputPixelType & i = inIt.Get();
636 o[1] = SquareSum + i * i;
637 noutIt.SetCenterPixel(o);
void BoxSquareAccumulateFunction(const TInputImage *inputImage, TOutputImage *outputImage, typename TInputImage::RegionType inputRegion, typename TOutputImage::RegionType outputRegion, ProgressReporter &progress)
void SetConstant(const OutputPixelType &c)
void BoxSigmaCalculatorFunction(const TInputImage *accImage, TOutputImage *outputImage, typename TInputImage::RegionType inputRegion, typename TOutputImage::RegionType outputRegion, typename TInputImage::SizeType Radius, ProgressReporter &progress)
std::vector< typename ImType::OffsetType > CornerOffsets(const ImType *im)
void SetIndex(const IndexType &ind) override
signed long OffsetValueType
void BoxMeanCalculatorFunction(const TInputImage *accImage, TOutputImage *outputImage, typename TInputImage::RegionType inputRegion, typename TOutputImage::RegionType outputRegion, typename TInputImage::SizeType Radius, ProgressReporter &progress)
This boundary condition returns a constant value for out-of-bounds image pixels.
unsigned long SizeValueType
A neighborhood iterator which can take on an arbitrary shape.
Splits an image into a main region and several "face" regions which are used to handle computations o...
A multi-dimensional iterator templated over image type that walks a region of pixels.
A multi-dimensional iterator templated over image type that walks pixels within a region and is speci...
Implements progress tracking for a filter.
TIterator * setConnectivityEarlyBox(TIterator *it, bool fullyConnected=false)
Define additional traits for native types such as int or float.
void BoxAccumulateFunction(const TInputImage *inputImage, const TOutputImage *outputImage, typename TInputImage::RegionType inputRegion, typename TOutputImage::RegionType outputRegion, ProgressReporter &progress)
A multi-dimensional iterator templated over image type that walks a region of pixels.