SuperimposeMesh
All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
SISkeleton.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016-2019 Istituto Italiano di Tecnologia (IIT)
3  *
4  * This software may be modified and distributed under the terms of the
5  * BSD 3-Clause license. See the accompanying LICENSE file for details.
6  */
7 
9 
10 #include <iostream>
11 
12 #include <glm/gtc/matrix_transform.hpp>
13 #include <glm/gtc/type_ptr.hpp>
14 
15 #include <opencv2/core/core.hpp>
16 #include <opencv2/calib3d/calib3d.hpp>
17 #include <opencv2/imgproc/imgproc.hpp>
18 
19 
21 (
22  const std::list<std::string>& skeleton_part,
23  const float cam_fx,
24  const float cam_fy,
25  const float cam_cx,
26  const float cam_cy
27  ) :
28  skeleton_part_(skeleton_part)
29 {
30  std::cout << log_ID_ << "Setting up default projection matrix." << std::endl;
31 
32  setProjectionMatrix(cam_fx, cam_fy, cam_cx, cam_cy);
33 }
34 
35 
37 { }
38 
39 
41 (
42  const ModelPoseContainer& objpos_map,
43  const double* cam_x,
44  const double* cam_o,
45  cv::Mat& img
46 )
47 {
48  cam_pos_ = glm::make_vec3(cam_x);
49  root_to_eye_ = glm::transpose(glm::mat3(glm::rotate(glm::mat4(1.0f), static_cast<float>(cam_o[3]), glm::vec3(static_cast<float>(cam_o[0]), static_cast<float>(cam_o[1]), static_cast<float>(cam_o[2])))));
50 
51  glm::vec2 ee_px = getWorldToPixel((objpos_map.find(skeleton_part_.front())->second).data());
52 
53  cv::Point endeffector_point(static_cast<int>(ee_px.x), static_cast<int>(ee_px.y));
54  cv::circle(img, endeffector_point, 4, cv::Scalar(0, 255, 0), 4);
55 
56  for (auto part = ++skeleton_part_.cbegin(); part != skeleton_part_.cend(); ++part)
57  {
58  cv::Point base_line = endeffector_point;
59  for (auto map = objpos_map.equal_range(*part).first; map != objpos_map.equal_range(*part).second; ++map)
60  {
61  glm::vec2 joint_px = getWorldToPixel((map->second).data());
62 
63  cv::Point joint_point(static_cast<int>(joint_px.x), static_cast<int>(joint_px.y));
64 
65  cv::circle(img, joint_point, 3, cv::Scalar(0, 0, 255), 4);
66  cv::line(img, base_line, joint_point, cv::Scalar(255, 0, 0), 2);
67 
68  base_line = joint_point;
69  }
70  }
71 
72  return true;
73 }
74 
75 
77 (
78  const float cam_fx,
79  const float cam_fy,
80  const float cam_cx,
81  const float cam_cy
82 )
83 {
84 
85  /* Projection matrix. */
86  /* Intrinsic camera matrix: (232.921 0.0 162.202 0.0
87  0.0 232.43 125.738 0.0
88  0.0 0.0 1.0 0.0)
89  Remember that GLM is column-major. */
90  projection_ = glm::mat3(cam_fx, 0.0f, 0.0f,
91  0.0f, cam_fy, 0.0f,
92  cam_cx, cam_cy, 1.0f);
93 
94  return true;
95 }
96 
97 
98 glm::vec2 SISkeleton::getWorldToPixel(const double* world_point)
99 {
100  glm::vec3 pos = glm::make_vec3(world_point);
101  glm::vec3 px_h = projection_ * (root_to_eye_ * (pos - cam_pos_));
102 
103  return glm::vec2(px_h) / px_h.z;
104 }
SISkeleton(const std::list< std::string > &skeleton_part, const float cam_fx, const float cam_fy, const float cam_cx, const float cam_cy)
Definition: SISkeleton.cpp:21
glm::vec2 getWorldToPixel(const double *world_point)
Definition: SISkeleton.cpp:98
glm::mat3 root_to_eye_
Definition: SISkeleton.h:40
glm::vec3 cam_pos_
Definition: SISkeleton.h:42
std::multimap< std::string, ModelPose > ModelPoseContainer
Definition: Superimpose.h:25
bool setProjectionMatrix(const float cam_fx, const float cam_fy, const float cam_cx, const float cam_cy)
Definition: SISkeleton.cpp:77
glm::mat3 projection_
Definition: SISkeleton.h:38
bool superimpose(const ModelPoseContainer &objpos_map, const double *cam_x, const double *cam_o, cv::Mat &img) override
Definition: SISkeleton.cpp:41