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
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: itkSimpleFuzzyConnectednessImageFilterBase.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 __itkSimpleFuzzyConnectednessImageFilterBase_txx
#define __itkSimpleFuzzyConnectednessImageFilterBase_txx
#include "itkSimpleFuzzyConnectednessImageFilterBase.h"
#include "vnl/vnl_math.h"
#include "itkImageRegionIteratorWithIndex.h"
#include "itkNumericTraits.h"
#include "itkProgressReporter.h"
namespace itk {
template <class TInputImage, class TOutputImage>
SimpleFuzzyConnectednessImageFilterBase<TInputImage,TOutputImage>
::SimpleFuzzyConnectednessImageFilterBase()
{
m_Threshold = 1.0;
m_ObjectSeed.Fill(0);
m_Weight = 1.0;
m_InsideValue = NumericTraits< OutputPixelType >::max();
m_OutsideValue = NumericTraits< OutputPixelType >::min();
}
template <class TInputImage, class TOutputImage>
SimpleFuzzyConnectednessImageFilterBase<TInputImage,TOutputImage>
::~SimpleFuzzyConnectednessImageFilterBase()
{
}
template <class TInputImage, class TOutputImage>
void
SimpleFuzzyConnectednessImageFilterBase<TInputImage,TOutputImage>
::PushNeighbors(const IndexType ¢er)
{
IndexType currentIndex =center;
for(unsigned int i = 0; i < ImageDimension; i++)
{
if( currentIndex[i] < static_cast<typename IndexType::IndexValueType>(m_Size[i])-1)
{
currentIndex[i]++;
m_Queue.push(currentIndex);
currentIndex[i]--;
}
if(currentIndex[i]>0)
{
currentIndex[i]--;
m_Queue.push(currentIndex);
currentIndex[i]++;
}
}
}
template <class TInputImage, class TOutputImage>
double
SimpleFuzzyConnectednessImageFilterBase<TInputImage,TOutputImage>
::FindStrongPath(const IndexType ¢er)
{
IndexType currentIndex = center;
double tmp=0;
double tmp1;
double tmp2;
PixelType centerpixel = m_InputImage->GetPixel(currentIndex);
if(currentIndex[0] > 0)
{
currentIndex[0]--;
tmp = (double)(m_FuzzyScene->GetPixel(currentIndex));
tmp1 = this->FuzzyAffinity(m_InputImage->GetPixel(currentIndex),centerpixel);
if(tmp > tmp1)
{
tmp=tmp1;
}
currentIndex[0]++;
}
if(currentIndex[0] < static_cast<typename IndexType::IndexValueType>(m_Size[0])-1)
{
currentIndex[0]++;
tmp2 = (double)(m_FuzzyScene->GetPixel(currentIndex));
tmp1 = this->FuzzyAffinity(m_InputImage->GetPixel(currentIndex), centerpixel);
if(tmp2 > tmp1)
{
tmp2 = tmp1;
}
if(tmp < tmp2){
tmp = tmp2;
}
currentIndex[0]--;
}
for(unsigned int i = 1;i < ImageDimension; i++)
{
if(currentIndex[i] > 0)
{
currentIndex[i]--;
tmp2 = (double)(m_FuzzyScene->GetPixel(currentIndex));
tmp1 = this->FuzzyAffinity(m_InputImage->GetPixel(currentIndex), centerpixel);
if(tmp2 > tmp1)
{
tmp2 = tmp1;
}
if(tmp < tmp2)
{
tmp = tmp2;
}
currentIndex[i]++;
}
if(currentIndex[i] < static_cast<typename IndexType::IndexValueType>(m_Size[i])-1)
{
currentIndex[i]++;
tmp2 = (double)(m_FuzzyScene->GetPixel(currentIndex));
tmp1 = this->FuzzyAffinity(m_InputImage->GetPixel(currentIndex), centerpixel);
if(tmp2 > tmp1)
{
tmp2 = tmp1;
}
if(tmp < tmp2)
{
tmp = tmp2;
}
currentIndex[i]--;
}
}
return(tmp);
}
template <class TInputImage, class TOutputImage>
void
SimpleFuzzyConnectednessImageFilterBase<TInputImage,TOutputImage>
::MakeSegmentObject()
{
RegionType regionOUT = m_SegmentObject->GetRequestedRegion();
typename FuzzySceneType::RegionType regionIN = m_FuzzyScene->GetRequestedRegion();
const double activeThreshold = (NumericTraits<unsigned short>::max()) * m_Threshold;
ImageRegionIteratorWithIndex < FuzzySceneType > it( m_FuzzyScene, regionIN );
ImageRegionIteratorWithIndex < OutputImageType > ot( m_SegmentObject, regionOUT );
while( !it.IsAtEnd())
{
if( it.Get() > activeThreshold )
{
ot.Set( m_InsideValue );
}
else
{
ot.Set( m_OutsideValue );
}
++it;
++ot;
}
}
template <class TInputImage, class TOutputImage>
void
SimpleFuzzyConnectednessImageFilterBase<TInputImage,TOutputImage>
::GenerateData()
{
IndexType currentIndex;
unsigned short fmax;
m_InputImage = this->GetInput();
m_SegmentObject = this->GetOutput();
m_Size = m_InputImage->GetLargestPossibleRegion().GetSize();
IndexType index;
index.Fill(0);
typename FuzzySceneType::RegionType region;
region.SetSize(m_Size);
region.SetIndex(index);
m_FuzzyScene = FuzzySceneType::New();
m_FuzzyScene->SetRegions( region );
m_FuzzyScene->Allocate();
m_FuzzyScene->FillBuffer( 0 );
m_FuzzyScene->CopyInformation(m_InputImage);
RegionType region1;
region1.SetSize(m_Size);
region1.SetIndex(index);
m_SegmentObject->SetRegions( region1 );
m_SegmentObject->Allocate();
this->PushNeighbors( m_ObjectSeed );
m_FuzzyScene->SetPixel( m_ObjectSeed, NumericTraits<unsigned short>::max() );
ProgressReporter progress(this, 0, region.GetNumberOfPixels() * 2 * m_InputImage->GetImageDimension() );
while( ! m_Queue.empty() )
{
currentIndex = m_Queue.front();
m_Queue.pop();
fmax = static_cast< unsigned short >( this->FindStrongPath( currentIndex ) );
if( fmax > m_FuzzyScene->GetPixel( currentIndex ) )
{
m_FuzzyScene->SetPixel( currentIndex, fmax );
this->PushNeighbors( currentIndex );
}
progress.CompletedPixel(); // potential exception thrown here
}
this->MakeSegmentObject();
}
template <class TInputImage, class TOutputImage>
void
SimpleFuzzyConnectednessImageFilterBase<TInputImage,TOutputImage>
::UpdateThreshold(const double x)
{
this->SetThreshold(x);
this->MakeSegmentObject();
}
template <class TInputImage, class TOutputImage>
void
SimpleFuzzyConnectednessImageFilterBase<TInputImage,TOutputImage>
::PrintSelf(std::ostream& os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "Weight: " << m_Weight << std::endl;
os << indent << "Threshold: " << m_Threshold << std::endl;
os << indent << "Inside value: "
<< static_cast<typename NumericTraits<OutputPixelType>::PrintType>(m_InsideValue)
<< std::endl;
os << indent << "Outside value: "
<< static_cast<typename NumericTraits<OutputPixelType>::PrintType>(m_OutsideValue)
<< std::endl;
os << indent << "Object seed : " << m_ObjectSeed << std::endl;
}
} /* end namespace itk. */
#endif
|