ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
itkBoxSigmaSqrtNMinusOneImageFilter.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright NumFOCUS
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18#ifndef itkBoxSigmaSqrtNMinusOneImageFilter_h
19#define itkBoxSigmaSqrtNMinusOneImageFilter_h
20
21#include "itkBoxUtilities.h"
22#include "itkBoxImageFilter.h"
23#include "itkVersion.h"
24
25namespace itk
26{
27
28// copied and tweaked from BoxSigmaCalculatorFunction
29template <class TInputImage, class TOutputImage>
30void
31BoxSigmaSqrtNMinusOneCalculatorFunction(const TInputImage * accImage,
32 TOutputImage * outputImage,
33 const typename TInputImage::RegionType & inputRegion,
34 const typename TOutputImage::RegionType & outputRegion,
35 const typename TInputImage::SizeType & Radius)
36{
37 // type alias
38 using InputImageType = TInputImage;
39 using RegionType = typename TInputImage::RegionType;
40 using SizeType = typename TInputImage::SizeType;
41 using IndexType = typename TInputImage::IndexType;
42 using OffsetType = typename TInputImage::OffsetType;
43 using OutputImageType = TOutputImage;
44 using OutputPixelType = typename TOutputImage::PixelType;
45 using InputPixelType = typename TInputImage::PixelType;
46 // use the face generator for speed
48 using FaceListType = typename FaceCalculatorType::FaceListType;
49 using FaceListTypeIt = typename FaceCalculatorType::FaceListType::iterator;
50 FaceCalculatorType faceCalculator;
51
52 FaceListType faceList;
53 FaceListTypeIt fit;
55
56 // this process is actually slightly asymmetric because we need to
57 // subtract rectangles that are next to our kernel, not overlapping it
58 SizeType kernelSize;
59 SizeType internalRadius;
60 SizeType RegionLimit;
61 IndexType RegionStart = inputRegion.GetIndex();
62 for (unsigned int i = 0; i < TInputImage::ImageDimension; ++i)
63 {
64 kernelSize[i] = Radius[i] * 2 + 1;
65 internalRadius[i] = Radius[i] + 1;
66 RegionLimit[i] = inputRegion.GetSize()[i] + RegionStart[i] - 1;
67 }
68
69 using AccPixType = typename NumericTraits<OutputPixelType>::RealType;
70 // get a set of offsets to corners for a unit hypercube in this image
71 std::vector<OffsetType> UnitCorners = CornerOffsets<TInputImage>(accImage);
72 std::vector<OffsetType> RealCorners;
73 std::vector<AccPixType> Weights;
74 // now compute the weights
75 for (unsigned int k = 0; k < UnitCorners.size(); ++k)
76 {
77 int prod = 1;
78 OffsetType ThisCorner;
79 for (unsigned int i = 0; i < TInputImage::ImageDimension; ++i)
80 {
81 prod *= UnitCorners[k][i];
82 if (UnitCorners[k][i] > 0)
83 {
84 ThisCorner[i] = Radius[i];
85 }
86 else
87 {
88 ThisCorner[i] = -((int)Radius[i] + 1);
89 }
90 }
91 Weights.push_back((AccPixType)prod);
92 RealCorners.push_back(ThisCorner);
93 }
94
95
96 faceList = faceCalculator(accImage, outputRegion, internalRadius);
97 // start with the body region
98 for (fit = faceList.begin(); fit != faceList.end(); ++fit)
99 {
100 if (fit == faceList.begin())
101 {
102 // this is the body region. This is meant to be an optimized
103 // version that doesn't use neigborhood regions
104 // compute the various offsets
105 AccPixType pixelscount = 1;
106 for (unsigned int i = 0; i < TInputImage::ImageDimension; ++i)
107 {
108 pixelscount *= (AccPixType)(2 * Radius[i] + 1);
109 }
110
111 using OutputIteratorType = ImageRegionIterator<OutputImageType>;
112 using InputIteratorType = ImageRegionConstIterator<InputImageType>;
113
114 using CornerItVecType = std::vector<InputIteratorType>;
115 CornerItVecType CornerItVec;
116 // set up the iterators for each corner
117 for (unsigned int k = 0; k < RealCorners.size(); ++k)
118 {
119 typename InputImageType::RegionType tReg = (*fit);
120 tReg.SetIndex(tReg.GetIndex() + RealCorners[k]);
121 InputIteratorType tempIt(accImage, tReg);
122 tempIt.GoToBegin();
123 CornerItVec.push_back(tempIt);
124 }
125 // set up the output iterator
126 OutputIteratorType oIt(outputImage, *fit);
127 // now do the work
128 for (oIt.GoToBegin(); !oIt.IsAtEnd(); ++oIt)
129 {
130 AccPixType Sum = 0;
131 AccPixType SquareSum = 0;
132 // check each corner
133 for (unsigned int k = 0; k < CornerItVec.size(); ++k)
134 {
135 const InputPixelType & i = CornerItVec[k].Get();
136 Sum += Weights[k] * i[0];
137 SquareSum += Weights[k] * i[1];
138 // increment each corner iterator
139 ++(CornerItVec[k]);
140 }
141
142 oIt.Set(static_cast<OutputPixelType>(std::sqrt(SquareSum - Sum * Sum / pixelscount)));
143 }
144 }
145 else
146 {
147 // now we need to deal with the border regions
148 using OutputIteratorType = ImageRegionIteratorWithIndex<OutputImageType>;
149 OutputIteratorType oIt(outputImage, *fit);
150 // now do the work
151 for (oIt.GoToBegin(); !oIt.IsAtEnd(); ++oIt)
152 {
153 // figure out the number of pixels in the box by creating an
154 // equivalent region and cropping - this could probably be
155 // included in the loop below.
156 RegionType currentKernelRegion;
157 currentKernelRegion.SetSize(kernelSize);
158 // compute the region's index
159 IndexType kernelRegionIdx = oIt.GetIndex();
160 IndexType CentIndex = kernelRegionIdx;
161 for (unsigned int i = 0; i < TInputImage::ImageDimension; ++i)
162 {
163 kernelRegionIdx[i] -= Radius[i];
164 }
165 currentKernelRegion.SetIndex(kernelRegionIdx);
166 currentKernelRegion.Crop(inputRegion);
167 long edgepixelscount = currentKernelRegion.GetNumberOfPixels();
168 AccPixType Sum = 0;
169 AccPixType SquareSum = 0;
170 // rules are : for each corner,
171 // for each dimension
172 // if dimension offset is positive -> this is
173 // a leading edge. Crop if outside the input
174 // region
175 // if dimension offset is negative -> this is
176 // a trailing edge. Ignore if it is outside
177 // image region
178 for (unsigned int k = 0; k < RealCorners.size(); ++k)
179 {
180 IndexType ThisCorner = CentIndex + RealCorners[k];
181 bool IncludeCorner = true;
182 for (unsigned int j = 0; j < TInputImage::ImageDimension; ++j)
183 {
184 if (UnitCorners[k][j] > 0)
185 {
186 // leading edge - crop it
187 if (ThisCorner[j] > static_cast<OffsetValueType>(RegionLimit[j]))
188 {
189 ThisCorner[j] = static_cast<OffsetValueType>(RegionLimit[j]);
190 }
191 }
192 else
193 {
194 // trailing edge - check bounds
195 if (ThisCorner[j] < RegionStart[j])
196 {
197 IncludeCorner = false;
198 break;
199 }
200 }
201 }
202 if (IncludeCorner)
203 {
204 const InputPixelType & i = accImage->GetPixel(ThisCorner);
205 Sum += Weights[k] * i[0];
206 SquareSum += Weights[k] * i[1];
207 }
208 }
209
210 oIt.Set(static_cast<OutputPixelType>(std::sqrt(SquareSum - Sum * Sum / edgepixelscount)));
211 }
212 }
213 }
214}
215
216
228template <class TInputImage, class TOutputImage = TInputImage>
229class ITK_TEMPLATE_EXPORT BoxSigmaSqrtNMinusOneImageFilter : public BoxImageFilter<TInputImage, TOutputImage>
230{
231public:
237
239 itkNewMacro(Self);
240
242 itkOverrideGetNameOfClassMacro(BoxSigmaSqrtNMinusOneImageFilter);
243
245 using InputImageType = TInputImage;
246 using OutputImageType = TOutputImage;
247 using RegionType = typename TInputImage::RegionType;
248 using SizeType = typename TInputImage::SizeType;
249 using IndexType = typename TInputImage::IndexType;
250 using PixelType = typename TInputImage::PixelType;
251 using OffsetType = typename TInputImage::OffsetType;
253 using OutputPixelType = typename TOutputImage::PixelType;
254
256 static constexpr unsigned int OutputImageDimension = TOutputImage::ImageDimension;
257 static constexpr unsigned int InputImageDimension = TInputImage::ImageDimension;
258
259
260#ifdef ITK_USE_CONCEPT_CHECKING
263
264
266#endif
267
268
269protected:
272
274#if ITK_VERSION_MAJOR < 5
275 void
276 ThreadedGenerateData(const OutputImageRegionType & outputRegionForThread, ThreadIdType threadId) override;
277#else
278 void
279 DynamicThreadedGenerateData(const OutputImageRegionType & outputRegionForThread) override;
280#endif
281
282private:
283 BoxSigmaSqrtNMinusOneImageFilter(const Self &); // purposely not implemented
284 void
285 operator=(const Self &); // purposely not implemented
286
287}; // end of class
288
289} // end namespace itk
290
291#ifndef ITK_MANUAL_INSTANTIATION
292# include "itkBoxSigmaSqrtNMinusOneImageFilter.hxx"
293#endif
294
295#endif
void ThreadedGenerateData(const OutputImageRegionType &outputRegionForThread, ThreadIdType threadId) override
~BoxSigmaSqrtNMinusOneImageFilter() override=default
Read-only ImageRegionIterator: ImageRegionConstIterator<TImage> behaves as ImageRegionIterator<const ...
A multi-dimensional iterator templated over image type that walks an image region and is specialized ...
A multi-dimensional iterator templated over image type that walks a region of pixels.
void SetSize(const SizeType &size)
virtual void DynamicThreadedGenerateData(const OutputImageRegionType &outputRegionForThread)
typename OutputImageType::RegionType OutputImageRegionType
Implements transparent reference counting.
A function object that determines a neighborhood of values at an image boundary according to a Neuman...
#define itkConceptMacro(name, conceptType)
Compile-time check that a template argument satisfies an itk::Concept.
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
std::vector< typename TImage::OffsetType > CornerOffsets(const TImage *im)
long OffsetValueType
Definition itkIntTypes.h:89
void BoxSigmaSqrtNMinusOneCalculatorFunction(const TInputImage *accImage, TOutputImage *outputImage, const typename TInputImage::RegionType &inputRegion, const typename TOutputImage::RegionType &outputRegion, const typename TInputImage::SizeType &Radius)
unsigned int ThreadIdType
Definition itkIntTypes.h:98
const IndexValueType * GetIndex() const
Definition itkIndex.h:227
void SetIndex(const IndexValueType val[VDimension])
Definition itkIndex.h:237
Splits an image into a main region and several "face" regions which are used to handle computations o...