00001
00024 #ifndef __D_CV_DRAWING__
00025 #define __D_CV_DRAWING__
00026
00027 #include <vector>
00028 #include <opencv/cv.h>
00029
00030 namespace DUtilsCV
00031 {
00032
00034 class Drawing
00035 {
00036 public:
00037
00039 class Plot
00040 {
00041 public:
00042
00044 struct Style
00045 {
00047 cv::Scalar color;
00049 int thickness;
00050
00054 Style(): color(cv::Scalar(0,0,0)), thickness(1) {}
00055
00061 Style(const cv::Scalar &_color, int _thickness = 1)
00062 : color(_color), thickness(_thickness){}
00063
00069 Style(int _thickness, const cv::Scalar &_color = cv::Scalar(0,0,0) )
00070 : color(_color), thickness(_thickness){}
00071
00077 Style(char c, int _thickness = 1);
00078
00084 Style(int _thickness, char c);
00085 };
00086
00087 public:
00088
00099 Plot(int rows = 240, int cols = 320,
00100 double minx = 0, double maxx = 1, double miny = 0, double maxy = 1,
00101 int margin = 0);
00102
00103 ~Plot(){}
00104
00114 void create(int rows, int cols,
00115 double minx, double maxx, double miny, double maxy);
00116
00127 void create(int rows, int cols,
00128 double minx, double maxx, double miny, double maxy, int margin);
00129
00137 void create(double minx, double maxx, double miny, double maxy);
00138
00147 void create(double minx, double maxx, double miny, double maxy, int margin);
00148
00153 inline void setBackgroundColor(const cv::Scalar &color);
00154
00158 inline void clear();
00159
00164 inline cv::Mat& getImage();
00165
00170 inline const cv::Mat& getImage() const;
00171
00180 void line(double x1, double y1, double x2, double y2,
00181 const Style &style = Style());
00182
00189 template<class T>
00190 void polyline(const std::vector<T> &x, const std::vector<T> &y,
00191 const Style &style = Style());
00192
00193 protected:
00194
00203 void setAxes(double minx, double maxx, double miny, double maxy,
00204 int margin);
00205
00211 inline int toPxX(double x) const;
00212
00218 inline int toPxY(double y) const;
00219
00220 protected:
00221
00223 cv::Scalar m_bg;
00224
00226 int m_margin;
00227
00229 cv::Mat m_canvas;
00230
00232 double m_cx, m_cy, m_uppx, m_uppy;
00233 };
00234
00235 public:
00236
00249 static void drawKeyPoints(cv::Mat &image,
00250 const std::vector<cv::KeyPoint> &keypoints,
00251 bool colorOctave = false,
00252 bool useCartesianAngle = false);
00253
00260 static void saveKeyPointImage(const std::string &filename,
00261 const cv::Mat &image, const std::vector<cv::KeyPoint> &keypoints);
00262
00273 static void drawCorrespondences(cv::Mat &image, const cv::Mat &im1,
00274 const cv::Mat &im2, const std::vector<cv::KeyPoint> &kp1,
00275 const std::vector<cv::KeyPoint> &kp2,
00276 const std::vector<int> &c1, const std::vector<int> &c2);
00277
00288 static void saveCorrespondenceImage(const std::string &filename,
00289 const cv::Mat &im1,
00290 const cv::Mat &im2, const std::vector<cv::KeyPoint> &kp1,
00291 const std::vector<cv::KeyPoint> &kp2,
00292 const std::vector<int> &c1, const std::vector<int> &c2);
00293
00303 static void drawReferenceSystem(cv::Mat &image, const cv::Mat &cTo,
00304 const cv::Mat &A, const cv::Mat &K = cv::Mat(),
00305 float length = 0.1);
00306
00317 static void drawReferenceSystem(cv::Mat &image, const cv::Mat &cRo,
00318 const cv::Mat &cto, const cv::Mat &A, const cv::Mat &K = cv::Mat(),
00319 float length = 0.1);
00320
00333 static void drawBox(cv::Mat &image, const cv::Mat &cRo,
00334 const cv::Mat &cto, float width, float height,
00335 const cv::Mat &A, const cv::Mat &K = cv::Mat(),
00336 std::vector<cv::Point2f> *box = NULL,
00337 const Plot::Style &style = Plot::Style('r', 2));
00338
00348 static void drawBox(cv::Mat &image, const cv::Mat &sHb, int cols, int rows,
00349 std::vector<cv::Point2f> *box = NULL,
00350 const Plot::Style &style = Plot::Style('r', 2));
00351
00361 template<class T>
00362 static void plot(cv::Mat &image, const std::vector<T>& x,
00363 const std::vector<T>& y, int thickness = 1,
00364 const cv::Scalar& color = cv::Scalar(0),
00365 const cv::Scalar& bgcolor = cv::Scalar(255,255,255));
00366
00367
00368
00369
00370
00371 };
00372
00373
00374
00375 inline void Drawing::Plot::setBackgroundColor(const cv::Scalar &color)
00376 {
00377 m_bg = color;
00378 }
00379
00380
00381
00382 inline void Drawing::Plot::clear()
00383 {
00384 m_canvas = m_bg;
00385 }
00386
00387
00388
00389 inline const cv::Mat& Drawing::Plot::getImage() const
00390 {
00391 return m_canvas;
00392 }
00393
00394
00395
00396 inline cv::Mat& Drawing::Plot::getImage()
00397 {
00398 return m_canvas;
00399 }
00400
00401
00402
00403 inline int Drawing::Plot::toPxX(double x) const
00404 {
00405 return((int)( (x - m_cx) / m_uppx + m_canvas.cols/2 ));
00406 }
00407
00408
00409
00410 inline int Drawing::Plot::toPxY(double y) const
00411 {
00412 return((int)( (y - m_cy) / m_uppy + m_canvas.rows/2 ));
00413 }
00414
00415
00416
00417 template<class T>
00418 void Drawing::Plot::polyline(const std::vector<T>& x,
00419 const std::vector<T>& y, const Drawing::Plot::Style &style)
00420 {
00421 if(x.empty() || y.empty())
00422 return;
00423
00424 const unsigned int N = (x.size() < y.size() ? x.size() : y.size());
00425
00426 if(N > 1)
00427 {
00428
00429 cv::Point a(toPxX(x[0]), toPxY(y[0]));
00430 for(unsigned int i = 1; i < N; ++i)
00431 {
00432 cv::Point b(toPxX(x[i]), toPxY(y[i]));
00433 cv::line(m_canvas, a, b, style.color, style.thickness);
00434 a = b;
00435 }
00436 }
00437 else
00438 {
00439
00440 cv::circle(m_canvas, cv::Point(toPxX(x[0]), toPxY(y[0])), 1,
00441 style.color, style.thickness);
00442 }
00443 }
00444
00445
00446
00447 }
00448
00449 #endif