TImageSegmentation.h

00001 #ifndef __CImageSegmentation_H_2005_03_23_15_44_W_AI__
00002 #define __CImageSegmentation_H_2005_03_23_15_44_W_AI__ 1
00003 
00004 #include <vector>
00005 #include <map>
00006 #include <assert.h>
00007 
00008 namespace segmentation {
00009 
00010 // image segmentation - manages a matrix of segment indexes for each pixel
00011 template <class INDEX_TYPE = signed long int, INDEX_TYPE idxUndefinedSegment = -1>
00012 class TImageSegmentation
00013 {
00014 public:
00015   typedef INDEX_TYPE THE_INDEX_TYPE;
00016   enum { IdxUndefinedSegment = idxUndefinedSegment};
00017 
00018   // create non initialized segmentation object
00019   TImageSegmentation(long width=0, long height=0);
00020 
00021   // create copy of supplied segmentation object
00022   // (not really needed for now 'cause all the members have copy constructor)
00023   TImageSegmentation(const TImageSegmentation & copy);
00024 
00025   // destructor :-)
00026   ~TImageSegmentation();
00027 
00028   // create matrix with specified dimensions and initialize it all with one segment (index 0)
00029   bool Init(long width, long height);
00030 
00031   // direct access to index buffer
00032   inline INDEX_TYPE * operator * ();
00033   inline const INDEX_TYPE * operator * () const;
00034 
00035   // access to value in the index buffer
00036   inline INDEX_TYPE & operator () (const long x, const long y);
00037   inline const INDEX_TYPE & operator () (const long x, const long y) const;
00038 
00039   // return width of index matrix
00040   INDEX_TYPE Width() const
00041   { return mWidth; }
00042 
00043   // return height of index matrix
00044   INDEX_TYPE Height() const
00045   { return mHeight; }
00046 
00047   // determines and returns the used indexes (returns false if object not initialized)
00048   // (the size of each segment is the value of each element in the map)
00049   bool GetUsedSegmentIndexes(std::map<INDEX_TYPE, size_t> &indexSet) const;
00050 
00051   // reorders the segment indexes consecutively starting with 0
00052   bool ReorderSegmentIndexes();
00053 
00054   // returns the number of segments
00055   long GetSegmentCount() const;
00056 
00057 
00058 protected:
00059   typedef std::vector<INDEX_TYPE> INDEX_ARRAY;
00060   
00061   INDEX_ARRAY mIndexArray;
00062   long mWidth;
00063   long mHeight;
00064 
00065 
00066   // asserts if objects state is not valid and returns true if object is initialized
00067   inline
00068   bool InitDone() const
00069   {
00070     AssertValid();
00071     return mWidth>0;
00072   }
00073 
00074 #if defined(DEBUG) || defined(_DEBUG)
00075 
00076   inline
00077   void AssertValid() const // assert if object state is not valid
00078   {
00079     assert((mWidth==0 && mHeight==0 && mIndexArray.size()==0) || 
00080       (mWidth>0 && mHeight>0 && mIndexArray.size()==mWidth*mHeight));
00081   }
00082 
00083   inline
00084   void AssertNonEmpty() const // assert if object state is not valid or uninitialized
00085   {
00086     assert(InitDone()); // this should be enough because the whole consisteny is tested in AssertValid
00087   }
00088 
00089 #else // Release versions of these functions are empty
00090   inline
00091   void AssertValid() const {}
00092 
00093   inline
00094   void AssertNonEmpty() const {}
00095 #endif
00096 
00097 };
00098 
00099 } // namespace segmentation
00100 
00101 #include "TImageSegmentation.txx"
00102 
00103 #endif
00104 

Generated on Thu Jun 22 14:47:19 2006 for ncut.kdevelop by  doxygen 1.4.6