ellipses_draw.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2014 by Markus Bader *
3  * markus.bader@tuwien.ac.at *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
21 #include <opencv2/imgproc/imgproc.hpp>
22 #include <opencv2/highgui/highgui.hpp>
23 #include <opencv2/calib3d/calib3d.hpp>
26 #include <boost/foreach.hpp>
27 
28 using namespace tuw;
29 
31  std::vector<cv::Point2f> vtx(4);
32  std::vector<cv::Point2f> vtxDis;
33  cv::Scalar colourContour(255,0,255);
34  cv::Scalar colourEllipse(0,0,255);
35  char text[0xFF];
36  for(unsigned int i = 0; i < ellipses_.size(); i++) {
37  Ellipse &ellipse = ellipses_[i];
38  if(ellipse.detection != VALID) continue;
39  //cv::drawContours(img, contours_, (int) i, colourContour, 1, 8);
40  cv::RotatedRect box1, box2;
41  camera_.distort(ellipse.boxEllipse, box1);
42  cv::ellipse(img, box1, colourEllipse, 1, CV_AA);
43  camera_.distort(ellipses_[ellipse.innerRing].boxEllipse, box2);
44  if(ellipse.innerRing >= 0) cv::ellipse(img, box2, colourEllipse, 1, CV_AA);
45  box1.points(&vtx[0]);
46  for( int j = 0; j < 4; j++ ) {
47  cv::line(img, vtx[j], vtx[(j+2)%4], cv::Scalar(0,255,0), 1, CV_AA);
48  sprintf(text, "%i", j);
49  //cv::putText(img, text, vtx[j], cv::FONT_HERSHEY_PLAIN, 1, cv::Scalar::all(0), 1, CV_AA);
50  }
51  camera_.distort(*ellipse.contourUndistort, vtxDis);
52  BOOST_FOREACH(const cv::Point2f &p, vtxDis ) {
53  img.at<cv::Vec3b>(p) = cv::Vec3b(0,255,255);
54  }
55 
56  // draw normals into the image
57  if(ellipse.cone.projections[0] != cv::Point2d(0,0)) {
58  for(unsigned int k = 0; k < 2; k++) {
59  cv::Mat pi, pw(ellipse.cone.translations[k]);
60  cv::Mat pin , pn = pw + cv::Mat(ellipse.cone.normals[k])*param_->circle_diameter;
61  cv::projectPoints(pw.t(), cv::Mat_<double>::zeros(1,3), cv::Mat_<double>::zeros(1,3), camera_.cameraMatrix, camera_.distCoeffs, pi);
62  cv::projectPoints(pn.t(), cv::Mat_<double>::zeros(1,3), cv::Mat_<double>::zeros(1,3), camera_.cameraMatrix, camera_.distCoeffs, pin);
63  for(int j = 0; j < pi.rows; j++) {
64  cv::Point p0(pi.at<double>(j,0),pi.at<double>(j,1));
65  cv::circle(img, p0,2, cv::Scalar(125*j,125*j,255));
66  cv::Point p1(pin.at<double>(j,0),pin.at<double>(j,1));
67  cv::line(img, p0, p1, cv::Scalar(125*j,125*j,255), 1, CV_AA);
68  }
69  }
70  }
71 
73  if(param_->debug) {
74  std::cout << "% === " << std::setw(4) << loop_count;
75  std::cout << " ===== ellipse " << std::setw(4) << i << " ===" << std::endl;
76  std::cout << "ellipse.center = " << ellipse.boxEllipse.center << "; " << std::endl;
77  std::cout << "ellipse.size = " << (cv::Point2f) ellipse.boxEllipse.size << "; " << std::endl;
78  std::cout << "ellipse.angle = " << M_PI/180.0 *ellipse.boxEllipse.angle << "; " << std::endl;
79  std::cout << "ellipse.C = " << ellipse.cone.C << "; " << " % Ellipse Image" << std::endl;
80  std::cout << "ellipse.radius = " << param_->circle_diameter/2. << "; " << std::endl;
81  std::cout << "ellipse.nr_of_edges=" << ellipse.contourUndistort->size() << "; " << std::endl;
82  std::cout << "ellipse.contour = " << cv::Mat(*ellipse.contourUndistort) << "; " << std::endl;
83  std::cout << "camera.intrinsic = " << camera_.cameraMatrix << "; " << std::endl;
84  std::cout << "camera.distortions = " << camera_.distCoeffs << "; " << std::endl;
85  std::cout << "camera.projectionMatrix = " << camera_.projectionMatrix << "; " << std::endl;
86  }
87  }
88  if(param_->debug) {
89  contour_detector_.Draw(img.data);
90  }
91 }
cv::Mat_< double > distCoeffs
Definition: camera.h:35
boost::shared_ptr< std::vector< cv::Point2f > > contourUndistort
void draw_ellipses(cv::Mat &m)
void distort(const std::vector< cv::Point2f > &src, std::vector< cv::Point2f > &des)
Definition: camera.cpp:17
cv::Mat_< double > projectionMatrix
Definition: camera.h:36
cv::Vec3d normals[2]
two plausible translations solutions
std::vector< Ellipse > ellipses_
cv::Mat_< double > cameraMatrix
Definition: camera.h:34
cv::Point2d projections[2]
two plausible translations solutions
void Draw(unsigned char *pImgRGB)
Draws the detected contours in a RGB image with radom colours.
Definition: contour.cpp:163
cv::Point3d translations[2]
ellipse equation


tuw_ellipses
Author(s): Markus Bader
autogenerated on Mon Jun 10 2019 15:42:10