18 #ifndef itkBoxUtilities_h
19 #define itkBoxUtilities_h
42 namespace itk_impl_details
45 template<
typename TIterator >
50 typename TIterator::OffsetType offset;
51 it->ClearActiveList();
52 if ( !fullyConnected )
60 it->ActivateOffset(offset);
68 unsigned int centerIndex = it->GetCenterNeighborhoodIndex();
69 for (
unsigned int d = 0; d < centerIndex; d++ )
71 offset = it->GetOffset(d);
84 it->ActivateOffset(offset);
88 it->DeactivateOffset(offset);
98 template<
typename TInputImage,
typename TOutputImage >
101 const TOutputImage *outputImage,
104 #
if defined(ITKV4_COMPATIBILITY)
111 using InputImageType = TInputImage;
112 using OffsetType =
typename TInputImage::OffsetType;
113 using OutputImageType = TOutputImage;
114 using OutputPixelType =
typename TOutputImage::PixelType;
119 InputIterator inIt(inputImage, inputRegion);
121 kernelRadius.
Fill(1);
123 NOutputIterator noutIt(kernelRadius, outputImage, outputRegion);
129 noutIt.OverrideBoundaryCondition(&oBC);
139 std::vector< int > weights;
140 typename NOutputIterator::ConstIterator sIt;
141 for (
auto idxIt = noutIt.GetActiveIndexList().begin();
142 idxIt != noutIt.GetActiveIndexList().end();
145 OffsetType offset = noutIt.GetOffset(*idxIt);
147 for (
unsigned int k = 0; k < InputImageType::ImageDimension; k++ )
149 if ( offset[k] != 0 )
155 weights.push_back(w);
158 for ( inIt.GoToBegin(), noutIt.GoToBegin(); !noutIt.IsAtEnd(); ++inIt, ++noutIt )
160 OutputPixelType sum = 0;
162 for ( k = 0, sIt = noutIt.Begin(); !sIt.IsAtEnd(); ++sIt, ++k )
164 sum += sIt.Get() * weights[k];
166 noutIt.SetCenterPixel( sum + inIt.Get() );
167 #if defined(ITKV4_COMPATIBILITY)
174 template<
typename TImage >
175 std::vector< typename TImage::OffsetType >
181 NIterator n1( unitradius, im, im->GetRequestedRegion() );
182 unsigned int centerIndex = n1.GetCenterNeighborhoodIndex();
183 typename NIterator::OffsetType offset;
184 std::vector< typename TImage::OffsetType > result;
185 for (
unsigned int d = 0; d < centerIndex * 2 + 1; d++ )
187 offset = n1.GetOffset(d);
190 for (
unsigned int k = 0; k < TImage::ImageDimension; k++ )
192 if ( offset[k] == 0 )
200 result.push_back(offset);
206 template<
typename TInputImage,
typename TOutputImage >
209 TOutputImage *outputImage,
213 #
if defined(ITKV4_COMPATIBILITY)
220 using InputImageType = TInputImage;
224 using OffsetType =
typename TInputImage::OffsetType;
225 using OutputImageType = TOutputImage;
226 using OutputPixelType =
typename TOutputImage::PixelType;
229 using FaceListType =
typename FaceCalculatorType::FaceListType;
230 using FaceListTypeIt =
typename FaceCalculatorType::FaceListType::iterator;
231 FaceCalculatorType faceCalculator;
233 FaceListType faceList;
244 for (
unsigned int i = 0; i < TInputImage::ImageDimension; i++ )
246 kernelSize[i] = radius[i] * 2 + 1;
247 internalRadius[i] = radius[i] + 1;
248 regionLimit[i] = inputRegion.GetSize()[i] + regionStart[i] - 1;
253 std::vector< OffsetType > unitCorners = CornerOffsets< TInputImage >(accImage);
254 std::vector< OffsetType > realCorners;
255 std::vector< AccPixType > weights;
257 for (
unsigned int k = 0; k < unitCorners.size(); k++ )
260 OffsetType thisCorner;
261 for (
unsigned int i = 0; i < TInputImage::ImageDimension; i++ )
263 prod *= unitCorners[k][i];
264 if ( unitCorners[k][i] > 0 )
266 thisCorner[i] = radius[i];
273 weights.push_back( (AccPixType)prod );
274 realCorners.push_back(thisCorner);
277 faceList = faceCalculator(accImage, outputRegion, internalRadius);
279 for ( fit = faceList.begin(); fit != faceList.end(); ++fit )
281 if ( fit == faceList.begin() )
286 AccPixType pixelscount = 1;
287 for (
unsigned int i = 0; i < TInputImage::ImageDimension; i++ )
289 pixelscount *= (AccPixType)( 2 * radius[i] + 1 );
295 using CornerItVecType = std::vector< InputIteratorType >;
296 CornerItVecType cornerItVec;
298 for (
unsigned int k = 0; k < realCorners.size(); k++ )
301 tReg.
SetIndex(tReg.GetIndex() + realCorners[k]);
302 InputIteratorType tempIt(accImage, tReg);
304 cornerItVec.push_back(tempIt);
307 OutputIteratorType oIt(outputImage, *fit);
309 for ( oIt.GoToBegin(); !oIt.IsAtEnd(); ++oIt )
313 for (
unsigned int k = 0; k < cornerItVec.size(); k++ )
315 sum += weights[k] * cornerItVec[k].Get();
317 ++( cornerItVec[k] );
319 oIt.Set( static_cast< OutputPixelType >( sum / pixelscount ) );
320 #if defined(ITKV4_COMPATIBILITY)
329 OutputIteratorType oIt(outputImage, *fit);
331 for ( oIt.GoToBegin(); !oIt.IsAtEnd(); ++oIt )
337 currentKernelRegion.
SetSize(kernelSize);
341 for (
unsigned int i = 0; i < TInputImage::ImageDimension; i++ )
343 kernelRegionIdx[i] -= radius[i];
345 currentKernelRegion.
SetIndex(kernelRegionIdx);
346 currentKernelRegion.Crop(inputRegion);
347 OffsetValueType edgepixelscount = currentKernelRegion.GetNumberOfPixels();
357 for (
unsigned int k = 0; k < realCorners.size(); k++ )
359 IndexType thisCorner = centIndex + realCorners[k];
360 bool includeCorner =
true;
361 for (
unsigned int j = 0; j < TInputImage::ImageDimension; j++ )
363 if ( unitCorners[k][j] > 0 )
366 if ( thisCorner[j] > static_cast< OffsetValueType >( regionLimit[j] ) )
374 if ( thisCorner[j] < regionStart[j] )
376 includeCorner =
false;
383 sum += accImage->GetPixel(thisCorner) * weights[k];
387 oIt.Set( static_cast< OutputPixelType >( sum / (AccPixType)edgepixelscount ) );
388 #if defined(ITKV4_COMPATIBILITY)
396 template<
typename TInputImage,
typename TOutputImage >
399 TOutputImage *outputImage,
403 #
if defined(ITKV4_COMPATIBILITY)
410 using InputImageType = TInputImage;
414 using OffsetType =
typename TInputImage::OffsetType;
415 using OutputImageType = TOutputImage;
416 using OutputPixelType =
typename TOutputImage::PixelType;
417 using InputPixelType =
typename TInputImage::PixelType;
420 using FaceListType =
typename FaceCalculatorType::FaceListType;
421 using FaceListTypeIt =
typename FaceCalculatorType::FaceListType::iterator;
422 FaceCalculatorType faceCalculator;
424 FaceListType faceList;
434 for (
unsigned int i = 0; i < TInputImage::ImageDimension; i++ )
436 kernelSize[i] = radius[i] * 2 + 1;
437 internalRadius[i] = radius[i] + 1;
438 regionLimit[i] = inputRegion.GetSize()[i] + regionStart[i] - 1;
443 std::vector< OffsetType > unitCorners = CornerOffsets< TInputImage >(accImage);
444 std::vector< OffsetType > realCorners;
445 std::vector< AccPixType > weights;
447 for (
unsigned int k = 0; k < unitCorners.size(); k++ )
450 OffsetType thisCorner;
451 for (
unsigned int i = 0; i < TInputImage::ImageDimension; i++ )
453 prod *= unitCorners[k][i];
454 if ( unitCorners[k][i] > 0 )
456 thisCorner[i] = radius[i];
463 weights.push_back( (AccPixType)prod );
464 realCorners.push_back(thisCorner);
467 faceList = faceCalculator(accImage, outputRegion, internalRadius);
469 for ( fit = faceList.begin(); fit != faceList.end(); ++fit )
471 if ( fit == faceList.begin() )
476 AccPixType pixelscount = 1;
477 for (
unsigned int i = 0; i < TInputImage::ImageDimension; i++ )
479 pixelscount *= (AccPixType)( 2 * radius[i] + 1 );
485 using CornerItVecType = std::vector< InputIteratorType >;
486 CornerItVecType cornerItVec;
488 for (
unsigned int k = 0; k < realCorners.size(); k++ )
491 tReg.
SetIndex(tReg.GetIndex() + realCorners[k]);
492 InputIteratorType tempIt(accImage, tReg);
494 cornerItVec.push_back(tempIt);
497 OutputIteratorType oIt(outputImage, *fit);
499 for ( oIt.GoToBegin(); !oIt.IsAtEnd(); ++oIt )
502 AccPixType squareSum = 0;
504 for (
unsigned int k = 0; k < cornerItVec.size(); k++ )
506 const InputPixelType & i = cornerItVec[k].Get();
507 sum += weights[k] * i[0];
508 squareSum += weights[k] * i[1];
510 ++( cornerItVec[k] );
513 oIt.Set( static_cast< OutputPixelType >( std::sqrt( ( squareSum - sum * sum / pixelscount ) / ( pixelscount - 1 ) ) ) );
514 #if defined(ITKV4_COMPATIBILITY)
523 OutputIteratorType oIt(outputImage, *fit);
525 for ( oIt.GoToBegin(); !oIt.IsAtEnd(); ++oIt )
531 currentKernelRegion.
SetSize(kernelSize);
535 for (
unsigned int i = 0; i < TInputImage::ImageDimension; i++ )
537 kernelRegionIdx[i] -= radius[i];
539 currentKernelRegion.
SetIndex(kernelRegionIdx);
540 currentKernelRegion.Crop(inputRegion);
541 SizeValueType edgepixelscount = currentKernelRegion.GetNumberOfPixels();
543 AccPixType squareSum = 0;
552 for (
unsigned int k = 0; k < realCorners.size(); k++ )
554 IndexType thisCorner = centIndex + realCorners[k];
555 bool includeCorner =
true;
556 for (
unsigned int j = 0; j < TInputImage::ImageDimension; j++ )
558 if ( unitCorners[k][j] > 0 )
561 if ( thisCorner[j] > static_cast< OffsetValueType >( regionLimit[j] ) )
569 if ( thisCorner[j] < regionStart[j] )
571 includeCorner =
false;
578 const InputPixelType & i = accImage->GetPixel(thisCorner);
579 sum += weights[k] * i[0];
580 squareSum += weights[k] * i[1];
584 oIt.Set( static_cast< OutputPixelType >( std::sqrt( ( squareSum - sum * sum
585 / edgepixelscount ) / ( edgepixelscount - 1 ) ) ) );
586 #if defined(ITKV4_COMPATIBILITY)
594 template<
typename TInputImage,
typename TOutputImage >
597 TOutputImage *outputImage,
600 #
if defined(ITKV4_COMPATIBILITY)
607 using InputImageType = TInputImage;
608 using OffsetType =
typename TInputImage::OffsetType;
609 using OutputImageType = TOutputImage;
610 using OutputPixelType =
typename TOutputImage::PixelType;
611 using ValueType =
typename OutputPixelType::ValueType;
612 using InputPixelType =
typename TInputImage::PixelType;
617 InputIterator inIt(inputImage, inputRegion);
619 kernelRadius.
Fill(1);
621 NOutputIterator noutIt(kernelRadius, outputImage, outputRegion);
627 noutIt.OverrideBoundaryCondition(&oBC);
637 std::vector< int > weights;
638 typename NOutputIterator::ConstIterator sIt;
639 for (
auto idxIt = noutIt.GetActiveIndexList().begin();
640 idxIt != noutIt.GetActiveIndexList().end();
643 OffsetType offset = noutIt.GetOffset(*idxIt);
645 for (
unsigned int k = 0; k < InputImageType::ImageDimension; k++ )
647 if ( offset[k] != 0 )
652 weights.push_back(w);
655 for ( inIt.GoToBegin(), noutIt.GoToBegin(); !noutIt.IsAtEnd(); ++inIt, ++noutIt )
658 ValueType squareSum = 0;
660 for ( k = 0, sIt = noutIt.Begin(); !sIt.IsAtEnd(); ++sIt, ++k )
662 const OutputPixelType & v = sIt.Get();
663 sum += v[0] * weights[k];
664 squareSum += v[1] * weights[k];
667 const InputPixelType & i = inIt.Get();
669 o[1] = squareSum + i * i;
670 noutIt.SetCenterPixel(o);
671 #if defined(ITKV4_COMPATIBILITY)
void SetSize(const SizeType &size)
void SetConstant(const OutputPixelType &c)
void BoxMeanCalculatorFunction(const TInputImage *accImage, TOutputImage *outputImage, typename TInputImage::RegionType inputRegion, typename TOutputImage::RegionType outputRegion, typename TInputImage::SizeType radius)
TIterator * setConnectivityEarlyBox(TIterator *it, bool fullyConnected=false)
Define numeric traits for std::vector.
unsigned long SizeValueType
This boundary condition returns a constant value for out-of-bounds image pixels.
constexpr unsigned int Dimension
A neighborhood iterator which can take on an arbitrary shape.
void BoxAccumulateFunction(const TInputImage *inputImage, const TOutputImage *outputImage, typename TInputImage::RegionType inputRegion, typename TOutputImage::RegionType outputRegion)
std::vector< typename TImage::OffsetType > CornerOffsets(const TImage *im)
Splits an image into a main region and several "face" regions which are used to handle computations o...
ImageBaseType::SizeType SizeType
ImageBaseType::IndexType IndexType
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.
void SetIndex(const IndexValueType val[VDimension])
void BoxSigmaCalculatorFunction(const TInputImage *accImage, TOutputImage *outputImage, typename TInputImage::RegionType inputRegion, typename TOutputImage::RegionType outputRegion, typename TInputImage::SizeType radius)
const IndexValueType * GetIndex() const
ImageBaseType::RegionType RegionType
void Fill(SizeValueType value)
void SetIndex(const IndexType &index)
signed long OffsetValueType
A multi-dimensional iterator templated over image type that walks a region of pixels.
void BoxSquareAccumulateFunction(const TInputImage *inputImage, TOutputImage *outputImage, typename TInputImage::RegionType inputRegion, typename TOutputImage::RegionType outputRegion)