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 <utils/math/angle.h>
32 #include <fvutils/base/types.h>
33 
34 #include <fvmodels/mirror/bulb.h>
35 
36 #include <iostream>
37 #include <vector>
38 #include <map>
39 #include <cassert>
40 
41 namespace firevision {
42 
44 {
45  public:
46  static void draw_line(unsigned char* yuv_buffer, double angle_deg,
47  int center_x, int center_y, int width, int height);
48  void draw_mark_lines(unsigned char* yuv_buffer);
49  static void draw_crosshair(unsigned char* yuv_buffer, int center_x,
50  int center_y, int width, int height);
51 
54 
55  void load_mask(const char* mask_file_name);
56  void push_back(const unsigned char* yuv_buffer,
57  size_t buflen,
58  int width,
59  int height,
60  double ori);
61  void abort();
62  void next_step();
63  const unsigned char* get_last_yuv_buffer() const;
64  const char* get_state_description() const;
65 
66  /** Sets preliminary center point.
67  * @param x X-coordinate
68  * @param y Y-coordinate */
69  inline void set_center(int x, int y) {
70  img_center_x_ = x;
71  img_center_y_ = y;
72  }
73 
74  /** Center X accessor.
75  * @return center X value */
76  inline int center_x() const { return img_center_x_; }
77  /** Center Y accessor.
78  * @return center Y value */
79  inline int center_y() const { return img_center_y_; }
80 
81  void eval(unsigned int x,
82  unsigned int y,
83  float* x_ret,
84  float* y_ret);
85 
86  void load(const char* filename);
87  void save(const char* filename);
88 
89  private:
90  class ConvexPolygon;
91  class StepResult;
92  typedef std::vector<StepResult> StepResultList;
93  class Point;
94  class PixelPoint;
95  class CartesianPoint;
96  class CartesianImage;
97  class Hole;
98  class Image;
99  typedef std::vector<Hole> HoleList;
100  typedef double PolarAngle;
101  typedef int PolarRadius;
102  typedef int RealDistance;
103  typedef std::vector<PolarRadius> MarkList;
104  typedef std::map<PolarAngle, MarkList> MarkMap;
105  typedef std::pair<PolarAngle, PolarAngle> PolarAnglePair;
106  typedef std::vector<Image> ImageList;
107 
108  class ConvexPolygon : public std::vector<PixelPoint> {
109  public:
110  ConvexPolygon();
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_x() const
Center X accessor.
Definition: mirror_calib.h:76
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.
const char * get_state_description() const
Get description of next step.
const unsigned char * get_last_yuv_buffer() const
Get last created 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.
int center_y() const
Center Y accessor.
Definition: mirror_calib.h:79
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:69
This class encapsulates the routines necessary for interactive mirror calibration.
Definition: mirror_calib.h:43
void next_step()
Performs one step in the calibration process.