18 #ifndef itkBoxUtilities_h
19 #define itkBoxUtilities_h
45 template<
typename TIterator >
47 setConnectivityEarlyBox(TIterator *it,
bool fullyConnected =
false)
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,
102 typename TInputImage::RegionType inputRegion,
103 typename TOutputImage::RegionType outputRegion,
107 typedef TInputImage InputImageType;
108 typedef typename TInputImage::OffsetType OffsetType;
109 typedef TOutputImage OutputImageType;
110 typedef typename TOutputImage::PixelType OutputPixelType;
115 InputIterator inIt(inputImage, inputRegion);
117 kernelRadius.
Fill(1);
119 NOutputIterator noutIt(kernelRadius, outputImage, outputRegion);
121 setConnectivityEarlyBox(&noutIt,
true);
125 noutIt.OverrideBoundaryCondition(&oBC);
135 std::vector< int > weights;
136 typename NOutputIterator::ConstIterator sIt;
137 for (
typename NOutputIterator::IndexListType::const_iterator idxIt = noutIt.GetActiveIndexList().begin();
138 idxIt != noutIt.GetActiveIndexList().end();
141 OffsetType offset = noutIt.GetOffset(*idxIt);
143 for (
unsigned int k = 0; k < InputImageType::ImageDimension; k++ )
145 if ( offset[k] != 0 )
151 weights.push_back(w);
154 for ( inIt.GoToBegin(), noutIt.GoToBegin(); !noutIt.IsAtEnd(); ++inIt, ++noutIt )
156 OutputPixelType sum = 0;
158 for ( k = 0, sIt = noutIt.Begin(); !sIt.IsAtEnd(); ++sIt, ++k )
160 sum += sIt.Get() * weights[k];
162 noutIt.SetCenterPixel( sum + inIt.Get() );
168 template<
typename TImage >
169 std::vector< typename TImage::OffsetType >
175 NIterator n1( unitradius, im, im->GetRequestedRegion() );
176 unsigned int centerIndex = n1.GetCenterNeighborhoodIndex();
177 typename NIterator::OffsetType offset;
178 std::vector< typename TImage::OffsetType > result;
179 for (
unsigned int d = 0; d < centerIndex * 2 + 1; d++ )
181 offset = n1.GetOffset(d);
184 for (
unsigned int k = 0; k < TImage::ImageDimension; k++ )
186 if ( offset[k] == 0 )
194 result.push_back(offset);
200 template<
typename TInputImage,
typename TOutputImage >
203 TOutputImage *outputImage,
204 typename TInputImage::RegionType inputRegion,
205 typename TOutputImage::RegionType outputRegion,
210 typedef TInputImage InputImageType;
211 typedef typename TInputImage::RegionType RegionType;
214 typedef typename TInputImage::OffsetType OffsetType;
215 typedef TOutputImage OutputImageType;
216 typedef typename TOutputImage::PixelType OutputPixelType;
219 typedef typename FaceCalculatorType::FaceListType FaceListType;
220 typedef typename FaceCalculatorType::FaceListType::iterator FaceListTypeIt;
221 FaceCalculatorType faceCalculator;
223 FaceListType faceList;
230 SizeType internalRadius;
231 SizeType regionLimit;
233 IndexType regionStart = inputRegion.GetIndex();
234 for (
unsigned int i = 0; i < TInputImage::ImageDimension; i++ )
236 kernelSize[i] = radius[i] * 2 + 1;
237 internalRadius[i] = radius[i] + 1;
238 regionLimit[i] = inputRegion.GetSize()[i] + regionStart[i] - 1;
243 std::vector< OffsetType > unitCorners = CornerOffsets< TInputImage >(accImage);
244 std::vector< OffsetType > realCorners;
245 std::vector< AccPixType > weights;
247 for (
unsigned int k = 0; k < unitCorners.size(); k++ )
250 OffsetType thisCorner;
251 for (
unsigned int i = 0; i < TInputImage::ImageDimension; i++ )
253 prod *= unitCorners[k][i];
254 if ( unitCorners[k][i] > 0 )
256 thisCorner[i] = radius[i];
263 weights.push_back( (AccPixType)prod );
264 realCorners.push_back(thisCorner);
267 faceList = faceCalculator(accImage, outputRegion, internalRadius);
269 for ( fit = faceList.begin(); fit != faceList.end(); ++fit )
271 if ( fit == faceList.begin() )
276 AccPixType pixelscount = 1;
277 for (
unsigned int i = 0; i < TInputImage::ImageDimension; i++ )
279 pixelscount *= (AccPixType)( 2 * radius[i] + 1 );
285 typedef std::vector< InputIteratorType > CornerItVecType;
286 CornerItVecType cornerItVec;
288 for (
unsigned int k = 0; k < realCorners.size(); k++ )
290 typename InputImageType::RegionType tReg = ( *fit );
291 tReg.
SetIndex(tReg.GetIndex() + realCorners[k]);
292 InputIteratorType tempIt(accImage, tReg);
294 cornerItVec.push_back(tempIt);
297 OutputIteratorType oIt(outputImage, *fit);
299 for ( oIt.GoToBegin(); !oIt.IsAtEnd(); ++oIt )
303 for (
unsigned int k = 0; k < cornerItVec.size(); k++ )
305 sum += weights[k] * cornerItVec[k].Get();
307 ++( cornerItVec[k] );
309 oIt.Set( static_cast< OutputPixelType >( sum / pixelscount ) );
317 OutputIteratorType oIt(outputImage, *fit);
319 for ( oIt.GoToBegin(); !oIt.IsAtEnd(); ++oIt )
324 RegionType currentKernelRegion;
325 currentKernelRegion.SetSize(kernelSize);
327 IndexType kernelRegionIdx = oIt.GetIndex();
328 IndexType centIndex = kernelRegionIdx;
329 for (
unsigned int i = 0; i < TInputImage::ImageDimension; i++ )
331 kernelRegionIdx[i] -= radius[i];
333 currentKernelRegion.SetIndex(kernelRegionIdx);
334 currentKernelRegion.Crop(inputRegion);
335 OffsetValueType edgepixelscount = currentKernelRegion.GetNumberOfPixels();
345 for (
unsigned int k = 0; k < realCorners.size(); k++ )
347 IndexType thisCorner = centIndex + realCorners[k];
348 bool includeCorner =
true;
349 for (
unsigned int j = 0; j < TInputImage::ImageDimension; j++ )
351 if ( unitCorners[k][j] > 0 )
354 if ( thisCorner[j] > static_cast< OffsetValueType >( regionLimit[j] ) )
362 if ( thisCorner[j] < regionStart[j] )
364 includeCorner =
false;
371 sum += accImage->GetPixel(thisCorner) * weights[k];
375 oIt.Set( static_cast< OutputPixelType >( sum / (AccPixType)edgepixelscount ) );
382 template<
typename TInputImage,
typename TOutputImage >
385 TOutputImage *outputImage,
386 typename TInputImage::RegionType inputRegion,
387 typename TOutputImage::RegionType outputRegion,
392 typedef TInputImage InputImageType;
393 typedef typename TInputImage::RegionType RegionType;
396 typedef typename TInputImage::OffsetType OffsetType;
397 typedef TOutputImage OutputImageType;
398 typedef typename TOutputImage::PixelType OutputPixelType;
399 typedef typename TInputImage::PixelType InputPixelType;
402 typedef typename FaceCalculatorType::FaceListType FaceListType;
403 typedef typename FaceCalculatorType::FaceListType::iterator FaceListTypeIt;
404 FaceCalculatorType faceCalculator;
406 FaceListType faceList;
413 SizeType internalRadius;
414 SizeType regionLimit;
415 IndexType regionStart = inputRegion.GetIndex();
416 for (
unsigned int i = 0; i < TInputImage::ImageDimension; i++ )
418 kernelSize[i] = radius[i] * 2 + 1;
419 internalRadius[i] = radius[i] + 1;
420 regionLimit[i] = inputRegion.GetSize()[i] + regionStart[i] - 1;
425 std::vector< OffsetType > unitCorners = CornerOffsets< TInputImage >(accImage);
426 std::vector< OffsetType > realCorners;
427 std::vector< AccPixType > weights;
429 for (
unsigned int k = 0; k < unitCorners.size(); k++ )
432 OffsetType thisCorner;
433 for (
unsigned int i = 0; i < TInputImage::ImageDimension; i++ )
435 prod *= unitCorners[k][i];
436 if ( unitCorners[k][i] > 0 )
438 thisCorner[i] = radius[i];
445 weights.push_back( (AccPixType)prod );
446 realCorners.push_back(thisCorner);
449 faceList = faceCalculator(accImage, outputRegion, internalRadius);
451 for ( fit = faceList.begin(); fit != faceList.end(); ++fit )
453 if ( fit == faceList.begin() )
458 AccPixType pixelscount = 1;
459 for (
unsigned int i = 0; i < TInputImage::ImageDimension; i++ )
461 pixelscount *= (AccPixType)( 2 * radius[i] + 1 );
467 typedef std::vector< InputIteratorType > CornerItVecType;
468 CornerItVecType cornerItVec;
470 for (
unsigned int k = 0; k < realCorners.size(); k++ )
472 typename InputImageType::RegionType tReg = ( *fit );
473 tReg.
SetIndex(tReg.GetIndex() + realCorners[k]);
474 InputIteratorType tempIt(accImage, tReg);
476 cornerItVec.push_back(tempIt);
479 OutputIteratorType oIt(outputImage, *fit);
481 for ( oIt.GoToBegin(); !oIt.IsAtEnd(); ++oIt )
484 AccPixType squareSum = 0;
486 for (
unsigned int k = 0; k < cornerItVec.size(); k++ )
488 const InputPixelType & i = cornerItVec[k].Get();
489 sum += weights[k] * i[0];
490 squareSum += weights[k] * i[1];
492 ++( cornerItVec[k] );
495 oIt.Set( static_cast< OutputPixelType >( std::sqrt( ( squareSum - sum * sum / pixelscount ) / ( pixelscount - 1 ) ) ) );
503 OutputIteratorType oIt(outputImage, *fit);
505 for ( oIt.GoToBegin(); !oIt.IsAtEnd(); ++oIt )
510 RegionType currentKernelRegion;
511 currentKernelRegion.SetSize(kernelSize);
513 IndexType kernelRegionIdx = oIt.GetIndex();
514 IndexType centIndex = kernelRegionIdx;
515 for (
unsigned int i = 0; i < TInputImage::ImageDimension; i++ )
517 kernelRegionIdx[i] -= radius[i];
519 currentKernelRegion.SetIndex(kernelRegionIdx);
520 currentKernelRegion.Crop(inputRegion);
521 SizeValueType edgepixelscount = currentKernelRegion.GetNumberOfPixels();
523 AccPixType squareSum = 0;
532 for (
unsigned int k = 0; k < realCorners.size(); k++ )
534 IndexType thisCorner = centIndex + realCorners[k];
535 bool includeCorner =
true;
536 for (
unsigned int j = 0; j < TInputImage::ImageDimension; j++ )
538 if ( unitCorners[k][j] > 0 )
541 if ( thisCorner[j] > static_cast< OffsetValueType >( regionLimit[j] ) )
549 if ( thisCorner[j] < regionStart[j] )
551 includeCorner =
false;
558 const InputPixelType & i = accImage->GetPixel(thisCorner);
559 sum += weights[k] * i[0];
560 squareSum += weights[k] * i[1];
564 oIt.Set( static_cast< OutputPixelType >( std::sqrt( ( squareSum - sum * sum
565 / edgepixelscount ) / ( edgepixelscount - 1 ) ) ) );
572 template<
typename TInputImage,
typename TOutputImage >
575 TOutputImage *outputImage,
576 typename TInputImage::RegionType inputRegion,
577 typename TOutputImage::RegionType outputRegion,
581 typedef TInputImage InputImageType;
582 typedef typename TInputImage::OffsetType OffsetType;
583 typedef TOutputImage OutputImageType;
584 typedef typename TOutputImage::PixelType OutputPixelType;
585 typedef typename OutputPixelType::ValueType ValueType;
586 typedef typename TInputImage::PixelType InputPixelType;
591 InputIterator inIt(inputImage, inputRegion);
593 kernelRadius.
Fill(1);
595 NOutputIterator noutIt(kernelRadius, outputImage, outputRegion);
597 setConnectivityEarlyBox(&noutIt,
true);
601 noutIt.OverrideBoundaryCondition(&oBC);
611 std::vector< int > weights;
612 typename NOutputIterator::ConstIterator sIt;
613 for (
typename NOutputIterator::IndexListType::const_iterator idxIt = noutIt.GetActiveIndexList().begin();
614 idxIt != noutIt.GetActiveIndexList().end();
617 OffsetType offset = noutIt.GetOffset(*idxIt);
619 for (
unsigned int k = 0; k < InputImageType::ImageDimension; k++ )
621 if ( offset[k] != 0 )
626 weights.push_back(w);
629 for ( inIt.GoToBegin(), noutIt.GoToBegin(); !noutIt.IsAtEnd(); ++inIt, ++noutIt )
632 ValueType squareSum = 0;
634 for ( k = 0, sIt = noutIt.Begin(); !sIt.IsAtEnd(); ++sIt, ++k )
636 const OutputPixelType & v = sIt.Get();
637 sum += v[0] * weights[k];
638 squareSum += v[1] * weights[k];
641 const InputPixelType & i = inIt.Get();
643 o[1] = squareSum + i * i;
644 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 SetIndex(const IndexType &ind) override
signed long OffsetValueType
void BoxSigmaCalculatorFunction(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.
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...
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...
const unsigned int Dimension
Implements progress tracking for a filter.
ImageBaseType::SizeType SizeType
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)
void Fill(SizeValueType value)
A multi-dimensional iterator templated over image type that walks a region of pixels.
ImageBaseType::IndexType IndexType
void BoxMeanCalculatorFunction(const TInputImage *accImage, TOutputImage *outputImage, typename TInputImage::RegionType inputRegion, typename TOutputImage::RegionType outputRegion, typename TInputImage::SizeType radius, ProgressReporter &progress)