Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Label.hh
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 
26 #ifndef __Label_HH__
27 #define __Label_HH__
28 
29 #include <bias_config.h>
30 
31 #include <vector>
32 #include <map>
33 #include <new>
34 
35 #include <Base/Debug/Debug.hh>
36 #include <Base/Image/Image.hh>
37 #include <Base/Math/Vector2.hh>
38 #include "FilterBase.hh"
39 
40 #define LABEL_STORAGE_TYPE ImageBase::ST_int
41 #define LABEL_CALC_TYPE int
42 #define D_INIT 0x0001
43 #define D_LABEL 0x0002
44 
45 
46 namespace BIAS
47 {
48  // for sorting SortVec_
49  class /*BIASFilter_EXPORT*/ CCAStruct {
50  public:
51  CCAStruct() {};
52  CCAStruct(const CCAStruct& s){
53  bc=s.bc; tl=s.tl; br=s.br; center=s.center; x=s.x; y=s.y; }
54 
55  int bc; ///< blob count = number of pixesl in blob
56  BIAS::Vector2<int> tl, br; // bounding box of blob
57  BIAS::Vector2<double> center; // blob center
58  std::vector<int> x, y; // for median computation
59 
61  bc=s.bc; tl=s.tl; br=s.br; center= s.center; x=s.x; y=s.y;return *this; }
62  bool operator<(const CCAStruct &right) const {
63  return bc>right.bc; };
64  bool operator==(const CCAStruct &right) const {
65  return bc==right.bc; };
66  };
67 
68 
69  /** @class Label
70  @ingroup g_filter
71  @brief does a connected component analysis and labels all connected
72  regions
73 
74  there is no difference between fore and background. Each region that
75  has a different color than all its neighbouring pixels is its own region.
76  Each pixel is assigned to one region.
77 
78  There are utility functions to get Blobcounts and bounding boxes.
79  So far only 4neighborhoud and grey value images only.
80  Important: When using Filter(), the resulting image is only valid
81  if there are less than 255 regions!!
82  @author woelk
83  */
84  class BIASFilter_EXPORT Label
85  : public BIAS::FilterBase<unsigned char, unsigned char>
86  {
87  public:
88  Label();
89  ~Label();
90 
91  /** call this if you don't need the output image, is faster than Filter
92  */
93  int Process(const Image<unsigned char>& im);
94 
95  virtual int Filter(const Image<unsigned char>& src,
97 
98  /** labels an unsigned short int using an 4er neighbourhood and
99  returns it in DestinationImage
100  if DestinationImage is allocated it is not reallocated
101  @status untested
102  @author Felix Woelk
103  */
104  int Label4Neighbour(const BIAS::Image<unsigned char>& Image,
105  BIAS::Image<unsigned char>& DestinationImage);
106 
107 
108 
109  /** labels an unsigned short int using an 4er neighbourhood and
110  returns it in DestinationImage
111  if DestinationImage is allocated it is not reallocated */
112  int Label4Neighbour(const BIAS::Image<LABEL_CALC_TYPE>& Image,
113  BIAS::Image<LABEL_CALC_TYPE>& DestinationImage);
114 
115  /** returns a list with the size of each blob, largest first,
116  if no blobs are found a list with a single 0 is returned
117  @author Daniel Grest, Aug. 2004
118  */
119  inline std::vector<int> GetBlobCounts()
120  { return BlobCount_; }
121 
122  /** Returns a map that hold the blob size for each label. */
123  inline std::map<unsigned char, int> GetBlobMap()
124  { return BlobMap_; }
125 
126  /** returns in li the image where each region has a different grey value,
127  value zero is the background region
128  */
130  { li=LabelResultImage_; }
131 
132  /** returns two lists,one with the topLeft coordinates of the
133  bounding boxes of each region and one with the botrights,
134  sorted by size of regions (in pixel)
135  @return 0, in case of no error
136  @author woelk
137  */
138  inline
139  int GetBoundingBoxes(std::vector<BIAS::Vector2<int> >& topLefts,
140  std::vector<BIAS::Vector2<int> >& botRights)
141  { topLefts = TopLefts_; botRights = BotRights_; return 0; };
142 
143  /** @brief returns the centers of the blobs
144  @author woelk 03/2006 */
145  inline void GetCenters(std::vector<BIAS::Vector2<double> >& centers) const
146  { centers = Centers_; }
147 
148  /** @brief returns the median centers of the blobs
149  @author woelk 03/2006 */
150  inline void GetMedianCenters(std::vector<BIAS::Vector2<double> >&cts) const
151  { cts = MedianCenters_; }
152 
153  inline void GetBlobSizes(std::vector<int>& sizes) const
154  { sizes = BlobCount_; }
155 
156  protected:
157 
158  // protecteds for Label filter
162  void LabelInit_(const ImageBase& Image);
163  void LabelRelease_();
164  int Label4Neighbour_();
165 
166  // for CCA funxtionality
167  std::vector<int> Num_;
168  std::vector<BIAS::CCAStruct> SortVec_;
169 
170  std::vector<int> BlobCount_;
171  std::vector<BIAS::Vector2<int> > TopLefts_, BotRights_;
172  std::vector<BIAS::Vector2<double> > Centers_;
173  std::vector<BIAS::Vector2<double> > MedianCenters_;
174 
175  virtual void GetBordersValid_(int& border_x, int& border_y) const;
176 
177 private: // avoid DLL export
178  std::map<unsigned char,int> BlobMap_;
179  };
180 
181 } // namespace
182 
183 #endif // __FILTER_HH__
std::vector< int > BlobCount_
Definition: Label.hh:170
std::map< unsigned char, int > GetBlobMap()
Returns a map that hold the blob size for each label.
Definition: Label.hh:123
std::vector< BIAS::Vector2< int > > TopLefts_
Definition: Label.hh:171
CCAStruct(const CCAStruct &s)
Definition: Label.hh:52
void GetBlobSizes(std::vector< int > &sizes) const
Definition: Label.hh:153
std::vector< BIAS::CCAStruct > SortVec_
Definition: Label.hh:168
void GetCenters(std::vector< BIAS::Vector2< double > > &centers) const
returns the centers of the blobs
Definition: Label.hh:145
virtual parent class for API definition of all (future) filters
Definition: FilterBase.hh:77
CCAStruct & operator=(const CCAStruct &s)
Definition: Label.hh:60
int GetBoundingBoxes(std::vector< BIAS::Vector2< int > > &topLefts, std::vector< BIAS::Vector2< int > > &botRights)
returns two lists,one with the topLeft coordinates of the bounding boxes of each region and one with ...
Definition: Label.hh:139
bool LabelInitialized_
Definition: Label.hh:159
BIAS::Vector2< int > tl
Definition: Label.hh:56
std::vector< BIAS::Vector2< double > > Centers_
Definition: Label.hh:172
int bc
blob count = number of pixesl in blob
Definition: Label.hh:55
bool operator==(const CCAStruct &right) const
Definition: Label.hh:64
std::vector< int > Num_
Definition: Label.hh:167
bool operator<(const CCAStruct &right) const
Definition: Label.hh:62
Image< LABEL_CALC_TYPE > LabelResultImage_
Definition: Label.hh:161
std::vector< int > x
Definition: Label.hh:58
Image< LABEL_CALC_TYPE > LabelSumImage_
Definition: Label.hh:160
std::vector< int > y
Definition: Label.hh:58
BIAS::Vector2< int > br
Definition: Label.hh:56
std::vector< BIAS::Vector2< double > > MedianCenters_
Definition: Label.hh:173
This is the base class for images in BIAS.
Definition: ImageBase.hh:102
std::vector< int > GetBlobCounts()
returns a list with the size of each blob, largest first, if no blobs are found a list with a single ...
Definition: Label.hh:119
void GetMedianCenters(std::vector< BIAS::Vector2< double > > &cts) const
returns the median centers of the blobs
Definition: Label.hh:150
BIAS::Vector2< double > center
Definition: Label.hh:57
does a connected component analysis and labels all connected regions
Definition: Label.hh:84
void GetLabelImage(Image< LABEL_CALC_TYPE > &li)
returns in li the image where each region has a different grey value, value zero is the background re...
Definition: Label.hh:129