18 #ifndef itkBoxUtilities_h
19 #define itkBoxUtilities_h
44 template <
typename TIterator>
49 typename TIterator::OffsetType offset;
50 it->ClearActiveList();
59 it->ActivateOffset(offset);
67 unsigned int centerIndex = it->GetCenterNeighborhoodIndex();
68 for (
unsigned int d = 0; d < centerIndex; ++d)
70 offset = it->GetOffset(d);
83 it->ActivateOffset(offset);
87 it->DeactivateOffset(offset);
97 template <
typename TInputImage,
typename TOutputImage>
100 const TOutputImage * outputImage,
105 using InputImageType = TInputImage;
106 using OffsetType =
typename TInputImage::OffsetType;
107 using OutputImageType = TOutputImage;
108 using OutputPixelType =
typename TOutputImage::PixelType;
113 InputIterator inIt(inputImage, inputRegion);
114 auto kernelRadius = TInputImage::SizeType::Filled(1);
116 NOutputIterator noutIt(kernelRadius, outputImage, outputRegion);
122 noutIt.OverrideBoundaryCondition(&oBC);
132 std::vector<int> weights;
133 typename NOutputIterator::ConstIterator sIt;
134 for (
auto idxIt = noutIt.GetActiveIndexList().begin(); idxIt != noutIt.GetActiveIndexList().end(); ++idxIt)
136 OffsetType offset = noutIt.GetOffset(*idxIt);
138 for (
unsigned int k = 0; k < InputImageType::ImageDimension; ++k)
146 weights.push_back(w);
149 for (inIt.GoToBegin(), noutIt.GoToBegin(); !noutIt.IsAtEnd(); ++inIt, ++noutIt)
151 OutputPixelType sum = 0;
153 for (k = 0, sIt = noutIt.Begin(); !sIt.IsAtEnd(); ++sIt, ++k)
155 sum += sIt.Get() * weights[k];
157 noutIt.SetCenterPixel(sum + inIt.Get());
162 template <
typename TImage>
163 std::vector<typename TImage::OffsetType>
167 auto unitradius = TImage::SizeType::Filled(1);
168 NIterator n1(unitradius, im, im->GetRequestedRegion());
169 unsigned int centerIndex = n1.GetCenterNeighborhoodIndex();
170 typename NIterator::OffsetType offset;
171 std::vector<typename TImage::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 < TImage::ImageDimension; ++k)
187 result.push_back(offset);
193 template <
typename TInputImage,
typename TOutputImage>
196 TOutputImage * outputImage,
202 using InputImageType = TInputImage;
206 using OffsetType =
typename TInputImage::OffsetType;
207 using OutputImageType = TOutputImage;
208 using OutputPixelType =
typename TOutputImage::PixelType;
211 using FaceListType =
typename FaceCalculatorType::FaceListType;
212 FaceCalculatorType faceCalculator;
223 for (
unsigned int i = 0; i < TInputImage::ImageDimension; ++i)
225 kernelSize[i] = radius[i] * 2 + 1;
226 internalRadius[i] = radius[i] + 1;
227 regionLimit[i] = inputRegion.GetSize()[i] + regionStart[i] - 1;
232 std::vector<OffsetType> unitCorners = CornerOffsets<TInputImage>(accImage);
233 std::vector<OffsetType> realCorners;
234 std::vector<AccPixType> weights;
236 for (
unsigned int k = 0; k < unitCorners.size(); ++k)
239 OffsetType thisCorner;
240 for (
unsigned int i = 0; i < TInputImage::ImageDimension; ++i)
242 prod *= unitCorners[k][i];
243 if (unitCorners[k][i] > 0)
245 thisCorner[i] = radius[i];
249 thisCorner[i] = -(static_cast<OffsetValueType>(radius[i]) + 1);
252 weights.push_back((AccPixType)prod);
253 realCorners.push_back(thisCorner);
256 FaceListType faceList = faceCalculator(accImage, outputRegion, internalRadius);
258 for (
const auto & face : faceList)
260 if (&face == &faceList.front())
265 AccPixType pixelscount = 1;
266 for (
unsigned int i = 0; i < TInputImage::ImageDimension; ++i)
268 pixelscount *= (AccPixType)(2 * radius[i] + 1);
274 using CornerItVecType = std::vector<InputIteratorType>;
275 CornerItVecType cornerItVec;
277 for (
unsigned int k = 0; k < realCorners.size(); ++k)
280 tReg.
SetIndex(tReg.GetIndex() + realCorners[k]);
281 InputIteratorType tempIt(accImage, tReg);
283 cornerItVec.push_back(tempIt);
286 OutputIteratorType oIt(outputImage, face);
288 for (oIt.GoToBegin(); !oIt.IsAtEnd(); ++oIt)
292 for (
unsigned int k = 0; k < cornerItVec.size(); ++k)
294 sum += weights[k] * cornerItVec[k].Get();
298 oIt.Set(static_cast<OutputPixelType>(sum / pixelscount));
305 OutputIteratorType oIt(outputImage, face);
307 for (oIt.GoToBegin(); !oIt.IsAtEnd(); ++oIt)
313 currentKernelRegion.
SetSize(kernelSize);
317 for (
unsigned int i = 0; i < TInputImage::ImageDimension; ++i)
319 kernelRegionIdx[i] -= radius[i];
321 currentKernelRegion.
SetIndex(kernelRegionIdx);
322 currentKernelRegion.Crop(inputRegion);
323 OffsetValueType edgepixelscount = currentKernelRegion.GetNumberOfPixels();
333 for (
unsigned int k = 0; k < realCorners.size(); ++k)
335 IndexType thisCorner = centIndex + realCorners[k];
336 bool includeCorner =
true;
337 for (
unsigned int j = 0; j < TInputImage::ImageDimension; ++j)
339 if (unitCorners[k][j] > 0)
342 thisCorner[j] = std::min(thisCorner[j], static_cast<OffsetValueType>(regionLimit[j]));
347 if (thisCorner[j] < regionStart[j])
349 includeCorner =
false;
356 sum += accImage->GetPixel(thisCorner) * weights[k];
360 oIt.Set(static_cast<OutputPixelType>(sum / (AccPixType)edgepixelscount));
366 template <
typename TInputImage,
typename TOutputImage>
369 TOutputImage * outputImage,
375 using InputImageType = TInputImage;
379 using OffsetType =
typename TInputImage::OffsetType;
380 using OutputImageType = TOutputImage;
381 using OutputPixelType =
typename TOutputImage::PixelType;
382 using InputPixelType =
typename TInputImage::PixelType;
385 using FaceListType =
typename FaceCalculatorType::FaceListType;
386 FaceCalculatorType faceCalculator;
396 for (
unsigned int i = 0; i < TInputImage::ImageDimension; ++i)
398 kernelSize[i] = radius[i] * 2 + 1;
399 internalRadius[i] = radius[i] + 1;
400 regionLimit[i] = inputRegion.GetSize()[i] + regionStart[i] - 1;
405 std::vector<OffsetType> unitCorners = CornerOffsets<TInputImage>(accImage);
406 std::vector<OffsetType> realCorners;
407 std::vector<AccPixType> weights;
409 for (
unsigned int k = 0; k < unitCorners.size(); ++k)
412 OffsetType thisCorner;
413 for (
unsigned int i = 0; i < TInputImage::ImageDimension; ++i)
415 prod *= unitCorners[k][i];
416 if (unitCorners[k][i] > 0)
418 thisCorner[i] = radius[i];
422 thisCorner[i] = -(static_cast<OffsetValueType>(radius[i]) + 1);
425 weights.push_back((AccPixType)prod);
426 realCorners.push_back(thisCorner);
429 FaceListType faceList = faceCalculator(accImage, outputRegion, internalRadius);
431 for (
const auto & face : faceList)
433 if (&face == &faceList.front())
438 AccPixType pixelscount = 1;
439 for (
unsigned int i = 0; i < TInputImage::ImageDimension; ++i)
441 pixelscount *= (AccPixType)(2 * radius[i] + 1);
447 using CornerItVecType = std::vector<InputIteratorType>;
448 CornerItVecType cornerItVec;
450 for (
unsigned int k = 0; k < realCorners.size(); ++k)
453 tReg.
SetIndex(tReg.GetIndex() + realCorners[k]);
454 InputIteratorType tempIt(accImage, tReg);
456 cornerItVec.push_back(tempIt);
459 OutputIteratorType oIt(outputImage, face);
461 for (oIt.GoToBegin(); !oIt.IsAtEnd(); ++oIt)
464 AccPixType squareSum = 0;
466 for (
unsigned int k = 0; k < cornerItVec.size(); ++k)
468 const InputPixelType & i = cornerItVec[k].Get();
469 sum += weights[k] * i[0];
470 squareSum += weights[k] * i[1];
475 oIt.Set(static_cast<OutputPixelType>(std::sqrt((squareSum - sum * sum / pixelscount) / (pixelscount - 1))));
482 OutputIteratorType oIt(outputImage, face);
484 for (oIt.GoToBegin(); !oIt.IsAtEnd(); ++oIt)
490 currentKernelRegion.
SetSize(kernelSize);
494 for (
unsigned int i = 0; i < TInputImage::ImageDimension; ++i)
496 kernelRegionIdx[i] -= radius[i];
498 currentKernelRegion.
SetIndex(kernelRegionIdx);
499 currentKernelRegion.Crop(inputRegion);
500 SizeValueType edgepixelscount = currentKernelRegion.GetNumberOfPixels();
502 AccPixType squareSum = 0;
511 for (
unsigned int k = 0; k < realCorners.size(); ++k)
513 IndexType thisCorner = centIndex + realCorners[k];
514 bool includeCorner =
true;
515 for (
unsigned int j = 0; j < TInputImage::ImageDimension; ++j)
517 if (unitCorners[k][j] > 0)
520 thisCorner[j] = std::min(thisCorner[j], static_cast<OffsetValueType>(regionLimit[j]));
525 if (thisCorner[j] < regionStart[j])
527 includeCorner =
false;
534 const InputPixelType & i = accImage->GetPixel(thisCorner);
535 sum += weights[k] * i[0];
536 squareSum += weights[k] * i[1];
541 static_cast<OutputPixelType>(std::sqrt((squareSum - sum * sum / edgepixelscount) / (edgepixelscount - 1))));
547 template <
typename TInputImage,
typename TOutputImage>
550 TOutputImage * outputImage,
555 using InputImageType = TInputImage;
556 using OffsetType =
typename TInputImage::OffsetType;
557 using OutputImageType = TOutputImage;
558 using OutputPixelType =
typename TOutputImage::PixelType;
559 using ValueType =
typename OutputPixelType::ValueType;
560 using InputPixelType =
typename TInputImage::PixelType;
565 InputIterator inIt(inputImage, inputRegion);
566 auto kernelRadius = TInputImage::SizeType::Filled(1);
568 NOutputIterator noutIt(kernelRadius, outputImage, outputRegion);
574 noutIt.OverrideBoundaryCondition(&oBC);
584 std::vector<int> weights;
585 typename NOutputIterator::ConstIterator sIt;
586 for (
auto idxIt = noutIt.GetActiveIndexList().begin(); idxIt != noutIt.GetActiveIndexList().end(); ++idxIt)
588 OffsetType offset = noutIt.GetOffset(*idxIt);
590 for (
unsigned int k = 0; k < InputImageType::ImageDimension; ++k)
597 weights.push_back(w);
600 for (inIt.GoToBegin(), noutIt.GoToBegin(); !noutIt.IsAtEnd(); ++inIt, ++noutIt)
603 ValueType squareSum = 0;
605 for (k = 0, sIt = noutIt.Begin(); !sIt.IsAtEnd(); ++sIt, ++k)
607 const OutputPixelType & v = sIt.Get();
608 sum += v[0] * weights[k];
609 squareSum += v[1] * weights[k];
612 const InputPixelType & i = inIt.Get();
614 o[1] = squareSum + i * i;
615 noutIt.SetCenterPixel(o);