forked from InsightSoftwareConsortium/ITK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
itkVoronoiPartitioningImageFilter.txx
216 lines (187 loc) · 5.85 KB
/
itkVoronoiPartitioningImageFilter.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
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: itkVoronoiPartitioningImageFilter.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 _itkVoronoiPartitioningImageFilter_txx
#define _itkVoronoiPartitioningImageFilter_txx
#include "itkVoronoiPartitioningImageFilter.h"
#include "itkImageRegionIteratorWithIndex.h"
#include "itkImageRegionConstIteratorWithIndex.h"
namespace itk
{
/* constructor: seting the default value of the parameters */
template <class TInputImage, class TOutputImage>
VoronoiPartitioningImageFilter <TInputImage,TOutputImage>
::VoronoiPartitioningImageFilter()
: m_SigmaThreshold( 10 )
{
}
/* destructor */
template <class TInputImage, class TOutputImage>
VoronoiPartitioningImageFilter <TInputImage,TOutputImage>
::~VoronoiPartitioningImageFilter()
{
}
template <class TInputImage, class TOutputImage>
void
VoronoiPartitioningImageFilter<TInputImage,TOutputImage>
::ClassifyDiagram(void)
{
PointIdIterator currPit;
PointIdIterator currPitEnd;
PointType currP;
PointTypeDeque VertList;
IndexList PixelPool;
this->m_NumberOfBoundary = 0;
for(int i=0;i<this->GetNumberOfSeeds();i++)
{
CellAutoPointer currCell;
this->m_WorkingVD->GetCellId(i,currCell);
currPitEnd = currCell->PointIdsEnd();
VertList.clear();
for(currPit=currCell->PointIdsBegin();currPit!=currPitEnd;++currPit)
{
this->m_WorkingVD->GetPoint((*currPit),&(currP));
VertList.push_back(currP);
}
PixelPool.clear();
this->GetPixelIndexFromPolygon(VertList,&PixelPool);
this->m_NumberOfPixels[i] = PixelPool.size();
this->m_Label[i] = this->TestHomogeneity(PixelPool);
// when partitioning the NumberOfBoundary is the number of regions that
// are not homogeneous
if (!this->m_Label[i])
{
this->m_NumberOfBoundary++;
}
}
}
template <class TInputImage, class TOutputImage>
void
VoronoiPartitioningImageFilter<TInputImage,TOutputImage>
::GenerateAddingSeeds(void)
{
EdgeIterator eit;
EdgeIterator eitend = this->m_WorkingVD->EdgeEnd();
PointType adds;
Point<int,2> seeds;
// Walk the edges of the diagram, if a seed to either side is
// no homogeneous, then place a new seed along the middle of the edge
for(eit = this->m_WorkingVD->EdgeBegin();eit != eitend; ++eit)
{
seeds = this->m_WorkingVD->GetSeedsIDAroundEdge(&*eit);
// if either seed is not homogeneous
if( ( !this->m_Label[seeds[0]] || !this->m_Label[seeds[1]])
&& (this->m_NumberOfPixels[seeds[0]]>this->GetMinRegion())
&& (this->m_NumberOfPixels[seeds[1]]>this->GetMinRegion()) )
{
adds[0] = (eit->m_Left[0] + eit->m_Right[0])*0.5;
adds[1] = (eit->m_Left[1] + eit->m_Right[1])*0.5;
this->m_SeedsToAdded.push_back(adds);
}
}
}
template <class TInputImage, class TOutputImage>
void
VoronoiPartitioningImageFilter <TInputImage,TOutputImage>
::MakeSegmentBoundary(void)
{
RegionType region = this->GetInput()->GetRequestedRegion();
itk::ImageRegionIteratorWithIndex <OutputImageType> oit(this->GetOutput(), region);
while( !oit.IsAtEnd())
{
oit.Set(0);
++oit;
}
NeighborIdIterator nit;
NeighborIdIterator nitend;
for(int i=0;i<this->GetNumberOfSeeds();i++)
{
nitend = this->m_WorkingVD->NeighborIdsEnd(i);
for(nit=this->m_WorkingVD->NeighborIdsBegin(i);nit!=nitend;++nit)
{
if ((*nit)>i)
{
drawLine(this->m_WorkingVD->GetSeed(i),this->m_WorkingVD->GetSeed(*nit));
i=i;
}
}
}
}
template <class TInputImage, class TOutputImage>
void
VoronoiPartitioningImageFilter<TInputImage,TOutputImage>
::MakeSegmentObject(void)
{
RegionType region = this->GetInput()->GetRequestedRegion();
itk::ImageRegionIteratorWithIndex <OutputImageType> oit(this->GetOutput(), region);
while( !oit.IsAtEnd())
{
oit.Set(0);
++oit;
}
PointIdIterator currPit;
PointIdIterator currPitEnd;
PointType currP;
PointTypeDeque VertList;
for(int i=0;i<this->GetNumberOfSeeds();i++)
{
CellAutoPointer currCell;
this->m_WorkingVD->GetCellId(i, currCell);
currPitEnd = currCell->PointIdsEnd();
VertList.clear();
for(currPit=currCell->PointIdsBegin();currPit!=currPitEnd;++currPit)
{
this->m_WorkingVD->GetPoint((*currPit),&(currP));
VertList.push_back(currP);
}
// Need to fill with an segment identifier
FillPolygon(VertList, static_cast<OutputPixelType>(i));
}
}
template <class TInputImage, class TOutputImage>
bool
VoronoiPartitioningImageFilter <TInputImage,TOutputImage>
::TestHomogeneity(IndexList &Plist)
{
int num=Plist.size();
int i;
double getp;
double addp=0;
double addpp=0;
for(i=0;i<num;i++)
{
getp = (double)(this->GetInput()->GetPixel(Plist[i]));
addp=addp+getp;
addpp=addpp+getp*getp;
}
double savevar;
if(num > 1)
{
savevar = vcl_sqrt((addpp - (addp*addp)/static_cast<double>(num) )
/(static_cast<double>(num)-1.0));
}
else
{
savevar = -1;
}
return (savevar >= 0 && vcl_sqrt(savevar) < m_SigmaThreshold);
}
template <class TInputImage, class TOutputImage>
void
VoronoiPartitioningImageFilter <TInputImage,TOutputImage>
::PrintSelf (std::ostream &os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "SigmaThreshold: " << m_SigmaThreshold << std::endl;
}
} //end namespace
#endif