Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ContourDetectorSimple.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 #ifndef __CONTOURDETECTORSIMPLE_HH__
26 #define __CONTOURDETECTORSIMPLE_HH__
27 
28 // std includes
29 #include <vector>
30 
31 // BIAS includes
32 #include <Base/Image/Image.hh>
33 #include <Base/Math/Vector2.hh>
34 #include "ContourDetectorBase.hh"
35 
36 namespace BIAS {
37 
38  /**
39  @class ContourDetectorSimple
40  @brief a class for calculating the contour of a segmented region
41  @author Dennis Herzog, Jan. 2005 ischiller 08/08
42  @ingroup g_feature
43  */
44  template <class StorageType>
45  class BIASFeatureDetector_EXPORT ContourDetectorSimple :
46  ContourDetectorBase<StorageType>
47  {
48  public:
49 
52 
53  /**
54  * @brief detect function of ContourDetectorSimple
55  * @param image (input) an image with a constant background
56  * and an object of which the contour is to be calculated
57  * @param contour (output) the contours of the detected objects,
58  * currently only one contour is searched
59  * @return <0=error, >0 length of contour in pixel
60  * @author ischiller
61  * @date 12/09
62  */
63  int Detect(Image<StorageType>& image,
64  std::vector<BIAS::BIASContour>& contour);
65 
66  /** @brief returns a Borgefor distance transformed image (returned in 'dist_img')
67  of the pixel set given by the contour pixels in 'contour'
68  @author herzog
69  @param dist_im (output) the Borgefor distance image
70  @param contour (input) the already detected contour by a call to Detect(...)
71  */
72  void CreateDistanceImage(BIAS::Image<StorageType>& dist_img,
73  const BIASContour& contour);
74 
75  /** @brief returns a Borgefor distance transformed image of the contour pixel
76  set of the first blob found in the image 'img'.
77  @author herzog
78  @param img (input) source image, from which the contour comes
79  @param dist_img (output) returns the distance transformed image of the contour
80  @return <0=error, > 0 length of contour in pixel
81  */
82  int CreateDistanceImageByContour(BIAS::Image<StorageType>& dist_img,
84 
85  /** @brief draws the contour in the given image as white line
86  @author ischiller
87  @param image (input) image in which to draw the contour. Has to be initialized with the correct size
88  @return false=no contour available, true=drawing successful
89  */
90  bool GetContourImage(BIAS::Image<unsigned char>& image, const std::vector<BIASContour>& contour);
91 
92  /** @brief instead of contour pixels
93  a start point and a list of difference vector (created during
94  surrounding the blob clock wise) is given
95  @author herzog
96  @return directionList difference vectors coded in FreeManCode
97  (that means: (
98  0 = step right --
99  1 = step right & step down --
100  2 = step down --
101  3 = step left & step down --
102  4 = step left --
103  5 = step left & up
104  6 = step up --
105  7 = step right & step up))
106  */
107  inline std::vector<int>& GetFreemanCode() { return directionList_;}
108 
109  /**
110  @brief Set the background color if it is different from 0
111  @param backgroundColor (input) the new background color
112  */
113  inline void SetBackground(StorageType backgroundColor)
114  { background_=backgroundColor;}
115 
116  /**
117  * @brief Set the maximum gap which is bridged in search for contour
118  * @param gap (input) the new max gap (default 5)
119  */
120  inline void SetGap(int gap=5){
121  gap_ = gap;
122  }
123 
124  protected:
125  /** @brief creates the contour of the first blob found in the image;
126  * a pixel is assumed as foreground pixel, if the pixel value is != 0;
127  * this function works even if the blob is located at the image frame
128  * @author herzog
129  * @param contour
130  * @return true=ok, false=no contour detected
131  */
132  bool CreateContourBase_(BIAS::BIASContour &contour, BIAS::Image<StorageType> &img);
133 
134  /**
135  * @brief Initializes the distance image
136  */
137  void InitDistanceImage_(BIAS::Image<StorageType>& img,const BIASContour& contour);
138 
139  /** converts the given image to an distance transformed image (the Borgefor
140  distance is used, that means one step in diagonal direction has a
141  distance of 4 other steps have distances of 3);
142  the underlying pixel set has to be marked as zero valued pixels --
143  all other pixels have to be set to a high value (MAX_USHORT)
144  @author herzog
145  @param img input and output image
146  */
147  void MakeDistanceImage_(BIAS::Image<StorageType>& img);
148 
149 
150  /**
151  * @brief determines of a pixel is foreground or not
152  * @param x (input) x position of pixel
153  * @param y (input) y position of pixel
154  * @return true=is foreground, false=not foreground
155  */
156  inline bool IsFg_(int x, int y) {
157  StorageType pixel = data_[y][x];
158  return !( pixel == background_ );
159  }
160 
161  /**
162  * @brief returns IsFg_ of the next pixel in the direction of param direction
163  * @param x (input) x position of pixel
164  * @param y (input) y position of pixel
165  * @param direction (input) direction of next pixel (freeman code)
166  * @return true=is foreground, false=not foreground
167  */
168  inline bool IsFg_(int x, int y, int direction) {
169  switch ( (direction+16)%8 ) {
170  case 0: return IsFg_(x+1,y+0);
171  case 2: return IsFg_(x+0,y+1);
172  case 4: return IsFg_(x-1,y+0);
173  case 6: return IsFg_(x+0,y-1);
174  case 1: return IsFg_(x+1,y+1);
175  case 3: return IsFg_(x-1,y+1);
176  case 5: return IsFg_(x-1,y-1);
177  case 7: return IsFg_(x+1,y-1);
178  }
179  //BIASERR("IsFg: error");
180  return false;
181  }
182 
183 
184  /**
185  * @brief Finds first foreground pixel
186  * @param pos_x (output) x position of pixel
187  * @param pos_y (output) y position of pixel
188  * @return true=found, false=not found
189  */
190  bool FindFirst_(int &pos_x, int &pos_y);
191  void Init_(BIAS::Image<StorageType> &img);
192 
193  int img_w;
194  int img_h;
195  StorageType background_;
196  std::vector<int> directionList_;
197  StorageType **data_;
198  int gap_;
199  };//class
200 
201 
202  template <class StorageType>
203  class BIASFeatureDetector_EXPORT DistImgMatAcc{
204  public:
205 
208  virtual ~DistImgMatAcc(){};
209 
210  StorageType Get(int row, int col);
211  void Set(int row, int col, StorageType value);
212 
213  protected:
214  StorageType **data;
215 
216  };
217 
218 } // namespace
219 
220 #endif //__CONTOURDETECTORSIMPLE_HH__
T * Get(SharedPtr< T > &t)
This class describes a contour using the freemancode.
std::vector< int > & GetFreemanCode()
instead of contour pixels a start point and a list of difference vector (created during surrounding t...
a class for calculating the contour of a segmented region
bool IsFg_(int x, int y)
determines of a pixel is foreground or not
The image template class for specific storage types.
Definition: Image.hh:78
void SetBackground(StorageType backgroundColor)
Set the background color if it is different from 0.
bool IsFg_(int x, int y, int direction)
returns IsFg_ of the next pixel in the direction of param direction
void SetGap(int gap=5)
Set the maximum gap which is bridged in search for contour.
purely virtual interface defining class for contour detectors