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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: itkAttributeMorphologyBaseImageFilter.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 __itkAttributeMorphologyBaseImageFilter_txx
#define __itkAttributeMorphologyBaseImageFilter_txx
#include "itkAttributeMorphologyBaseImageFilter.h"
#include "itkImageRegionIterator.h"
#include "itkImageRegionConstIteratorWithIndex.h"
#include "itkNumericTraits.h"
#include "itkProgressReporter.h"
#include "itkConnectedComponentAlgorithm.h"
#include "itkConstShapedNeighborhoodIterator.h"
#include "itkNeighborhoodAlgorithm.h"
namespace itk
{
template< class TInputImage, class TOutputImage, class TAttribute, class TFunction>
void
AttributeMorphologyBaseImageFilter< TInputImage, TOutputImage, TAttribute, TFunction >
::GenerateInputRequestedRegion()
{
// call the superclass' implementation of this method
Superclass::GenerateInputRequestedRegion();
// We need all the input.
InputImagePointer input = const_cast<InputImageType *>(this->GetInput());
input->SetRequestedRegion( input->GetLargestPossibleRegion() );
}
template <class TInputImage, class TOutputImage, class TAttribute, class TFunction>
void
AttributeMorphologyBaseImageFilter<TInputImage, TOutputImage, TAttribute, TFunction >
::EnlargeOutputRequestedRegion(DataObject *)
{
this->GetOutput()
->SetRequestedRegion( this->GetOutput()->GetLargestPossibleRegion() );
}
template< class TInputImage, class TOutputImage, class TAttribute, class TFunction >
void
AttributeMorphologyBaseImageFilter< TInputImage, TOutputImage, TAttribute, TFunction >
::GenerateData()
{
typename TOutputImage::Pointer output = this->GetOutput();
typename TInputImage::ConstPointer input = this->GetInput();
// Allocate the output
this->AllocateOutputs();
TFunction compare;
unsigned long buffsize = output->GetRequestedRegion().GetNumberOfPixels();
SizeType kernelRadius;
kernelRadius.Fill(1);
typedef itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<TInputImage>
FaceCalculatorType;
FaceCalculatorType faceCalculator;
typename FaceCalculatorType::FaceListType faceList;
faceList = faceCalculator(input, output->GetRequestedRegion(), kernelRadius);
typename FaceCalculatorType::FaceListType::iterator fit;
ProgressReporter progress(this, 0, buffsize*4); // pretend we have 4 steps
fit = faceList.begin();
m_SortPixels = new GreyAndPos[buffsize];
m_Parent = new long[buffsize];
#ifndef PAMI
m_Processed = new bool[buffsize];
#endif
// This is a bit ugly, but I can't see an easy way around
m_Raw = new InputPixelType[buffsize];
m_AuxData = new AttributeType[buffsize];
// copy the pixels to the sort buffer
typedef ImageRegionConstIteratorWithIndex<TInputImage> CRegionIteratorType;
CRegionIteratorType RegIt(input, output->GetRequestedRegion());
//IndexType Origin = RegIt.GetIndex();
long int pos = 0;
for (RegIt.GoToBegin();!RegIt.IsAtEnd();++RegIt, ++pos)
{
GreyAndPos P;
P.Val = RegIt.Get();
P.Pos = pos;
m_SortPixels[pos] = P;
m_Raw[pos] = P.Val;
#ifndef PAMI
m_Processed[pos]=false;
#endif
m_Parent[pos]=INACTIVE;
m_AuxData[pos] = -1; // invalid value;
progress.CompletedPixel();
}
progress.CompletedPixel();
std::sort(&(m_SortPixels[0]), &(m_SortPixels[buffsize - 1]), ComparePixStruct());
progress.CompletedPixel();
// set up the offset vector
OffsetVecType TheseOffsets;
OffsetDirectVecType TheseDirectOffsets;
SetupOffsetVec(TheseDirectOffsets, TheseOffsets);
// the core algorithm
// process first pixel
#ifdef PAMI
MakeSet(m_SortPixels[0].Pos);
//m_Processed[0] = true;
for (unsigned long k = 1; k < buffsize;k++)
{
long ThisPos = m_SortPixels[k].Pos;
IndexType ThisWhere = input->ComputeIndex( ThisPos );
InputPixelType ThisPix = m_SortPixels[k].Val;
MakeSet(ThisPos);
// Some optimization of bounds check
if (fit->IsInside(ThisWhere))
{
// no need for bounds check on neighbours
for (unsigned i = 0; i<TheseDirectOffsets.size();i++)
{
long NeighInd = ThisPos + TheseDirectOffsets[i];
InputPixelType NeighPix = m_Raw[NeighInd];
if (compare(NeighPix, ThisPix) || ((ThisPix == NeighPix) && (NeighInd < ThisPos)))
{
Union(NeighInd, ThisPos);
}
}
}
else
{
// need a bounds check for each neighbour
for (unsigned i = 0; i<TheseOffsets.size();i++)
{
if (output->GetRequestedRegion().IsInside(ThisWhere + TheseOffsets[i]))
{
long NeighInd = ThisPos + TheseDirectOffsets[i];
InputPixelType NeighPix = m_Raw[NeighInd];
if (compare(NeighPix, ThisPix) || ((ThisPix == NeighPix) && (NeighInd < ThisPos)))
{
Union(NeighInd, ThisPos);
}
}
}
}
progress.CompletedPixel();
}
#else
MakeSet(m_SortPixels[0].Pos);
m_Processed[0] = true;
for (long k = 1; k < buffsize;k++)
{
long ThisPos, PrevPos;
InputPixelType ThisPix, PrevPix;
ThisPos = m_SortPixels[k].Pos;
PrevPos = m_SortPixels[k-1].Pos;
ThisPix = m_Raw[ThisPos];
PrevPix = m_Raw[PrevPos];
IndexType ThisWhere = input->ComputeIndex( ThisPos );
if (ThisPix != PrevPix)
{
for (long QPos = k-1; QPos >= 0; --QPos)
{
long QLoc = m_SortPixels[QPos].Pos;
if (m_Raw[QLoc] != PrevPix)
{
break;
}
if ((m_Parent[QLoc] == ACTIVE) &&
(m_AuxData[QLoc] >= m_Lambda))
{
m_Parent[QLoc] = INACTIVE;
m_AuxData[QLoc]=-1;
// dispose auxdata[QLoc]
}
}
}
MakeSet(ThisPos);
if (fit->IsInside(ThisWhere))
{
// no need for neighbor bounds check
for (unsigned i = 0; i<TheseDirectOffsets.size();i++)
{
long NeighInd = ThisPos + TheseDirectOffsets[i];
if (m_Processed[NeighInd])
{
Union(NeighInd, ThisPos);
}
}
}
else
{
for (unsigned i = 0; i<TheseOffsets.size();i++)
{
if (output->GetRequestedRegion().IsInside(ThisWhere + TheseOffsets[i]))
{
long NeighInd = ThisPos + TheseDirectOffsets[i];
if (m_Processed[NeighInd])
{
Union(NeighInd, ThisPos);
}
}
}
}
m_Processed[ThisPos] = true;
progress.CompletedPixel();
}
#endif
// resolving phase
// copy pixels back
typedef ImageRegionIterator<TOutputImage> RegionIteratorType;
RegionIteratorType ORegIt(output, output->GetRequestedRegion());
ORegIt.GoToBegin();
// fill Raw - worry about iteration details later.
// We aren't filling m_Parent, as suggested in the paper, because it
// is an integer array. We want this to work with float types
#ifdef PAMI
// write the new image to Raw - note that we aren't putting the
// result in parent
for (pos = buffsize - 1;pos >= 0; --pos)
{
long RPos = m_SortPixels[pos].Pos;
if (m_Parent[RPos] >= 0)
{
m_Raw[RPos] = m_Raw[m_Parent[RPos]];
}
#if 0
else
{
// Original value already in raw
}
#endif
progress.CompletedPixel();
}
for (unsigned long ppos = 0;ppos < buffsize; ++ppos, ++ORegIt)
{
ORegIt.Set(static_cast<OutputPixelType>(m_Raw[ppos]));
progress.CompletedPixel();
}
#else
// the version from the paper
for (pos = buffsize - 1;pos >= 0; --pos)
{
long RPos = m_SortPixels[pos].Pos;
if (m_Parent[RPos] < 0)
{
m_Parent[RPos] = (long)m_Raw[RPos];
}
else
{
m_Parent[RPos] = m_Parent[m_Parent[RPos]];
}
progress.CompletedPixel();
}
for (pos = 0;pos < buffsize; ++pos, ++ORegIt)
{
ORegIt.Set(static_cast<OutputPixelType>(m_Parent[pos]));
progress.CompletedPixel();
}
#endif
delete [] m_Raw;
delete [] m_SortPixels;
delete [] m_Parent;
#ifndef PAMI
delete [] m_Processed;
#endif
delete [] m_AuxData;
}
template< class TInputImage, class TOutputImage, class TAttribute, class TFunction >
void
AttributeMorphologyBaseImageFilter< TInputImage, TOutputImage, TAttribute, TFunction >
::SetupOffsetVec(OffsetDirectVecType &PosOffsets, OffsetVecType &Offsets)
{
typedef ConstShapedNeighborhoodIterator<TOutputImage> NeighType;
SizeType KernRad;
KernRad.Fill(1);
NeighType It(KernRad,
this->GetOutput(), this->GetOutput()->GetRequestedRegion());
setConnectivity(&It, m_FullyConnected);
typename NeighType::IndexListType OffsetList;
typename NeighType::IndexListType::const_iterator LIt;
OffsetList = It.GetActiveIndexList();
IndexType idx = this->GetOutput()->GetRequestedRegion().GetIndex();
long offset = this->GetOutput()->ComputeOffset( idx );
for (LIt = OffsetList.begin(); LIt != OffsetList.end(); LIt++)
{
OffsetType O = It.GetOffset(*LIt);
PosOffsets.push_back( this->GetOutput()->ComputeOffset( idx + O ) - offset );
Offsets.push_back(O);
}
}
template< class TInputImage, class TOutputImage, class TAttribute, class TFunction >
void
AttributeMorphologyBaseImageFilter< TInputImage, TOutputImage, TAttribute, TFunction >
::PrintSelf(std::ostream& os, Indent indent) const
{
Superclass::PrintSelf(os,indent);
os << indent << "FullyConnected: " << m_FullyConnected << std::endl;
os << indent << "Lambda: " << static_cast< typename NumericTraits< AttributeType >::PrintType>( m_Lambda ) << std::endl;
}
} // end namespace itk
#endif
|