Fawkes API  Fawkes Development Version
mirror_calib.h
1 
2 /***************************************************************************
3  * mirror_calib.h - Mirror calibration tool
4  *
5  * Created: Fri Dec 07 18:34:50 2007
6  * Copyright 2007 Daniel Beck
7  * Copyright 2009 Christoph Schwering
8  *
9  ****************************************************************************/
10 
11 /* This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL file in the doc directory.
22  */
23 
24 #ifndef _FIREVISION_MODELS_MIRROR_MIRROR_CALIB_H_
25 #define _FIREVISION_MODELS_MIRROR_MIRROR_CALIB_H_
26 
27 #if !defined(HAVE_IPP) and !defined(HAVE_OPENCV)
28 # error "Neither IPP nor OpenCV are installed."
29 #endif
30 
31 #include <fvmodels/mirror/bulb.h>
32 #include <fvutils/base/types.h>
33 #include <utils/math/angle.h>
34 
35 #include <cassert>
36 #include <iostream>
37 #include <map>
38 #include <vector>
39 
40 namespace firevision {
41 
43 {
44 public:
45  static void draw_line(unsigned char *yuv_buffer,
46  double angle_deg,
47  int center_x,
48  int center_y,
49  int width,
50  int height);
51  void draw_mark_lines(unsigned char *yuv_buffer);
52  static void
53  draw_crosshair(unsigned char *yuv_buffer, int center_x, int center_y, int width, int height);
54 
57 
58  void load_mask(const char *mask_file_name);
59  void push_back(const unsigned char *yuv_buffer, size_t buflen, int width, int height, double ori);
60  void abort();
61  void next_step();
62  const unsigned char *get_last_yuv_buffer() const;
63  const char * get_state_description() const;
64 
65  /** Sets preliminary center point.
66  * @param x X-coordinate
67  * @param y Y-coordinate */
68  inline void
69  set_center(int x, int y)
70  {
71  img_center_x_ = x;
72  img_center_y_ = y;
73  }
74 
75  /** Center X accessor.
76  * @return center X value */
77  inline int
78  center_x() const
79  {
80  return img_center_x_;
81  }
82  /** Center Y accessor.
83  * @return center Y value */
84  inline int
85  center_y() const
86  {
87  return img_center_y_;
88  }
89 
90  void eval(unsigned int x, unsigned int y, float *x_ret, float *y_ret);
91 
92  void load(const char *filename);
93  void save(const char *filename);
94 
95 private:
96  class ConvexPolygon;
97  class StepResult;
98  typedef std::vector<StepResult> StepResultList;
99  class Point;
100  class PixelPoint;
101  class CartesianPoint;
102  class CartesianImage;
103  class Hole;
104  class Image;
105  typedef std::vector<Hole> HoleList;
106  typedef double PolarAngle;
107  typedef int PolarRadius;
108  typedef int RealDistance;
109  typedef std::vector<PolarRadius> MarkList;
110  typedef std::map<PolarAngle, MarkList> MarkMap;
111  typedef std::pair<PolarAngle, PolarAngle> PolarAnglePair;
112  typedef std::vector<Image> ImageList;
113 
114  class ConvexPolygon : public std::vector<PixelPoint>
115  {
116  public:
117  ConvexPolygon();
118  bool contains(const CartesianImage &img, const CartesianPoint &r) const;
119  bool contains(const PixelPoint &r) const;
120  };
121 
122  enum StepName {
123  SHARPENING,
124  EDGE_DETECTION,
125  COMBINATION,
126  CENTERING,
127  PRE_MARKING,
128  FINAL_MARKING,
129  DONE
130  };
131  /// @cond INTERNALS
132  struct CalibrationState
133  {
134  StepName step;
135  ImageList::size_type image_index;
136  bool centering_done;
137  CalibrationState() : step(SHARPENING), image_index(0), centering_done(false){};
138  };
139  /// @endcond
140 
141  void goto_next_state();
142  void set_last_yuv_buffer(const unsigned char *last_buf);
143  void draw_center(StepResult &result);
144 
145  static PolarAngle relativeOrientationToImageRotation(PolarAngle ori);
146  static PolarAngle imageRotationToRelativeOrientation(PolarAngle ori);
147 
148  static void
149  apply_sobel(unsigned char *src, unsigned char *dst, int widt, int height, orientation_t ori);
150  static void apply_sharpen(unsigned char *src, unsigned char *dst, int widt, int height);
151  static void apply_median(unsigned char *src, unsigned char *dst, int widt, int height, int i);
152  static void apply_min(unsigned char *src, unsigned char *dst, int widt, int height);
153  static void
154  apply_or(unsigned char *src1, unsigned char *src2, unsigned char *dst, int widt, int height);
155  static void make_contrast(unsigned char *buf, size_t buflen);
156  static void make_grayscale(unsigned char *buf, size_t buflen);
157  static MirrorCalibTool::MarkList premark(const StepResult & prev,
158  const unsigned char *yuv_mask,
159  StepResult & result,
160  PolarAngle phi,
161  const PixelPoint & center);
162  static MirrorCalibTool::MarkList premark(const ConvexPolygon &polygon,
163  const StepResult & prev,
164  const unsigned char *yuv_mask,
165  StepResult & result,
166  PolarAngle phi,
167  const PixelPoint & center);
168  static HoleList search_holes(const MarkList &premarks);
169  static HoleList filter_biggest_holes(const HoleList &holes, unsigned int n);
170  static MarkList determine_marks(const HoleList &holes);
171  static MarkList mark(const MarkList & premarks,
172  const unsigned char *yuv_mask,
173  StepResult & result,
174  PolarAngle phi,
175  const PixelPoint & center);
176 
177  static PixelPoint calculate_center(const ImageList &images);
178  static RealDistance calculate_real_distance(int n);
179  static PolarAnglePair find_nearest_neighbors(PolarAngle angle, const MarkMap &mark_map);
180  static RealDistance interpolate(PolarRadius radius, const MarkList &marks);
181  static Bulb generate(int width, int height, const PixelPoint &center, const MarkMap &mark_map);
182 
183  unsigned char *img_yuv_buffer_;
184  int img_center_x_;
185  int img_center_y_;
186  unsigned char *img_yuv_mask_;
187 
188  ImageList source_images_;
189  CalibrationState state_;
190  MarkList premarks_;
191  MarkMap mark_map_; /** orientations wrt robot (i.e. Y axis) */
192 
193  const unsigned char *last_yuv_buffer_;
194 
195  Bulb *bulb_;
196 };
197 
198 } // namespace firevision
199 
200 #endif /* FIREVISION_MODELS_MIRROR_MIRROR_CALIB_H__ */
This class encapsulates the routines necessary for interactive mirror calibration.
Definition: mirror_calib.h:43
void push_back(const unsigned char *yuv_buffer, size_t buflen, int width, int height, double ori)
Store image for calibration process.
void draw_mark_lines(unsigned char *yuv_buffer)
Draws a crosshair with the lines in the directions of the keys of the mark map.
const unsigned char * get_last_yuv_buffer() const
Get last created YUV buffer.
void save(const char *filename)
Saves calibration data to a file.
void eval(unsigned int x, unsigned int y, float *x_ret, float *y_ret)
Get the assumed distance and orientation of a pixel point.
void next_step()
Performs one step in the calibration process.
static void draw_crosshair(unsigned char *yuv_buffer, int center_x, int center_y, int width, int height)
Draws a crosshair in the YUV-buffer.
void load(const char *filename)
Loads a calibration file.
void abort()
Aborts the calibration process.
void set_center(int x, int y)
Sets preliminary center point.
Definition: mirror_calib.h:69
static void draw_line(unsigned char *yuv_buffer, double angle_deg, int center_x, int center_y, int width, int height)
Draws a line from the image center in the given angle.
int center_y() const
Center Y accessor.
Definition: mirror_calib.h:85
int center_x() const
Center X accessor.
Definition: mirror_calib.h:78
void load_mask(const char *mask_file_name)
Loads a PNM mask file for the robot's bars.
const char * get_state_description() const
Get description of next step.