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 <geometry/hom_point.h>
32 #include <utils/math/angle.h>
33 #include <fvutils/base/types.h>
34 
35 #include <fvmodels/mirror/bulb.h>
36 
37 #include <iostream>
38 #include <vector>
39 #include <map>
40 #include <cassert>
41 
42 namespace firevision {
43 
45 {
46  public:
47  static void draw_line(unsigned char* yuv_buffer, double angle_deg,
48  int center_x, int center_y, int width, int height);
49  void draw_mark_lines(unsigned char* yuv_buffer);
50  static void draw_crosshair(unsigned char* yuv_buffer, int center_x,
51  int center_y, int width, int height);
52 
55 
56  void load_mask(const char* mask_file_name);
57  void push_back(const unsigned char* yuv_buffer,
58  size_t buflen,
59  int width,
60  int height,
61  double ori);
62  void abort();
63  void next_step();
64  const unsigned char* get_last_yuv_buffer() const;
65  const char* get_state_description() const;
66 
67  /** Sets preliminary center point.
68  * @param x X-coordinate
69  * @param y Y-coordinate */
70  inline void set_center(int x, int y) {
71  img_center_x_ = x;
72  img_center_y_ = y;
73  }
74 
75  /** Center X accessor.
76  * @return center X value */
77  inline int center_x() const { return img_center_x_; }
78  /** Center Y accessor.
79  * @return center Y value */
80  inline int center_y() const { return img_center_y_; }
81 
82  void eval(unsigned int x,
83  unsigned int y,
84  float* x_ret,
85  float* y_ret);
86 
87  void load(const char* filename);
88  void save(const char* filename);
89 
90  private:
91  class ConvexPolygon;
92  class StepResult;
93  typedef std::vector<StepResult> StepResultList;
94  class Point;
95  class PixelPoint;
96  class CartesianPoint;
97  class CartesianImage;
98  class Hole;
99  class Image;
100  typedef std::vector<Hole> HoleList;
101  typedef double PolarAngle;
102  typedef int PolarRadius;
103  typedef int RealDistance;
104  typedef std::vector<PolarRadius> MarkList;
105  typedef std::map<PolarAngle, MarkList> MarkMap;
106  typedef std::pair<PolarAngle, PolarAngle> PolarAnglePair;
107  typedef std::vector<Image> ImageList;
108 
109  class ConvexPolygon : public std::vector<PixelPoint> {
110  public:
111  bool contains(const CartesianImage& img, const CartesianPoint& r) const;
112  bool contains(const PixelPoint& r) const;
113  };
114 
115  enum StepName { SHARPENING, EDGE_DETECTION, COMBINATION, CENTERING,
116  PRE_MARKING, FINAL_MARKING, DONE };
117  /// @cond INTERNALS
118  struct CalibrationState {
119  StepName step;
120  ImageList::size_type image_index;
121  bool centering_done;
122  CalibrationState()
123  : step(SHARPENING), image_index(0), centering_done(false) {};
124  };
125  /// @endcond
126 
127  void goto_next_state();
128  void set_last_yuv_buffer(const unsigned char* last_buf);
129  void draw_center(StepResult& result);
130 
131 
132  static PolarAngle relativeOrientationToImageRotation(PolarAngle ori);
133  static PolarAngle imageRotationToRelativeOrientation(PolarAngle ori);
134 
135  static void apply_sobel(unsigned char* src, unsigned char* dst,
136  int widt, int height,
137  orientation_t ori);
138  static void apply_sharpen(unsigned char* src, unsigned char* dst,
139  int widt, int height);
140  static void apply_median(unsigned char* src, unsigned char* dst,
141  int widt, int height, int i);
142  static void apply_min(unsigned char* src, unsigned char* dst,
143  int widt, int height);
144  static void apply_or(unsigned char* src1, unsigned char* src2,
145  unsigned char* dst, int widt, int height);
146  static void make_contrast(unsigned char* buf, size_t buflen);
147  static void make_grayscale(unsigned char* buf, size_t buflen);
148  static MirrorCalibTool::MarkList premark(const StepResult& prev, const unsigned char* yuv_mask,
149  StepResult& result, PolarAngle phi,
150  const PixelPoint& center);
151  static MirrorCalibTool::MarkList premark(const ConvexPolygon& polygon, const StepResult& prev,
152  const unsigned char* yuv_mask, StepResult& result,
153  PolarAngle phi, const PixelPoint& center);
154  static HoleList search_holes(const MarkList& premarks);
155  static HoleList filter_biggest_holes(const HoleList& holes, unsigned int n);
156  static MarkList determine_marks(const HoleList& holes);
157  static MarkList mark(const MarkList& premarks, const unsigned char* yuv_mask,
158  StepResult& result, PolarAngle phi,
159  const PixelPoint& center);
160 
161  static PixelPoint calculate_center(const ImageList& images);
162  static RealDistance calculate_real_distance(int n);
163  static PolarAnglePair find_nearest_neighbors(PolarAngle angle,
164  const MarkMap& mark_map);
165  static RealDistance interpolate(PolarRadius radius, const MarkList& marks);
166  static Bulb generate(int width, int height,
167  const PixelPoint& center,
168  const MarkMap& mark_map);
169 
170  unsigned char* img_yuv_buffer_;
171  int img_center_x_;
172  int img_center_y_;
173  unsigned char* img_yuv_mask_;
174 
175  ImageList source_images_;
176  CalibrationState state_;
177  MarkList premarks_;
178  MarkMap mark_map_; /** orientations wrt robot (i.e. Y axis) */
179 
180  const unsigned char* last_yuv_buffer_;
181 
182  Bulb* bulb_;
183 };
184 
185 }
186 
187 #endif /* __FIREVISION_MODELS_MIRROR_MIRROR_CALIB_H_ */
188 
void load_mask(const char *mask_file_name)
Loads a PNM mask file for the robot&#39;s bars.
int center_y() const
Center Y accessor.
Definition: mirror_calib.h:80
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.
The result of a step contains a YUV buffer.
void eval(unsigned int x, unsigned int y, float *x_ret, float *y_ret)
Get the assumed distance and orientation of a pixel point.
A container for a YUV-buffer etc.
A pixel point is a 2d point with positive X and Y coords.
A hole is a sequence of pixels between two lines.
void draw_mark_lines(unsigned char *yuv_buffer)
Draws a crosshair with the lines in the directions of the keys of the mark map.
Wraps an image so that access to (0, 0) is mapped to the middle of the image and so on...
void push_back(const unsigned char *yuv_buffer, size_t buflen, int width, int height, double ori)
Store image for calibration process.
void load(const char *filename)
Loads a calibration file.
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.
void save(const char *filename)
Saves calibration data to a file.
void abort()
Aborts the calibration process.
A cartesian point is a 2d point which can have negative X and Y coords.
Bulb mirror lookup table.
Definition: bulb.h:38
void set_center(int x, int y)
Sets preliminary center point.
Definition: mirror_calib.h:70
const unsigned char * get_last_yuv_buffer() const
Get last created YUV buffer.
This class encapsulates the routines necessary for interactive mirror calibration.
Definition: mirror_calib.h:44
void next_step()
Performs one step in the calibration process.
const char * get_state_description() const
Get description of next step.
int center_x() const
Center X accessor.
Definition: mirror_calib.h:77