Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
DistTransform.cpp
1 #include "DistTransform.hh"
2 
3 //BIAS header
4 #include "Base/Image/ImageConvert.hh"
5 
6 // std includes
7 #include <limits>
8 
9 using namespace std;
10 using namespace BIAS;
11 
12 
13 template <class InputStorageType, class OutputStorageType>
16  : FilterNToN<InputStorageType, OutputStorageType>()
17 {
18  zeroLevel_ = 0;
19 }
20 
21 template <class InputStorageType, class OutputStorageType>
24  : FilterNToN<InputStorageType, OutputStorageType>(other),
25  zeroLevel_(other.zeroLevel_)
26 {
27 }
28 
29 template <class InputStorageType, class OutputStorageType>
30 void
33 {
34  OutputStorageType *data = img.GetImageData();
35  int pixels = img.GetPixelCount();
36  OutputStorageType maxVal=
37  OutputStorageType(OutputStorageType(img.GetWidth()+img.GetHeight())/
38  2.0*9.0);
39 
40  // initiate with max possible distance infinity
41  for (int i = 0; i < pixels; i++)
42  if (data[i]==zeroLevel_) data[i] = 0;
43  else data[i] = maxVal;
44 }
45 
46 
47 template <class InputStorageType, class OutputStorageType>
50 {
51 #define mymin(a,b) ((a<=b)?a:b)
52 #define minDEF(s0,s1,s2,s3,s4) mymin(s0, mymin(s1, mymin(s2, mymin(s3, s4))))
53 
54  OutputStorageType **data=img.GetImageDataArray();
55 
56  int rows = img.GetHeight();
57  int cols = img.GetWidth();
58 
59  // forward pass
60  for (int k = 1; k <= rows-1; k++)
61  for (int j = 1; j <= cols-1-1; j++)
62  data[k][j]= minDEF(data[k][j], // origin
63  OutputStorageType(data[k][j-1]+3),
64  OutputStorageType(data[k-1][j]+3), // left, top
65  OutputStorageType(data[k-1][j-1]+4),
66  OutputStorageType(data[k-1][j+1]+4) ); // top diags
67 
68  // backward pass
69  for (int k = rows-1-1; k >= 0; k--)
70  for (int j = cols-1-1; j >= 1; j--)
71  data[k][j]=
72  minDEF(data[k][j], // origin
73  OutputStorageType(data[k][j+1]+3),
74  OutputStorageType(data[k+1][j]+3), // right, bottom
75  OutputStorageType(data[k+1][j+1]+4),
76  OutputStorageType(data[k+1][j-1]+4) ); // bottom diags
77 }
78 
79 
80 template <class InputStorageType, class OutputStorageType>
84 {
85 #ifdef BIAS_DEBUG
86  if ( (dist_img.GetStorageType()==ImageBase::ST_unsignedchar) ) {
87  BIASERR("OutputType unsigned char is invalid for distance images, "
88  "use unsigned short or float!");
89  return -1;
90  }
91 #endif
92  ImageConvert::ConvertST(src_img,dist_img,dist_img.GetStorageType());
93  InitDistanceImage_(dist_img);
94  MakeDistanceImage_(dist_img);
95  return 0;
96 }
97 
98 template <class InputStorageType, class OutputStorageType>
101 {
102  BIASERR("FilterInt makes no sense here, using general Filter interface.");
103  return Filter(src, dst);
104 }
105 
106 template <class InputStorageType, class OutputStorageType>
109 {
110  BIASERR("FilterFloat makes no sense here, using general Filter interface.");
111  return Filter(src, dst);
112 }
113 
114 template <class InputStorageType, class OutputStorageType>
116 GetBordersValid_(int & /*border_x*/, int & /*border_y*/) const
117 {
118  BIASERR("Unfinished code");
119  BIASABORT;
120 }
121 
122 //////////////////////////////////////////////////////////////////////////
123 // instantiation
124 //////////////////////////////////////////////////////////////////////////
125 namespace BIAS{
126 #define FILTER_INSTANTIATION_CLASS DistTransform
127 #include "Filterinst.hh"
128 }
virtual int FilterFloat(const Image< InputStorageType > &src, Image< OutputStorageType > &dst)
virtual void GetBordersValid_(int &border_x, int &border_y) const
Class for calculating the distance transform.
unsigned int GetWidth() const
Definition: ImageBase.hh:312
static int ConvertST(const BIAS::ImageBase &source, BIAS::ImageBase &dest, ImageBase::EStorageType targetST)
Function to convert the storage type of images e.g.
base class for simple n-&gt;n filter implementations
Definition: FilterNToN.hh:43
unsigned int GetHeight() const
Definition: ImageBase.hh:319
const StorageType * GetImageData() const
overloaded GetImageData() from ImageBase
Definition: Image.hh:137
virtual int Filter(const Image< InputStorageType > &src, Image< OutputStorageType > &dst)
Calculates the distanceImage from source image src.
virtual int FilterInt(const Image< InputStorageType > &src, Image< OutputStorageType > &dst)
enum EStorageType GetStorageType() const
Definition: ImageBase.hh:414
(8bit) unsigned char image storage type
Definition: ImageBase.hh:112
InputStorageType zeroLevel_
unsigned long int GetPixelCount() const
returns number of pixels in image
Definition: ImageBase.hh:422
void MakeDistanceImage_(BIAS::Image< OutputStorageType > &img)
void InitDistanceImage_(BIAS::Image< OutputStorageType > &img)
const StorageType ** GetImageDataArray() const
overloaded GetImageDataArray() from ImageBase
Definition: Image.hh:153