00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef __itkConnectedComponentImageFilter_h
00019 #define __itkConnectedComponentImageFilter_h
00020
00021 #include "itkImageToImageFilter.h"
00022 #include "itkImage.h"
00023 #include "itkConceptChecking.h"
00024 #include <vector>
00025 #include <map>
00026 #include "itkProgressReporter.h"
00027
00028 namespace itk
00029 {
00030
00049 template <class TInputImage, class TOutputImage, class TMaskImage=TInputImage>
00050 class ITK_EXPORT ConnectedComponentImageFilter :
00051 public ImageToImageFilter< TInputImage, TOutputImage >
00052 {
00053 public:
00057 typedef ConnectedComponentImageFilter Self;
00058 typedef ImageToImageFilter< TInputImage, TOutputImage > Superclass;
00059
00063 typedef typename Superclass::InputImagePointer InputImagePointer;
00064
00069 typedef typename TOutputImage::PixelType OutputPixelType;
00070 typedef typename TOutputImage::InternalPixelType OutputInternalPixelType;
00071 typedef typename TInputImage::PixelType InputPixelType;
00072 typedef typename TInputImage::InternalPixelType InputInternalPixelType;
00073 typedef typename TMaskImage::PixelType MaskPixelType;
00074 itkStaticConstMacro(ImageDimension, unsigned int,
00075 TOutputImage::ImageDimension);
00076 itkStaticConstMacro(OutputImageDimension, unsigned int,
00077 TOutputImage::ImageDimension);
00078 itkStaticConstMacro(InputImageDimension, unsigned int,
00079 TInputImage::ImageDimension);
00081
00085 typedef TInputImage InputImageType;
00086 typedef TMaskImage MaskImageType;
00087 typedef typename TInputImage::IndexType IndexType;
00088 typedef typename TInputImage::SizeType SizeType;
00089 typedef typename TInputImage::OffsetType OffsetType;
00090
00091 typedef TOutputImage OutputImageType;
00092 typedef typename TOutputImage::RegionType RegionType;
00093 typedef typename TOutputImage::IndexType OutputIndexType;
00094 typedef typename TOutputImage::SizeType OutputSizeType;
00095 typedef typename TOutputImage::OffsetType OutputOffsetType;
00096
00097 typedef std::list<IndexType> ListType;
00098 typedef typename MaskImageType::Pointer MaskImagePointer;
00099
00103 typedef SmartPointer<Self> Pointer;
00104 typedef SmartPointer<const Self> ConstPointer;
00105
00109 itkTypeMacro(ConnectedComponentImageFilter, ImageToImageFilter);
00110
00114 itkNewMacro(Self);
00115
00122 itkSetMacro(FullyConnected, bool);
00123 itkGetConstReferenceMacro(FullyConnected, bool);
00124 itkBooleanMacro(FullyConnected);
00126
00127
00128 itkGetConstReferenceMacro(ObjectCount, unsigned long);
00129
00130
00131 itkConceptMacro(SameDimension,
00132 (Concept::SameDimension<itkGetStaticConstMacro(InputImageDimension),
00133 itkGetStaticConstMacro(OutputImageDimension)>));
00134
00135
00136 void SetMaskImage(TMaskImage* mask)
00137 {
00138 this->SetNthInput(1, const_cast<TMaskImage *>( mask ));
00139 }
00140
00141 const TMaskImage* GetMaskImage() const
00142 {
00143 return (static_cast<const TMaskImage*>(this->ProcessObject::GetInput(1)));
00144 }
00145
00146 protected:
00147 ConnectedComponentImageFilter()
00148 {
00149 m_FullyConnected = false;
00150 m_ObjectCount = 0;
00151 }
00152 virtual ~ConnectedComponentImageFilter() {}
00153 ConnectedComponentImageFilter(const Self&) {}
00154 void PrintSelf(std::ostream& os, Indent indent) const;
00155
00159 void GenerateData();
00160
00164 void GenerateInputRequestedRegion();
00165
00170 void EnlargeOutputRequestedRegion(DataObject *itkNotUsed(output));
00171
00172 bool m_FullyConnected;
00173
00174 private:
00175 unsigned long m_ObjectCount;
00176
00177 typedef typename TOutputImage::RegionType::SizeType OutSizeType;
00178
00179
00180 class runLength
00181 {
00182 public:
00183
00184 long int length;
00185 typename InputImageType::IndexType where;
00186 unsigned long int label;
00187 };
00188
00189 typedef std::vector<runLength> lineEncoding;
00190
00191
00192 typedef std::map<long, lineEncoding> LineMapType;
00193
00194 typedef std::vector<long> OffsetVec;
00195
00196
00197 typedef std::vector<unsigned long int> UnionFindType;
00198 UnionFindType m_UnionFind;
00199 UnionFindType m_Consecutive;
00200
00201 void InitUnion(const unsigned long int size)
00202 {
00203 m_UnionFind = UnionFindType(size + 1);
00204 }
00205 void InsertSet(const unsigned long int label);
00206 unsigned long int LookupSet(const unsigned long int label);
00207 void LinkLabels(const unsigned long int lab1, const unsigned long int lab2);
00208 unsigned long int CreateConsecutive();
00210 bool CheckNeighbors(const OutputIndexType &A,
00211 const OutputIndexType &B);
00212
00213 void CompareLines(lineEncoding ¤t, const lineEncoding &Neighbour);
00214
00215 void FillOutput(const LineMapType &LineMap,
00216 ProgressReporter &progress);
00217
00218 void SetupLineOffsets(OffsetVec &LineOffsets);
00219 };
00220
00221 }
00222
00223 #ifndef ITK_MANUAL_INSTANTIATION
00224 #include "itkConnectedComponentImageFilter.txx"
00225 #endif
00226
00227 #endif
00228