Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleRefineContour.cpp
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  @example ExampleRefineContour.cpp
27  @relates ContourDetectorSimple
28  @brief Example Contour Refinement
29  Usage: ExampleRefineContour SegmentedImage ColorImage ContourImage
30  @ingroup g_examples
31  @author MIP
32 */
33 
34 #include <Filter/FilterNTo2N.hh>
35 #include <Filter/GradientSobel3x3.hh>
36 #include <Filter/GradientGauss.hh>
37 #include <Filter/GradientGaussAsymmetric.hh>
38 #include <Base/Image/ImageIO.hh>
39 #include <Base/Image/ImageConvert.hh>
40 #include <Base/Debug/TimeMeasure.hh>
41 #include <math.h>
42 #include <Base/ImageUtils/ImageDraw.hh>
43 #include "FeatureDetector/ContourDetectorSimple.hh"
44 #include <Base/Common/BIASpragma.hh>
45 
46 using namespace BIAS;
47 using namespace std;
48 
49 #define InputImageType unsigned char
50 #define OutputImageType float
51 
52 
53 int main(int argc, char *argv[])
54 {
55 
56  if(argc < 4){
57  cerr << "usage: "<< argv[0] << "SegmentedImage ColorImage ContourImage" << endl;
58  exit(0);
59  }
60 
61 
62  Image<float> segIm;
64  Image<OutputImageType> gx, gy, g;
65  Image<float>contourImg;
66 
67  // read images
68  ImageIO::Load(argv[1],segIm);
69  ImageIO::Load(argv[2],im);
70 
71  // first search contours of image
73  cont.SetBackground(0);
74  std::vector<BIASContour> contour;
75  cont.Detect(segIm, contour);
76 
77  // calculate gradients of image
82  //grad = &gsobel3x3;
83  grad = &ggauss;
84  grad->SetBorderHandling(FilterBase<InputImageType,
85  OutputImageType>::TBH_valid);
86 
87  gsobel3x3.Filter(im, gx, gy, g);
88  float** data = g.GetImageDataArray();
89 
90  // search refinements
91 
92  BIASContour ct = contour[0];
93  BIASContour ct_copy = contour[0];
94 
95  // die Contour ablaufen
96  int dSearchLength = 20;
97  double start_x, start_y, end_x, end_y;
98  BIAS::Vector2<double> pointS;
99  BIAS::Vector2<double> pointMiddle;
100  BIAS::Vector2<double> pointE;
101  for(unsigned int i = 0; i < ct.length; i++){
102  // read startpoint of line
103  pointS = ct.contourPixel[i];
104  start_x = pointS[0];
105  start_y = pointS[1];
106 
107  // read middle point (this one will maybe refined)
108  pointMiddle = ct.contourPixel[(i+2)%ct.length];
109  int offset = (i+4)%ct.length;
110 
111  // read endpoint of line
112  pointE = ct.contourPixel[offset];
113  end_x = pointE[0];
114  end_y = pointE[1];
115 
116  //freeman = ct.freemanCode[i];
117 
118  //double dx = end_x - start_x;
119  //double dy = end_y - start_y;
120 
121  // calc distance
122  double distance = sqrt(pow((end_x - start_x),2) +
123  pow((end_y - start_y),2));
124 
125 
126  double PvectorXone = (end_y - start_y);
127  double PvectorYone = -(end_x - start_x);
128 
129  double coefficient = dSearchLength / distance;
130  double normX = coefficient*PvectorXone;
131  //double normY = coefficient*PvectorYone;
132 
133  //cout << start_x << " , " << start_y << ", Normale: " << PvectorXone << "," << PvectorYone << endl;
134 
135  // die Normale nach aussen laufen
136  int x,y;
137  int fk = 1;
138  bool found = false;
139 
140  // search outside of the contour
141  while((normX+pointMiddle[0]) - (pointMiddle[0] + PvectorXone*fk) > 0 ){
142  x = pointMiddle[0] + PvectorXone*fk;
143  y = pointMiddle[1] + PvectorYone*fk;
144 
145  if(data[y][x] > 12.0){
146  // move point
147  ct_copy.contourPixel[i][0]=x;
148  ct_copy.contourPixel[i][1]=y;
149  found = true;
150  break;
151  }
152  fk++;
153  }
154  // search inside of the contour
155  fk=-1;
156  if(!found){
157  while((pointMiddle[0]-normX) - (pointMiddle[0] + PvectorXone*fk) > 0 ){
158  x = pointMiddle[0] + PvectorXone*fk;
159  y = pointMiddle[1] + PvectorYone*fk;
160  if(x >=0 && y >=0){
161  if(data[y][x] > 12.0){
162  // move point
163  ct_copy.contourPixel[i][0]=x;
164  ct_copy.contourPixel[i][1]=y;
165  found = true;
166  break;
167  }
168  }else{
169  break;
170  }
171  fk--;
172  }
173  }
174 
175  }
176 
177  contourImg.Init(segIm.GetWidth(), segIm.GetHeight(),3);
178 
179  // draw contour before refinement
180  for (unsigned int k=0;k<ct.length;k++){
181  contourImg.SetPixel(100,100,100,
182  (unsigned int)ct.contourPixel[k][0],
183  (unsigned int)ct.contourPixel[k][1]);
184 
185  }
186  // draw contour after refinement
187  for (unsigned int k=0;k<ct_copy.length;k++){
188  contourImg.SetPixel(255,255,255,
189  (unsigned int)ct_copy.contourPixel[k][0],
190  (unsigned int)ct_copy.contourPixel[k][1]);
191 
192  }
193 
194  // connect points of contour with lines
195  float col[3];
196  col[0] = 255;
197  col[1] = 255;
198  col[2] = 255;
199  for (unsigned int k=0;k<ct_copy.length-1;k++){
200  ImageDraw<float>::Line(contourImg,(unsigned int) ct_copy.contourPixel[k][0],
201  (unsigned int) ct_copy.contourPixel[k][1],
202  (unsigned int) ct_copy.contourPixel[k+1][0],
203  (unsigned int) ct_copy.contourPixel[k+1][1],
204  col,
205  1.0);
206  }
207 
208 
209  // save contour
210  ImageIO::Save(argv[3], contourImg);
211 
212 }
gradient calculation with separated gauss masks
This class describes a contour using the freemancode.
base class for simple n-&gt;2n filter implementations
Definition: FilterNTo2N.hh:44
std::vector< BIAS::Vector2< double > > contourPixel
All pixels contained in the contour.
virtual parent class for API definition of all (future) filters
Definition: FilterBase.hh:77
gradient calculation with sobel 3 by 3 masks
unsigned int GetWidth() const
Definition: ImageBase.hh:312
int Detect(Image< StorageType > &image, std::vector< BIAS::BIASContour > &contour)
detect function of ContourDetectorSimple
static int Line(Image< StorageType > &im, const unsigned int start[2], const unsigned int end[2], const StorageType value[])
lines
Definition: ImageDraw.cpp:404
a class for calculating the contour of a segmented region
unsigned int GetHeight() const
Definition: ImageBase.hh:319
void SetBorderHandling(const int bh)
Definition: FilterBase.hh:127
static int Save(const std::string &filename, const ImageBase &img, const enum TFileFormat FileFormat=FF_auto, const bool sync=BIAS_DEFAULT_SYNC, const int c_jpeg_quality=BIAS_DEFAULT_IMAGE_QUALITY, const bool forceNewID=BIAS_DEFAULT_FORCENEWID, const bool &writeMetaData=true)
Export image as file using extrnal libs.
Definition: ImageIO.cpp:725
void SetBackground(StorageType backgroundColor)
Set the background color if it is different from 0.
void SetPixel(const StorageType &value, const unsigned int &x, const unsigned int &y, const unsigned short int channel=0)
Set the value of a given pixel (x,y) in channel to value.
Definition: Image.hh:171
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
gradient calculation with separated gauss masks
static int Load(const std::string &FileName, ImageBase &img)
first tries a call to Read MIP image and if that fails, tries to Import Image with all other availabl...
Definition: ImageIO.cpp:141
virtual int Filter(const Image< InputStorageType > &src, Image< OutputStorageType > &grad)
returns a 2 channel image containing gx and gy
const StorageType ** GetImageDataArray() const
overloaded GetImageDataArray() from ImageBase
Definition: Image.hh:153