18 #ifndef __itkBoxUtilities_h
19 #define __itkBoxUtilities_h
44 template<
class 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<
class TInputImage,
class TOutputImage >
95 const TOutputImage *outputImage,
96 typename TInputImage::RegionType inputRegion,
97 typename TOutputImage::RegionType outputRegion,
101 typedef TInputImage InputImageType;
102 typedef typename TInputImage::RegionType RegionType;
103 typedef typename TInputImage::SizeType SizeType;
104 typedef typename TInputImage::IndexType IndexType;
105 typedef typename TInputImage::PixelType PixelType;
106 typedef typename TInputImage::OffsetType OffsetType;
107 typedef TOutputImage OutputImageType;
108 typedef typename TOutputImage::PixelType OutputPixelType;
109 typedef typename TInputImage::PixelType InputPixelType;
115 InputIterator inIt(inputImage, inputRegion);
116 typename TInputImage::SizeType kernelRadius;
117 kernelRadius.Fill(1);
119 NOutputIterator noutIt(kernelRadius, outputImage, outputRegion);
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<
class ImType >
172 typename ImType::SizeType unitradius;
174 NIterator N1( unitradius, im, im->GetRequestedRegion() );
175 unsigned int centerIndex = N1.GetCenterNeighborhoodIndex();
176 typename NIterator::OffsetType offset;
177 std::vector< typename ImType::OffsetType > result;
178 for (
unsigned int d = 0; d < centerIndex * 2 + 1; d++ )
180 offset = N1.GetOffset(d);
183 for (
unsigned int k = 0; k < ImType::ImageDimension; k++ )
185 if ( offset[k] == 0 )
193 result.push_back(offset);
199 template<
class TInputImage,
class TOutputImage >
202 TOutputImage *outputImage,
203 typename TInputImage::RegionType inputRegion,
204 typename TOutputImage::RegionType outputRegion,
205 typename TInputImage::SizeType Radius,
209 typedef TInputImage InputImageType;
210 typedef typename TInputImage::RegionType RegionType;
211 typedef typename TInputImage::SizeType SizeType;
212 typedef typename TInputImage::IndexType IndexType;
213 typedef typename TInputImage::PixelType PixelType;
214 typedef typename TInputImage::OffsetType OffsetType;
215 typedef TOutputImage OutputImageType;
216 typedef typename TOutputImage::PixelType OutputPixelType;
217 typedef typename TInputImage::PixelType InputPixelType;
220 typedef typename FaceCalculatorType::FaceListType FaceListType;
221 typedef typename FaceCalculatorType::FaceListType::iterator FaceListTypeIt;
222 FaceCalculatorType faceCalculator;
224 FaceListType faceList;
231 SizeType internalRadius;
232 SizeType RegionLimit;
234 IndexType RegionStart = inputRegion.GetIndex();
235 for (
unsigned int i = 0; i < TInputImage::ImageDimension; i++ )
237 kernelSize[i] = Radius[i] * 2 + 1;
238 internalRadius[i] = Radius[i] + 1;
239 RegionLimit[i] = inputRegion.GetSize()[i] + RegionStart[i] - 1;
244 std::vector< OffsetType > UnitCorners = CornerOffsets< TInputImage >(accImage);
245 std::vector< OffsetType > RealCorners;
246 std::vector< AccPixType > Weights;
248 for (
unsigned int k = 0; k < UnitCorners.size(); k++ )
251 OffsetType ThisCorner;
252 for (
unsigned int i = 0; i < TInputImage::ImageDimension; i++ )
254 prod *= UnitCorners[k][i];
255 if ( UnitCorners[k][i] > 0 )
257 ThisCorner[i] = Radius[i];
261 ThisCorner[i] = -( Radius[i] + 1 );
264 Weights.push_back( (AccPixType)prod );
265 RealCorners.push_back(ThisCorner);
268 faceList = faceCalculator(accImage, outputRegion, internalRadius);
270 for ( fit = faceList.begin(); fit != faceList.end(); ++fit )
272 if ( fit == faceList.begin() )
277 AccPixType pixelscount = 1;
278 for (
unsigned int i = 0; i < TInputImage::ImageDimension; i++ )
280 pixelscount *= (AccPixType)( 2 * Radius[i] + 1 );
286 typedef std::vector< InputIteratorType > CornerItVecType;
287 CornerItVecType CornerItVec;
289 for (
unsigned int k = 0; k < RealCorners.size(); k++ )
291 typename InputImageType::RegionType tReg = ( *fit );
292 tReg.
SetIndex(tReg.GetIndex() + RealCorners[k]);
293 InputIteratorType tempIt(accImage, tReg);
295 CornerItVec.push_back(tempIt);
298 OutputIteratorType oIt(outputImage, *fit);
300 for ( oIt.GoToBegin(); !oIt.IsAtEnd(); ++oIt )
304 for (
unsigned int k = 0; k < CornerItVec.size(); k++ )
306 Sum += Weights[k] * CornerItVec[k].Get();
308 ++( CornerItVec[k] );
310 oIt.Set( static_cast< OutputPixelType >( Sum / pixelscount ) );
318 OutputIteratorType oIt(outputImage, *fit);
320 for ( oIt.GoToBegin(); !oIt.IsAtEnd(); ++oIt )
325 RegionType currentKernelRegion;
326 currentKernelRegion.SetSize(kernelSize);
328 IndexType kernelRegionIdx = oIt.GetIndex();
329 IndexType CentIndex = kernelRegionIdx;
330 for (
unsigned int i = 0; i < TInputImage::ImageDimension; i++ )
332 kernelRegionIdx[i] -= Radius[i];
334 currentKernelRegion.SetIndex(kernelRegionIdx);
335 currentKernelRegion.Crop(inputRegion);
336 OffsetValueType edgepixelscount = currentKernelRegion.GetNumberOfPixels();
346 for (
unsigned int k = 0; k < RealCorners.size(); k++ )
348 IndexType ThisCorner = CentIndex + RealCorners[k];
349 bool IncludeCorner =
true;
350 for (
unsigned int j = 0; j < TInputImage::ImageDimension; j++ )
352 if ( UnitCorners[k][j] > 0 )
355 if ( ThisCorner[j] > static_cast< OffsetValueType >( RegionLimit[j] ) )
363 if ( ThisCorner[j] < RegionStart[j] )
365 IncludeCorner =
false;
372 Sum += accImage->GetPixel(ThisCorner) * Weights[k];
376 oIt.Set( static_cast< OutputPixelType >( Sum / (AccPixType)edgepixelscount ) );
383 template<
class TInputImage,
class TOutputImage >
386 TOutputImage *outputImage,
387 typename TInputImage::RegionType inputRegion,
388 typename TOutputImage::RegionType outputRegion,
389 typename TInputImage::SizeType Radius,
393 typedef TInputImage InputImageType;
394 typedef typename TInputImage::RegionType RegionType;
395 typedef typename TInputImage::SizeType SizeType;
396 typedef typename TInputImage::IndexType IndexType;
397 typedef typename TInputImage::PixelType PixelType;
398 typedef typename TInputImage::OffsetType OffsetType;
399 typedef TOutputImage OutputImageType;
400 typedef typename TOutputImage::PixelType OutputPixelType;
401 typedef typename TInputImage::PixelType InputPixelType;
402 typedef typename InputPixelType::ValueType ValueType;
405 typedef typename FaceCalculatorType::FaceListType FaceListType;
406 typedef typename FaceCalculatorType::FaceListType::iterator FaceListTypeIt;
407 FaceCalculatorType faceCalculator;
409 FaceListType faceList;
416 SizeType internalRadius;
417 SizeType RegionLimit;
418 IndexType RegionStart = inputRegion.GetIndex();
419 for (
unsigned int i = 0; i < TInputImage::ImageDimension; i++ )
421 kernelSize[i] = Radius[i] * 2 + 1;
422 internalRadius[i] = Radius[i] + 1;
423 RegionLimit[i] = inputRegion.GetSize()[i] + RegionStart[i] - 1;
428 std::vector< OffsetType > UnitCorners = CornerOffsets< TInputImage >(accImage);
429 std::vector< OffsetType > RealCorners;
430 std::vector< AccPixType > Weights;
432 for (
unsigned int k = 0; k < UnitCorners.size(); k++ )
435 OffsetType ThisCorner;
436 for (
unsigned int i = 0; i < TInputImage::ImageDimension; i++ )
438 prod *= UnitCorners[k][i];
439 if ( UnitCorners[k][i] > 0 )
441 ThisCorner[i] = Radius[i];
445 ThisCorner[i] = -( Radius[i] + 1 );
448 Weights.push_back( (AccPixType)prod );
449 RealCorners.push_back(ThisCorner);
452 faceList = faceCalculator(accImage, outputRegion, internalRadius);
454 for ( fit = faceList.begin(); fit != faceList.end(); ++fit )
456 if ( fit == faceList.begin() )
461 AccPixType pixelscount = 1;
462 for (
unsigned int i = 0; i < TInputImage::ImageDimension; i++ )
464 pixelscount *= (AccPixType)( 2 * Radius[i] + 1 );
470 typedef std::vector< InputIteratorType > CornerItVecType;
471 CornerItVecType CornerItVec;
473 for (
unsigned int k = 0; k < RealCorners.size(); k++ )
475 typename InputImageType::RegionType tReg = ( *fit );
476 tReg.
SetIndex(tReg.GetIndex() + RealCorners[k]);
477 InputIteratorType tempIt(accImage, tReg);
479 CornerItVec.push_back(tempIt);
482 OutputIteratorType oIt(outputImage, *fit);
484 for ( oIt.GoToBegin(); !oIt.IsAtEnd(); ++oIt )
487 AccPixType SquareSum = 0;
489 for (
unsigned int k = 0; k < CornerItVec.size(); k++ )
491 const InputPixelType & i = CornerItVec[k].Get();
492 Sum += Weights[k] * i[0];
493 SquareSum += Weights[k] * i[1];
495 ++( CornerItVec[k] );
498 oIt.Set( static_cast< OutputPixelType >( vcl_sqrt( ( SquareSum - Sum * Sum / pixelscount ) / ( pixelscount - 1 ) ) ) );
506 OutputIteratorType oIt(outputImage, *fit);
508 for ( oIt.GoToBegin(); !oIt.IsAtEnd(); ++oIt )
513 RegionType currentKernelRegion;
514 currentKernelRegion.SetSize(kernelSize);
516 IndexType kernelRegionIdx = oIt.GetIndex();
517 IndexType CentIndex = kernelRegionIdx;
518 for (
unsigned int i = 0; i < TInputImage::ImageDimension; i++ )
520 kernelRegionIdx[i] -= Radius[i];
522 currentKernelRegion.SetIndex(kernelRegionIdx);
523 currentKernelRegion.Crop(inputRegion);
524 SizeValueType edgepixelscount = currentKernelRegion.GetNumberOfPixels();
526 AccPixType SquareSum = 0;
535 for (
unsigned int k = 0; k < RealCorners.size(); k++ )
537 IndexType ThisCorner = CentIndex + RealCorners[k];
538 bool IncludeCorner =
true;
539 for (
unsigned int j = 0; j < TInputImage::ImageDimension; j++ )
541 if ( UnitCorners[k][j] > 0 )
544 if ( ThisCorner[j] > static_cast< OffsetValueType >( RegionLimit[j] ) )
552 if ( ThisCorner[j] < RegionStart[j] )
554 IncludeCorner =
false;
561 const InputPixelType & i = accImage->GetPixel(ThisCorner);
562 Sum += Weights[k] * i[0];
563 SquareSum += Weights[k] * i[1];
567 oIt.Set( static_cast< OutputPixelType >( vcl_sqrt( ( SquareSum - Sum * Sum
568 / edgepixelscount ) / ( edgepixelscount - 1 ) ) ) );
575 template<
class TInputImage,
class TOutputImage >
578 TOutputImage *outputImage,
579 typename TInputImage::RegionType inputRegion,
580 typename TOutputImage::RegionType outputRegion,
584 typedef TInputImage InputImageType;
585 typedef typename TInputImage::RegionType RegionType;
586 typedef typename TInputImage::SizeType SizeType;
587 typedef typename TInputImage::IndexType IndexType;
588 typedef typename TInputImage::PixelType PixelType;
589 typedef typename TInputImage::OffsetType OffsetType;
590 typedef TOutputImage OutputImageType;
591 typedef typename TOutputImage::PixelType OutputPixelType;
592 typedef typename OutputPixelType::ValueType ValueType;
593 typedef typename TInputImage::PixelType InputPixelType;
599 InputIterator inIt(inputImage, inputRegion);
600 typename TInputImage::SizeType kernelRadius;
601 kernelRadius.Fill(1);
603 NOutputIterator noutIt(kernelRadius, outputImage, outputRegion);
609 noutIt.OverrideBoundaryCondition(&oBC);
619 std::vector< int > Weights;
620 typename NOutputIterator::ConstIterator sIt;
621 for (
typename NOutputIterator::IndexListType::const_iterator idxIt = noutIt.GetActiveIndexList().begin();
622 idxIt != noutIt.GetActiveIndexList().end();
625 OffsetType offset = noutIt.GetOffset(*idxIt);
627 for (
unsigned int k = 0; k < InputImageType::ImageDimension; k++ )
629 if ( offset[k] != 0 )
634 Weights.push_back(w);
637 for ( inIt.GoToBegin(), noutIt.GoToBegin(); !noutIt.IsAtEnd(); ++inIt, ++noutIt )
640 ValueType SquareSum = 0;
642 for ( k = 0, sIt = noutIt.Begin(); !sIt.IsAtEnd(); ++sIt, ++k )
644 const OutputPixelType & v = sIt.Get();
645 Sum += v[0] * Weights[k];
646 SquareSum += v[1] * Weights[k];
649 const InputPixelType & i = inIt.Get();
651 o[1] = SquareSum + i * i;
652 noutIt.SetCenterPixel(o);