forked from InsightSoftwareConsortium/ITK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
itkFastChamferDistanceImageFilter.txx
290 lines (246 loc) · 7.4 KB
/
itkFastChamferDistanceImageFilter.txx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: itkFastChamferDistanceImageFilter.txx
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef _itkFastChamferDistanceImageFilter_txx
#define _itkFastChamferDistanceImageFilter_txx
#include <iostream>
#include "itkFastChamferDistanceImageFilter.h"
#include <itkNeighborhoodIterator.h>
#include <itkImageRegionIterator.h>
#include <itkImageRegionConstIterator.h>
namespace itk
{
template <class TInputImage,class TOutputImage>
FastChamferDistanceImageFilter<TInputImage,TOutputImage>
::FastChamferDistanceImageFilter()
{
unsigned int i;
unsigned int dim = ImageDimension;
switch (ImageDimension)
{
// Note the fall through the cases to set all the components
case 3:
m_Weights[--dim] = 1.65849;
case 2:
m_Weights[--dim] = 1.34065;
case 1:
m_Weights[--dim] = 0.92644;
break;
default:
itkWarningMacro(<< "Dimension " << ImageDimension << " with Default weights ");
for( i=1; i <= ImageDimension; i++ )
{
m_Weights[i-1] = vcl_sqrt((float)i);
}
}
m_MaximumDistance = 10.0;
m_NarrowBand = 0;
}
template <class TInputImage,class TOutputImage>
void FastChamferDistanceImageFilter<TInputImage,TOutputImage>
::GenerateDataND()
{
const int SIGN_MASK = 1;
const int INNER_MASK = 2;
typename NeighborhoodIterator<TInputImage>::RadiusType r;
bool in_bounds;
r.Fill(1);
NeighborhoodIterator<TInputImage> it(r, this->GetOutput(), m_RegionToProcess);
const unsigned int center_voxel = it.Size()/2;
int *neighbor_type;
neighbor_type = new int[ it.Size() ];
int i;
unsigned int n;
float val[ImageDimension];
PixelType center_value;
int neighbor_start,neighbor_end;
BandNodeType node;
/** 1st Scan , using neighbors from center_voxel+1 to it.Size()-1 */
/** Precomputing the neighbor types */
neighbor_start = center_voxel + 1;
neighbor_end = it.Size() - 1;
for ( i = neighbor_start; i <= neighbor_end; i++)
{
neighbor_type[i] = -1;
for( n = 0; n < ImageDimension; n++ )
{
neighbor_type[i] += (it.GetOffset(i)[n] != 0);
}
}
/** Scan the image */
for ( it.GoToBegin(); ! it.IsAtEnd(); ++it )
{
center_value = it.GetPixel(center_voxel);
if (center_value>= m_MaximumDistance)
{
continue;
}
if (center_value<= -m_MaximumDistance)
{
continue;
}
/** Update Positive Distance */
if (center_value>-m_Weights[0])
{
for(n=0; n<ImageDimension; n++)
{
val[n]=center_value+m_Weights[n];
}
for (i = neighbor_start; i <= neighbor_end; i++)
{
// Experiment an InlineGetPixel()
if (val[neighbor_type[i]]<it.GetPixel(i))
{
it.SetPixel(i,val[neighbor_type[i]],in_bounds);
}
}
}
/** Update Negative Distance */
if (center_value<m_Weights[0])
{
for(n=0; n<ImageDimension; n++)
{
val[n]=center_value-m_Weights[n];
}
for (i = neighbor_start; i <= neighbor_end; i++)
{
// Experiment an InlineGetPixel()
if (val[neighbor_type[i]]>it.GetPixel(i))
{
it.SetPixel(i,val[neighbor_type[i]],in_bounds);
}
}
}
}
/** 2nd Scan , using neighbors from 0 to center_voxel-1 */
/*Clear the NarrowBand if it has been assigned */
if (m_NarrowBand.IsNotNull())
{
m_NarrowBand->Clear();
}
/** Precomputing the neighbor neighbor types */
neighbor_start = 0;
neighbor_end = center_voxel-1;
for( i = neighbor_start; i <= neighbor_end; i++ )
{
neighbor_type[i] = -1;
for (n=0; n < ImageDimension; n++)
{
neighbor_type[i] += (it.GetOffset(i)[n] != 0);
}
}
/** Scan the image */
for (it.GoToEnd(), --it; ! it.IsAtBegin(); --it)
{
center_value = it.GetPixel(center_voxel);
if (center_value>= m_MaximumDistance)
{
continue;
}
if (center_value<= -m_MaximumDistance)
{
continue;
}
// Update the narrow band
if (m_NarrowBand.IsNotNull()) {
if (fabs((float)center_value) <= m_NarrowBand->GetTotalRadius())
{
node.m_Index = it.GetIndex();
//Check node state.
node.m_NodeState = 0;
if (center_value>0)
{
node.m_NodeState += SIGN_MASK;
}
if (fabs((float)center_value) < m_NarrowBand->GetInnerRadius())
{
node.m_NodeState += INNER_MASK;
}
m_NarrowBand->PushBack(node);
}
}
/** Update Positive Distance */
if (center_value>-m_Weights[0])
{
for(n=0; n<ImageDimension; n++)
{
val[n]=center_value+m_Weights[n];
}
for (i = neighbor_start; i <= neighbor_end; i++)
{
// Experiment an InlineGetPixel()
if (val[neighbor_type[i]]<it.GetPixel(i))
{
it.SetPixel(i,val[neighbor_type[i]],in_bounds);
}
}
}
/** Update Negative Distance */
if (center_value<m_Weights[0])
{
for(n=0; n<ImageDimension; n++)
{
val[n]=center_value-m_Weights[n];
}
for (i = neighbor_start; i <= neighbor_end; i++)
{
// Experiment an InlineGetPixel()
if ( val[neighbor_type[i]] > it.GetPixel(i) )
{
it.SetPixel(i,val[neighbor_type[i]],in_bounds);
}
}
}
}
delete [] neighbor_type;
}
template <class TInputImage,class TOutputImage>
void
FastChamferDistanceImageFilter<TInputImage,TOutputImage>
::GenerateData()
{
// Allocate the output image.
typename TOutputImage::Pointer output = this->GetOutput();
output->SetBufferedRegion(output->GetRequestedRegion());
output->Allocate();
ImageRegionIterator<TOutputImage>
out(this->GetOutput(),this->GetInput()->GetRequestedRegion());
ImageRegionConstIterator<TOutputImage>
in( this->GetInput(), this->GetInput()->GetRequestedRegion());
for(in.GoToBegin(),out.GoToBegin(); !in.IsAtEnd(); ++in,++out)
{
out.Set(in.Get());
}
m_RegionToProcess = this->GetInput()->GetRequestedRegion();
//If the NarrowBand has been set, we update m_MaximumDistance using
//narrowband TotalRadius plus a margin of 1 pixel.
if ( m_NarrowBand.IsNotNull() )
{
m_MaximumDistance = m_NarrowBand->GetTotalRadius() + 1;
}
this->GenerateDataND();
} // end GenerateData()
template <class TInputImage,class TOutputImage>
void
FastChamferDistanceImageFilter<TInputImage,TOutputImage>
::PrintSelf(std::ostream& os, Indent indent) const
{
unsigned int i;
Superclass::PrintSelf(os,indent);
for ( i = 0; i < ImageDimension; i++ )
{
os << indent << "Chamfer weight " << i << ": " << m_Weights[i] << std::endl;
}
os << indent << "Maximal computed distance : " << m_MaximumDistance << std::endl;
}
} // end namespace itk
#endif