32 #include <pcl/point_cloud.h> 34 #include <pcl/common/transforms.h> 35 #include <pcl/filters/passthrough.h> 37 #include <opencv2/opencv.hpp> 39 #include <sensor_msgs/CameraInfo.h> 60 if (v < vmin) v = vmin;
61 if (v > vmax) v = vmax;
64 if (v < (vmin + 0.25 * dv)) {
66 c.
g = 4 * (v - vmin) / dv;
67 }
else if (v < (vmin + 0.5 * dv)) {
69 c.
b = 1 + 4 * (vmin + 0.25 * dv - v) / dv;
70 }
else if (v < (vmin + 0.75 * dv)) {
71 c.
r = 4 * (v - vmin - 0.5 * dv) / dv;
74 c.
g = 1 + 4 * (vmin + 0.75 * dv - v) / dv;
89 out_mat (0, 0) = mv[0]; out_mat (0, 1) = mv[4]; out_mat (0, 2) = mv[8];
90 out_mat (1, 0) = mv[1]; out_mat (1, 1) = mv[5]; out_mat (1, 2) = mv[9];
91 out_mat (2, 0) = mv[2]; out_mat (2, 1) = mv[6]; out_mat (2, 2) = mv[10];
93 out_mat (3, 0) = out_mat (3, 1) = out_mat (3, 2) = 0; out_mat (3, 3) = 1;
94 out_mat (0, 3) = origin.
x ();
95 out_mat (1, 3) = origin.
y ();
96 out_mat (2, 3) = origin.
z ();
102 for (
int i = 0, k = 0; i < 3; i++) {
103 for (
int j = 0; j < 4; j++, k++){
104 out_mat(i, j) = cinfo_msg->P[k];
107 out_mat(3,0) = out_mat(3,1) = out_mat(3,2) = 0;
111 void callback(
const PointCloud2::ConstPtr& pcl_msg,
const CameraInfoConstPtr& cinfo_msg,
const ImageConstPtr& image_msg){
114 pcl::PointCloud<Velodyne::Point>::Ptr pcl_cloud(
new pcl::PointCloud<Velodyne::Point>);
115 pcl::PointCloud<Velodyne::Point>::Ptr filtered_pcl_cloud(
new pcl::PointCloud<Velodyne::Point>);
116 pcl::PointCloud<Velodyne::Point>::Ptr trans_cloud(
new pcl::PointCloud<Velodyne::Point>);
126 ROS_ERROR(
"cv_bridge exception: %s", e.what());
138 listener.
lookupTransform (image_msg->header.frame_id.c_str(), pcl_msg->header.frame_id.c_str(),
ros::Time(0), transform);
140 ROS_WARN(
"[pcl_projection] TF exception:\n%s", ex.what());
146 pcl::PassThrough<Velodyne::Point> pass;
147 pass.setInputCloud (pcl_cloud);
148 pass.setFilterFieldName (
"x");
149 pass.setFilterLimits (0.0, 100.0);
150 pass.filter (*filtered_pcl_cloud);
153 Eigen::Matrix4f transf_mat;
158 Eigen::Matrix4f projection_matrix;
162 Eigen::MatrixXf points_all = filtered_pcl_cloud->getMatrixXfMap();
165 Eigen::MatrixXf points = points_all.topRows(4);
166 points.row(3) = Eigen::MatrixXf::Constant(1, points_all.cols(), 1);
169 Eigen::MatrixXf points_img = projection_matrix * transf_mat * points;
170 points_img.row(0) = points_img.row(0).cwiseQuotient(points_img.row(2));
171 points_img.row(1) = points_img.row(1).cwiseQuotient(points_img.row(2));
174 for (
int i = 0; i < points_img.cols(); i++){
175 cv::Point2i p(points_img(0,i), points_img(1,i));
177 if (p.x >= 0 && p.x < cinfo_msg->width && p.y >= 0 && p.y < cinfo_msg->height){
179 float depth = filtered_pcl_cloud->points[i].range;
182 cv::circle(image, p, 3, cv::Scalar(
int(255*c.
b),
int(255*c.
g),
int(255*c.
r)), -1);
191 int main(
int argc,
char **argv){
201 pub = it.
advertise(
"image_with_points", 1);
CvImageConstPtr toCvShare(const sensor_msgs::ImageConstPtr &source, const std::string &encoding=std::string())
COLOUR GetColour(double v, double vmin, double vmax)
image_transport::Publisher pub
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
Publisher advertise(const std::string &base_topic, uint32_t queue_size, bool latch=false)
void fromROSMsg(const sensor_msgs::PointCloud2 &cloud, pcl::PointCloud< T > &pcl_cloud)
ROSCPP_DECL void spin(Spinner &spinner)
TFSIMD_FORCE_INLINE const tfScalar & x() const
Connection registerCallback(C &callback)
TFSIMD_FORCE_INLINE const tfScalar & z() const
void publish(const sensor_msgs::Image &message) const
void projectionAsMatrix(const CameraInfoConstPtr &cinfo_msg, Eigen::Matrix4f &out_mat)
void transformAsMatrix(const tf::Transform &bt, Eigen::Matrix4f &out_mat)
TFSIMD_FORCE_INLINE const tfScalar & y() const
void addRange(pcl::PointCloud< Velodyne::Point > &pc)
void callback(const PointCloud2::ConstPtr &pcl_msg, const CameraInfoConstPtr &cinfo_msg, const ImageConstPtr &image_msg)
void getOpenGLSubMatrix(tfScalar *m) const
int main(int argc, char **argv)
sensor_msgs::ImagePtr toImageMsg() const