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,
103 #
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(); idxIt != noutIt.GetActiveIndexList().end(); idxIt++)
143 OffsetType offset = noutIt.GetOffset(*idxIt);
145 for (
unsigned int k = 0; k < InputImageType::ImageDimension; k++)
153 weights.push_back(w);
156 for (inIt.GoToBegin(), noutIt.GoToBegin(); !noutIt.IsAtEnd(); ++inIt, ++noutIt)
158 OutputPixelType sum = 0;
160 for (k = 0, sIt = noutIt.Begin(); !sIt.IsAtEnd(); ++sIt, ++k)
162 sum += sIt.Get() * weights[k];
164 noutIt.SetCenterPixel(sum + inIt.Get());
165 #if defined(ITKV4_COMPATIBILITY)
172 template <
typename TImage>
173 std::vector<typename TImage::OffsetType>
179 NIterator n1(unitradius, im, im->GetRequestedRegion());
180 unsigned int centerIndex = n1.GetCenterNeighborhoodIndex();
181 typename NIterator::OffsetType offset;
182 std::vector<typename TImage::OffsetType> result;
183 for (
unsigned int d = 0; d < centerIndex * 2 + 1; d++)
185 offset = n1.GetOffset(d);
188 for (
unsigned int k = 0; k < TImage::ImageDimension; k++)
198 result.push_back(offset);
204 template <
typename TInputImage,
typename TOutputImage>
207 TOutputImage * outputImage,
211 #
if defined(ITKV4_COMPATIBILITY)
219 using InputImageType = TInputImage;
223 using OffsetType =
typename TInputImage::OffsetType;
224 using OutputImageType = TOutputImage;
225 using OutputPixelType =
typename TOutputImage::PixelType;
228 using FaceListType =
typename FaceCalculatorType::FaceListType;
229 using FaceListTypeIt =
typename FaceCalculatorType::FaceListType::iterator;
230 FaceCalculatorType faceCalculator;
232 FaceListType faceList;
243 for (
unsigned int i = 0; i < TInputImage::ImageDimension; i++)
245 kernelSize[i] = radius[i] * 2 + 1;
246 internalRadius[i] = radius[i] + 1;
247 regionLimit[i] = inputRegion.GetSize()[i] + regionStart[i] - 1;
252 std::vector<OffsetType> unitCorners = CornerOffsets<TInputImage>(accImage);
253 std::vector<OffsetType> realCorners;
254 std::vector<AccPixType> weights;
256 for (
unsigned int k = 0; k < unitCorners.size(); k++)
259 OffsetType thisCorner;
260 for (
unsigned int i = 0; i < TInputImage::ImageDimension; i++)
262 prod *= unitCorners[k][i];
263 if (unitCorners[k][i] > 0)
265 thisCorner[i] = radius[i];
269 thisCorner[i] = -(static_cast<OffsetValueType>(radius[i]) + 1);
272 weights.push_back((AccPixType)prod);
273 realCorners.push_back(thisCorner);
276 faceList = faceCalculator(accImage, outputRegion, internalRadius);
278 for (fit = faceList.begin(); fit != faceList.end(); ++fit)
280 if (fit == faceList.begin())
285 AccPixType pixelscount = 1;
286 for (
unsigned int i = 0; i < TInputImage::ImageDimension; i++)
288 pixelscount *= (AccPixType)(2 * radius[i] + 1);
294 using CornerItVecType = std::vector<InputIteratorType>;
295 CornerItVecType cornerItVec;
297 for (
unsigned int k = 0; k < realCorners.size(); k++)
300 tReg.
SetIndex(tReg.GetIndex() + realCorners[k]);
301 InputIteratorType tempIt(accImage, tReg);
303 cornerItVec.push_back(tempIt);
306 OutputIteratorType oIt(outputImage, *fit);
308 for (oIt.GoToBegin(); !oIt.IsAtEnd(); ++oIt)
312 for (
unsigned int k = 0; k < cornerItVec.size(); k++)
314 sum += weights[k] * cornerItVec[k].Get();
318 oIt.Set(static_cast<OutputPixelType>(sum / pixelscount));
319 #if defined(ITKV4_COMPATIBILITY)
328 OutputIteratorType oIt(outputImage, *fit);
330 for (oIt.GoToBegin(); !oIt.IsAtEnd(); ++oIt)
336 currentKernelRegion.
SetSize(kernelSize);
340 for (
unsigned int i = 0; i < TInputImage::ImageDimension; i++)
342 kernelRegionIdx[i] -= radius[i];
344 currentKernelRegion.
SetIndex(kernelRegionIdx);
345 currentKernelRegion.Crop(inputRegion);
346 OffsetValueType edgepixelscount = currentKernelRegion.GetNumberOfPixels();
356 for (
unsigned int k = 0; k < realCorners.size(); k++)
358 IndexType thisCorner = centIndex + realCorners[k];
359 bool includeCorner =
true;
360 for (
unsigned int j = 0; j < TInputImage::ImageDimension; j++)
362 if (unitCorners[k][j] > 0)
365 if (thisCorner[j] > static_cast<OffsetValueType>(regionLimit[j]))
367 thisCorner[j] = static_cast<OffsetValueType>(regionLimit[j]);
373 if (thisCorner[j] < regionStart[j])
375 includeCorner =
false;
382 sum += accImage->GetPixel(thisCorner) * weights[k];
386 oIt.Set(static_cast<OutputPixelType>(sum / (AccPixType)edgepixelscount));
387 #if defined(ITKV4_COMPATIBILITY)
395 template <
typename TInputImage,
typename TOutputImage>
398 TOutputImage * outputImage,
402 #
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];
460 thisCorner[i] = -(static_cast<OffsetValueType>(radius[i]) + 1);
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];
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]))
563 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];
585 static_cast<OutputPixelType>(std::sqrt((squareSum - sum * sum / edgepixelscount) / (edgepixelscount - 1))));
586 #if defined(ITKV4_COMPATIBILITY)
594 template <
typename TInputImage,
typename TOutputImage>
597 TOutputImage * outputImage,
600 #
if defined(ITKV4_COMPATIBILITY)
608 using InputImageType = TInputImage;
609 using OffsetType =
typename TInputImage::OffsetType;
610 using OutputImageType = TOutputImage;
611 using OutputPixelType =
typename TOutputImage::PixelType;
612 using ValueType =
typename OutputPixelType::ValueType;
613 using InputPixelType =
typename TInputImage::PixelType;
618 InputIterator inIt(inputImage, inputRegion);
620 kernelRadius.
Fill(1);
622 NOutputIterator noutIt(kernelRadius, outputImage, outputRegion);
628 noutIt.OverrideBoundaryCondition(&oBC);
638 std::vector<int> weights;
639 typename NOutputIterator::ConstIterator sIt;
640 for (
auto idxIt = noutIt.GetActiveIndexList().begin(); idxIt != noutIt.GetActiveIndexList().end(); idxIt++)
642 OffsetType offset = noutIt.GetOffset(*idxIt);
644 for (
unsigned int k = 0; k < InputImageType::ImageDimension; k++)
651 weights.push_back(w);
654 for (inIt.GoToBegin(), noutIt.GoToBegin(); !noutIt.IsAtEnd(); ++inIt, ++noutIt)
657 ValueType squareSum = 0;
659 for (k = 0, sIt = noutIt.Begin(); !sIt.IsAtEnd(); ++sIt, ++k)
661 const OutputPixelType & v = sIt.Get();
662 sum += v[0] * weights[k];
663 squareSum += v[1] * weights[k];
666 const InputPixelType & i = inIt.Get();
668 o[1] = squareSum + i * i;
669 noutIt.SetCenterPixel(o);
670 #if defined(ITKV4_COMPATIBILITY)