ipa_3d_fov_visualization.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2018 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <cv_bridge/cv_bridge.h>
19 #include <iostream>
20 #include <ros/ros.h>
21 #include <sensor_msgs/CameraInfo.h>
22 #include <visualization_msgs/Marker.h>
23 
24 class FOVNode
25 {
26 public:
31  {
32  pub_marker_ = n_.advertise<visualization_msgs::Marker>("cam_fov",10);
33  sub_cam_ = n_.subscribe("camera_info", 10, &FOVNode::camCallback, this);
34  }
35 
38  {
40  }
41 
42 private:
44 
47  void camCallback(const sensor_msgs::CameraInfo::ConstPtr& msg);
48 };
49 
50 void FOVNode::camCallback(const sensor_msgs::CameraInfo::ConstPtr& msg)
51 {
52  bool show_marker;
53  double min_distance, max_distance;
54  ros::param::get("~show_marker", show_marker);
55  ros::param::get("~min_distance", min_distance);
56  ros::param::get("~max_distance", max_distance);
57 
58  if(show_marker==true)
59  {
60  sensor_msgs::CameraInfo camerainfo = *msg; // get camera infos
61  image_geometry::PinholeCameraModel pinmodel; // get pinhole model from header
62  pinmodel.fromCameraInfo(camerainfo);
63 
64  visualization_msgs::Marker cam_poly; // create marker
65  cam_poly.header = camerainfo.header;
66  cam_poly.action = visualization_msgs::Marker::ADD;
67  cam_poly.id = 0;
68  cam_poly.ns = "fov";
69  cam_poly.type = visualization_msgs::Marker::LINE_STRIP; // ..as line strip
70  cam_poly.scale.x = 0.01;
71  cam_poly.color.g = 1.0; // set colors
72  cam_poly.color.b = 1.0;
73  cam_poly.color.a = 0.5; // set transparency
74 
75  // calc 3D Points out of boundaries of image. Result are four points (rect at 1m distance)
76  cv::Point3d P_topleft = pinmodel.projectPixelTo3dRay(cv::Point(0, 0));
77  cv::Point3d P_downright = pinmodel.projectPixelTo3dRay(cv::Point(camerainfo.width, camerainfo.height));
78  cv::Point3d P_topright = pinmodel.projectPixelTo3dRay(cv::Point(camerainfo.width, 0));
79  cv::Point3d P_downleft = pinmodel.projectPixelTo3dRay(cv::Point(0, camerainfo.height));
80 
81 
82  // project rect into desired distances (min_distance and max_distance)
83 
84  // vertices front rect
85  geometry_msgs::Point p1, p2, p3, p4, p5, p6, p7, p8;
86  p1.x = P_topleft.x * min_distance;
87  p1.y = P_topleft.y * min_distance;
88  p1.z = P_topleft.z * min_distance;
89 
90  p2.x = P_topright.x * min_distance;
91  p2.y = P_topright.y * min_distance;
92  p2.z = P_topright.z * min_distance;
93 
94  p3.x = P_downright.x * min_distance;
95  p3.y = P_downright.y * min_distance;
96  p3.z = P_downright.z * min_distance;
97 
98  p4.x = P_downleft.x * min_distance;
99  p4.y = P_downleft.y * min_distance;
100  p4.z = P_downleft.z * min_distance;
101 
102  // vertices rear rect
103  p5.x = P_topleft.x * max_distance;
104  p5.y = P_topleft.y * max_distance;
105  p5.z = P_topleft.z * max_distance;
106 
107  p6.x = P_topright.x * max_distance;
108  p6.y = P_topright.y * max_distance;
109  p6.z = P_topright.z * max_distance;
110 
111  p7.x= P_downright.x * max_distance;
112  p7.y= P_downright.y * max_distance;
113  p7.z= P_downright.z * max_distance;
114 
115  p8.x= P_downleft.x * max_distance;
116  p8.y= P_downleft.y * max_distance;
117  p8.z= P_downleft.z * max_distance;
118 
119  // push back points to get line polynom
120  cam_poly.points.push_back(p1);
121  cam_poly.points.push_back(p2);
122  cam_poly.points.push_back(p3);
123  cam_poly.points.push_back(p4);
124  cam_poly.points.push_back(p1);
125  cam_poly.points.push_back(p5);
126  cam_poly.points.push_back(p6);
127  cam_poly.points.push_back(p7);
128  cam_poly.points.push_back(p8);
129  cam_poly.points.push_back(p5);
130  cam_poly.points.push_back(p8);
131  cam_poly.points.push_back(p4);
132  cam_poly.points.push_back(p3);
133  cam_poly.points.push_back(p7);
134  cam_poly.points.push_back(p6);
135  cam_poly.points.push_back(p2);
136 
137  pub_marker_.publish(cam_poly);
138 
139 
140  }
141 }
142 
143 int main(int argc, char *argv[])
144  {
145  ros::init(argc, argv, "visualize_cam_fov");
146  FOVNode fov;
147  ros::spin();
148  return 0;
149  }
void publish(const boost::shared_ptr< M > &message) const
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
cv::Point3d projectPixelTo3dRay(const cv::Point2d &uv_rect) const
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
FOVNode()
Constructor.
ros::Publisher pub_marker_
ros::NodeHandle n_
ROS node handle.
ROSCPP_DECL bool get(const std::string &key, std::string &s)
bool fromCameraInfo(const sensor_msgs::CameraInfo &msg)
void camCallback(const sensor_msgs::CameraInfo::ConstPtr &msg)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
ROSCPP_DECL void spin()
int main(int argc, char *argv[])
ros::Subscriber sub_cam_


ipa_3d_fov_visualization
Author(s): Florenz Graf
autogenerated on Sun Oct 18 2020 13:13:26