| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkNeighborhoodAlgorithm.txx.html,v $ |
| 5 |
|
Language: C++ |
| 6 |
|
Date: $Date: 2006/01/17 19:15:42 $ |
| 7 |
|
Version: $Revision: 1.4 $ |
| 8 |
|
|
| 9 |
|
Copyright (c) Insight Software Consortium. All rights reserved. |
| 10 |
|
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details. |
| 11 |
|
|
| 12 |
|
This software is distributed WITHOUT ANY WARRANTY; without even |
| 13 |
|
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 |
|
PURPOSE. See the above copyright notices for more information. |
| 15 |
|
|
| 16 |
|
=========================================================================*/ |
| 17 |
DEF |
#ifndef _itkNeighborhoodAlgorithm_txx |
| 18 |
DEF |
#define _itkNeighborhoodAlgorithm_txx |
| 19 |
|
#include "itkNeighborhoodAlgorithm.h" |
| 20 |
|
#include "itkImageRegionIterator.h" |
| 21 |
|
#include "itkImageRegion.h" |
| 22 |
|
#include "itkConstSliceIterator.h" |
| 23 |
|
|
| 24 |
|
namespace itk |
| 25 |
|
{ |
| 26 |
|
|
| 27 |
|
namespace NeighborhoodAlgorithm { |
| 28 |
|
|
| 29 |
|
template<class TImage> |
| 30 |
|
typename ImageBoundaryFacesCalculator<TImage>::FaceListType |
| 31 |
|
ImageBoundaryFacesCalculator<TImage> |
| 32 |
|
::operator()(const TImage *img, RegionType regionToProcess, RadiusType radius) |
| 33 |
|
{ |
| 34 |
|
unsigned int j, i; |
| 35 |
|
// Analyze the regionToProcess to determine if any of its faces are |
| 36 |
|
// along a buffer boundary (we have no data in the buffer for pixels |
| 37 |
|
// that are outside the boundary, but within the neighborhood radius and will |
| 38 |
|
// have to treat them differently). We also determine the size of the non- |
| 39 |
|
// boundary region that will be processed. |
| 40 |
|
|
| 41 |
|
const IndexType bStart = img->GetBufferedRegion().GetIndex(); |
| 42 |
|
const SizeType bSize = img->GetBufferedRegion().GetSize(); |
| 43 |
|
const IndexType rStart = regionToProcess.GetIndex(); |
| 44 |
|
const SizeType rSize = regionToProcess.GetSize(); |
| 45 |
|
|
| 46 |
|
long overlapLow, overlapHigh; |
| 47 |
|
FaceListType faceList; |
| 48 |
|
IndexType fStart; // Boundary, "face" |
| 49 |
|
SizeType fSize; // region data. |
| 50 |
|
RegionType fRegion; |
| 51 |
|
SizeType nbSize = regionToProcess.GetSize(); // Non-boundary region |
| 52 |
|
IndexType nbStart = regionToProcess.GetIndex(); // data. |
| 53 |
|
RegionType nbRegion; |
| 54 |
|
|
| 55 |
|
for (i = 0; i < ImageDimension; ++i) |
| 56 |
|
{ |
| 57 |
|
overlapLow = static_cast<long>((rStart[i] - radius[i]) - bStart[i]); |
| 58 |
LEN |
overlapHigh= static_cast<long>((bStart[i] + bSize[i]) - (rStart[i] + rSize[i] + radius[i])); |
| 59 |
|
|
| 60 |
|
if (overlapLow < 0) // out of bounds condition, define a region of |
| 61 |
|
{ // iteration along this face |
| 62 |
|
for (j = 0; j < ImageDimension; ++j) // define the starting index |
| 63 |
|
{ // and size of the face region |
| 64 |
|
fStart[j] = rStart[j]; |
| 65 |
|
if ( j == i ) |
| 66 |
|
{ |
| 67 |
|
fSize[j] = -overlapLow; |
| 68 |
|
} |
| 69 |
|
else // NOTE: this algorithm |
| 70 |
|
{ // results in duplicate |
| 71 |
|
fSize[j] = rSize[j]; // pixels at corners between |
| 72 |
|
} // adjacent faces. |
| 73 |
|
|
| 74 |
|
// Boundary region cannot be outside the region to process |
| 75 |
|
if (fSize[j] > rSize[j]) |
| 76 |
|
{ |
| 77 |
|
fSize[j] = rSize[j]; |
| 78 |
|
} |
| 79 |
|
} |
| 80 |
|
|
| 81 |
LEN |
// avoid unsigned overflow if the non-boundary region is too small to process |
| 82 |
|
if (fSize[i] > nbSize[i]) |
| 83 |
|
{ |
| 84 |
|
nbSize[i] = 0; |
| 85 |
|
} |
| 86 |
|
else |
| 87 |
|
{ |
| 88 |
|
nbSize[i] -= fSize[i]; |
| 89 |
|
} |
| 90 |
|
nbStart[i] += -overlapLow; |
| 91 |
|
fRegion.SetIndex(fStart); |
| 92 |
|
fRegion.SetSize(fSize); |
| 93 |
|
faceList.push_back(fRegion); |
| 94 |
|
} |
| 95 |
|
if (overlapHigh < 0) |
| 96 |
|
{ |
| 97 |
|
for (j = 0; j < ImageDimension; ++j) |
| 98 |
|
{ |
| 99 |
|
if ( j == i ) |
| 100 |
|
{ |
| 101 |
LEN |
fStart[j] = rStart[j] + static_cast<IndexValueType>(rSize[j]) + overlapHigh; |
| 102 |
|
fSize[j] = -overlapHigh; |
| 103 |
|
|
| 104 |
|
// Start of the boundary condition region cannot be to the |
| 105 |
|
// left of the region to process |
| 106 |
|
if (fStart[j] < rStart[j]) |
| 107 |
|
{ |
| 108 |
|
fStart[j] = rStart[j]; |
| 109 |
|
fSize[j] = rSize[j]; // is this the right size? |
| 110 |
|
} |
| 111 |
|
} |
| 112 |
|
else |
| 113 |
|
{ |
| 114 |
|
fStart[j] = rStart[j]; |
| 115 |
|
fSize[j] = rSize[j]; |
| 116 |
|
} |
| 117 |
|
} |
| 118 |
LEN |
// avoid unsigned overflow if the non-boundary region is too small to process |
| 119 |
|
if (fSize[i] > nbSize[i]) |
| 120 |
|
{ |
| 121 |
|
nbSize[i] = 0; |
| 122 |
|
} |
| 123 |
|
else |
| 124 |
|
{ |
| 125 |
|
nbSize[i] -= fSize[i]; |
| 126 |
|
} |
| 127 |
|
fRegion.SetIndex(fStart); |
| 128 |
|
fRegion.SetSize(fSize); |
| 129 |
|
faceList.push_back(fRegion); |
| 130 |
|
} |
| 131 |
|
} |
| 132 |
|
nbRegion.SetSize(nbSize); |
| 133 |
|
nbRegion.SetIndex(nbStart); |
| 134 |
|
|
| 135 |
|
faceList.push_front(nbRegion); |
| 136 |
|
return faceList; |
| 137 |
|
} |
| 138 |
|
|
| 139 |
|
template<class TImage> |
| 140 |
|
typename CalculateOutputWrapOffsetModifiers<TImage>::OffsetType |
| 141 |
|
CalculateOutputWrapOffsetModifiers<TImage> |
| 142 |
|
::operator()(TImage *input, TImage *output) const |
| 143 |
|
{ |
| 144 |
|
OffsetType ans; |
| 145 |
LEN |
const Size<TImage::ImageDimension> isz = input->GetBufferedRegion().GetSize(); |
| 146 |
LEN |
const Size<TImage::ImageDimension> osz = output->GetBufferedRegion().GetSize(); |
| 147 |
|
|
| 148 |
|
for (int i=0; i<TImage::ImageDimension; ++i) |
| 149 |
|
{ |
| 150 |
|
ans[i] = osz[i] - isz[i]; |
| 151 |
|
} |
| 152 |
|
return ans; |
| 153 |
|
} |
| 154 |
|
|
| 155 |
|
|
| 156 |
EML |
|
| 157 |
EML |
|
| 158 |
EML |
|
| 159 |
|
} // end namespace NeighborhoodAlgorithm |
| 160 |
|
} // end namespace itk |
| 161 |
|
|
| 162 |
|
#endif |
| 163 |
|
|