Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
BlobDetectorLevelSet.cpp
1 #include "BlobDetectorLevelSet.hh"
2 
3 // std include
4 #include <iostream>
5 #include <vector>
6 
7 using namespace BIAS;
8 using namespace std;
9 
10 template <class StorageType>
13  image_ = NULL;
14  level_ = new BIAS::Image<StorageType>;
15  level_s_ = new BIAS::Image<StorageType>;
16  eval_ownsdata_ = true;
17 }
18 
19 template <class StorageType>
22  if ( eval_ownsdata_ == false )
23  eval_.RedirectImageDataPointer(NULL);
24 
25  delete level_;
26  delete level_s_;
27 }
28 
29 
30 
31 template <class StorageType> int
34  std::vector<BIAS::BIASBlob>& blobs){
35 
36  Init(image, image.GetROI()->MaskValid() );
37  Evolute();
38  GetSegmentation(image);
39  BIASWARN("Implementation unfinished, see ExampleBlobDetectorLevelSet for usage instructions!");
40  return 0;
41 }
42 
43 
44 template <class StorageType> void
47 {
48  const int w = GetImage().GetWidth();
49  const int h = GetImage().GetHeight();
50 
51  Resize(im, w,h);
52 
53  const StorageType ** image = GetImage().GetImageDataArray();
54  const StorageType ** level = GetLevel().GetImageDataArray();
55  StorageType ** dst = im.GetImageDataArray();
56 
57  for (int j = 0; j < h; j++)
58  for (int k = 0; k < w; k++)
59  dst[j][k] = level[j][k]==0 ? image[j][k] : 255-1;
60 }
61 
62 template <class StorageType> std::vector<int>
64 GetHistogram(int hist) {
65  RecalcHistograms();
66 
67  vector<int> vec;
68  for (int i = 0; i < 256; i++)
69  vec.push_back(hist==0 ? _hist0[i]:_hist1[i]);
70  return vec;
71 }
72 
73 
74 template <class StorageType> void
76 Resize(BIAS::Image<StorageType> &image, const int w, const int h)
77 {
78  if ( image.IsEmpty() )
79  image.Init(w,h);
80  else
81  if ( int(image.GetWidth())!=w || int(image.GetHeight())!=h ||
82  image.GetChannelCount()!=1 ) {
83  image.Release();
84  image.Init(w,h);
85  }
86 }
87 
88 template <class StorageType> void
90 ClearHistogram(int hist[]) {
91  for(int i = 0; i < 256; i++)
92  hist[i] = 0;
93 }
94 
95 template <class StorageType> void
98  ClearHistogram( _hist0 );
99  ClearHistogram( _hist1 );
100 }
101 
102 template <class StorageType> void
105 {
106  const int w = eval_.GetWidth();
107  const int h = eval_.GetHeight();
108 
109  ClearHistograms();
110 
111  const StorageType ** image = image_->GetImageDataArray();
112  StorageType ** level = level_->GetImageDataArray();
113  StorageType ** eval = eval_.GetImageDataArray();
114 
115  for(int j = 0; j < h; j++)
116  for(int k = 0; k < w; k++)
117  if ( eval[j][k]!=0 ) {
118  const StorageType pv = image[j][k];
119 
120  if ( level[j][k]==0 )
121  _hist0[pv]++;
122  else
123  _hist1[pv]++;
124  }
125 }
126 
127 template <class StorageType> void
130  const BIAS::Vector2<int>& p1,
131  const BIAS::Vector2<int>& p2)
132 {
133  const int x1 = p1[0];
134  const int y1 = p1[1];
135  const int x2 = p2[0];
136  const int y2 = p2[1];
137 
138 #ifdef BIAS_DEBUG
139  { // scope
140  const int h = image.GetHeight();
141  const int w = image.GetWidth();
142  Vector2<int> vDim(w,h);
143  BIASASSERT( p1>=Vector2<int>(0,0) && p1<=p2 && p2<=vDim );
144  }
145 #endif //BIAS_DEBUG
146 
147  StorageType ** data = image.GetImageDataArray();
148 
149  for (int j = y1; j < y2; j++)
150  for (int k = x1; k < x2; k++)
151  data[j][k] = 255;
152 }
153 
154 template <class StorageType> void
156 Set(const BIAS::Image<StorageType> &image, bool useROI)
157 {
158  image_ = &image;
159 
160  const int w = image.GetWidth();
161  const int h = image.GetHeight();
162 
163  BIASASSERT( w==(int)eval_.GetWidth() && h==(int)eval_.GetHeight() );
164 
165  if ( image.GetROI()->MaskValid() && useROI ) { // use image's roi msk
166  if ( eval_ownsdata_ == true )
167  eval_.ReleaseImageDataPointer();
168  //HACK!!!
169  BIAS::ImageBase img;
170  image_->GetROI()->GetMaskImage(img);
171  eval_.RedirectImageDataPointer( (void*)img.GetImageData());
172  eval_ownsdata_ = false;
173  }
174  else { // use own msk
175  if ( eval_ownsdata_ == false ) {
176  void * data = new char[w*h];
177  eval_.RedirectImageDataPointer( data );
178  eval_ownsdata_ = true;
179  }
180  }
181 }
182 
183 template <class StorageType> void
185 Init(const BIAS::Image<StorageType> &image, bool useROI)
186 {
187  // dimension of all image data
188  const int w = image.GetWidth();
189  const int h = image.GetHeight();
190 
191  // setup images
192  Resize( *level_, w,h );
193  Resize( *level_s_, w,h );
194  Resize( eval_, w,h );
195  Set(image, useROI);
196 
197  // clear border of level_/level_s_
198  StorageType ** level = level_->GetImageDataArray();
199  StorageType ** level_s = level_s_->GetImageDataArray();
200  { for (int k = 0; k < w; k += w-1)
201  for (int j = 0; j < h; j++)
202  level[j][k] = level_s[j][k] = 0; }
203  { for (int j = 0; j < h; j += h-1)
204  for (int k = 0; k < w; k++)
205  level[j][k] = level_s[j][k] = 0; }
206 }
207 
208 
209 template <class StorageType> void
212  const BIAS::Vector2<int>& p2)
213 {
214  level_->Clear();
215  AddSquare( *level_, p1,p2 );
216 }
217 
218 template <class StorageType> void
221  const BIAS::Vector2<int>& p2)
222 {
223  BIASASSERT( eval_ownsdata_ == true );
224 
225  eval_.Clear();
226  AddSquare( eval_, p1,p2 );
227 }
228 
229 template <class StorageType> void
232  const BIAS::Vector2<int>& p2)
233 {
234  BIASASSERT( eval_ownsdata_ == true );
235 
236  AddSquare( eval_, p1,p2 );
237 }
238 
239 
240 template <class StorageType> void
243 {
244  RecalcHistograms();
245 
246  const int h = level_->GetHeight();
247  const int w = level_->GetWidth();
248 
249  *level_s_ = *level_;
250 
251  const StorageType ** image = image_->GetImageDataArray();
252  StorageType ** level = level_->GetImageDataArray();
253  StorageType ** level_s = level_s_->GetImageDataArray();
254  StorageType ** eval = eval_.GetImageDataArray();
255 
256  // search for 3x3 neighbor
257  for(int j = 1; j<h-1; j++)
258  for (int k = 1; k<w-1; k++)
259  if ( eval[j][k]!=0 ) {
260  const int val = 0;
261  if ( level[j][k] == val )
262  if ( level[j-1][k+1] != val ||
263  level[j-1][k+0] != val ||
264  level[j-1][k-1] != val ||
265  level[j+0][k+1] != val ||
266  level[j+0][k-1] != val ||
267  level[j+1][k-1] != val ||
268  level[j+1][k+0] != val ||
269  level[j+1][k+1] != val )
270  {
271  const StorageType grey = image[j][k];
272  if ( _hist0[grey] < _hist1[grey] )
273  level_s[j][k] = 1;
274  }
275  if ( level[j][k] != val )
276  if ( level[j-1][k+1] == val ||
277  level[j-1][k+0] == val ||
278  level[j-1][k-1] == val ||
279  level[j+0][k+1] == val ||
280  level[j+0][k-1] == val ||
281  level[j+1][k-1] == val ||
282  level[j+1][k+0] == val ||
283  level[j+1][k+1] == val )
284  {
285  const StorageType grey = image[j][k];
286  if ( _hist1[grey] < _hist0[grey] )
287  level_s[j][k] = 0;
288  }
289  }
290 
291  // swap level set and init histograms
292  BIAS::Image<StorageType>* buff = level_;
293  level_ = level_s_;
294  level_s_ = buff;
295 }
296 
297 //////////////////////////////////////////////////////////////////////////
298 // instantiation
299 //////////////////////////////////////////////////////////////////////////
300 namespace BIAS{
302 //template class BlobDetectorLevelSet<float>;
303 
304 // fill in instances as required
305 #ifdef BUILD_IMAGE_INT
306 template class BlobDetectorLevelSet<int>;
307 #endif
308 #ifdef BUILD_IMAGE_CHAR
309 //template class BlobDetectorLevelSet<char>;
310 #endif
311 #ifdef BUILD_IMAGE_SHORT
312 #endif
313 #ifdef BUILD_IMAGE_USHORT
315 #endif
316 #ifdef BUILD_IMAGE_UINT
317 #endif
318 #ifdef BUILD_IMAGE_DOUBLE
319 #endif
320 }
void Release()
reimplemented from ImageBase
Definition: Image.cpp:1579
void GetSegmentation(BIAS::Image< StorageType > &im) const
creates an image for visulalistion of the actual segmentation given by actual level set ...
void Set(const BIAS::Image< StorageType > &image, bool useROI=true)
sets image to operate on, note: image must not be modified until you delete this instance or an other...
void SetEvaluationSquare(const BIAS::Vector2< int > &p1, const BIAS::Vector2< int > &p2)
can be used only, iff image&#39;s roi is not in use (look at class discription)
bool IsEmpty() const
check if ImageData_ points to allocated image buffer or not
Definition: ImageBase.hh:245
int Detect(Image< StorageType > &image, std::vector< BIAS::BIASBlob > &blobs)
purely virtual interface definition
void Evolute()
does the hole segmentation work by modifying the level set (look at class discription) ...
void AddSquare(BIAS::Image< StorageType > &image, const BIAS::Vector2< int > &p1, const BIAS::Vector2< int > &p2)
unsigned int GetWidth() const
Definition: ImageBase.hh:312
const void * GetImageData() const
Definition: ImageBase.hh:280
void AddEvaluationSquare(const BIAS::Vector2< int > &p1, const BIAS::Vector2< int > &p2)
look at SetEvaluationSquare()
static void Resize(BIAS::Image< StorageType > &image, const int w, const int h)
ROI * GetROI()
Returns a pointer to the roi object.
Definition: ImageBase.hh:615
Evaluates a segementation into fore/back-ground.
void Init(const BIAS::Image< StorageType > &image, bool useROI=true)
initialise this instance with given image, note: image must not be modified until you delete this ins...
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
Definition: ImageBase.hh:382
unsigned int GetHeight() const
Definition: ImageBase.hh:319
std::vector< int > GetHistogram(int hist)
The image template class for specific storage types.
Definition: Image.hh:78
void Init(unsigned int Width, unsigned int Height, unsigned int channels=1, enum EStorageType storageType=ST_unsignedchar, const bool interleaved=true)
calls Init from ImageBase storageType is ignored, just dummy argument
Definition: Image.cpp:421
bool MaskValid() const
is the position vector valid?
Definition: ROI.hh:307
This is the base class for images in BIAS.
Definition: ImageBase.hh:102
void InitLevelSetSquare(const BIAS::Vector2< int > &p1, const BIAS::Vector2< int > &p2)
look at class discription
int GetMaskImage(ImageBase &im) const
returns an image of StorageType unsigned char, where every pixel not in the ROI is set to UCHAR_MAX a...
Definition: ROI.cpp:369
static void ClearHistogram(int hist[])
const StorageType ** GetImageDataArray() const
overloaded GetImageDataArray() from ImageBase
Definition: Image.hh:153