disp_masker.cpp
Go to the documentation of this file.
1 /*
2  velo2cam_calibration - Automatic calibration algorithm for extrinsic parameters of a stereo camera and a velodyne
3  Copyright (C) 2017-2018 Jorge Beltran, Carlos Guindel
4 
5  This file is part of velo2cam_calibration.
6 
7  velo2cam_calibration is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 2 of the License, or
10  (at your option) any later version.
11 
12  velo2cam_calibration is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with velo2cam_calibration. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 /*
22  disp_masker: Mask the disparity map according to the edges image
23 */
24 
25 #include <ros/ros.h>
30 #include <opencv2/opencv.hpp>
32 #include <cv_bridge/cv_bridge.h>
33 #include <sensor_msgs/Image.h>
34 #include <stereo_msgs/DisparityImage.h>
35 
36 class Masker
37 {
38 public:
40 
44 
45  bool isfreeobs_;
47 
50 
51  Masker():
52  nh_("~"),
53  image_sub_(nh_, "image", 1),
54  mask_sub_(nh_, "mask", 1),
55  sync(ExSync(100), image_sub_, mask_sub_)
56  {
57 
58  masked_pub_ = nh_.advertise<stereo_msgs::DisparityImage>("output", 1);
59 
60  nh_.param("isFreeobs", isfreeobs_, false);
61  nh_.param("edges_threshold", edges_threshold_, 16);
62 
63  sync.registerCallback(boost::bind(&Masker::callback, this, _1, _2));
64 
65  }
66 
67  void callback(const stereo_msgs::DisparityImageConstPtr& disp, const sensor_msgs::ImageConstPtr& ma)
68  {
69  cv::Mat mask, binary_mask, output;
71 
72  try
73  {
75  mask = cv_bridge::toCvShare(ma, "mono8")->image;
76  }
77  catch (cv_bridge::Exception& e)
78  {
79  ROS_ERROR("CvBridge failed");
80  }
81 
82  static cv::Mat disparity32(cv_im->image.rows, cv_im->image.cols, CV_32FC1);
83  disparity32 = cv_im->image;
84 
85  if (isfreeobs_){
86  const static int OBSTACLE_LABEL = 32;
87  cv::Mat obs_pattern(mask.rows, mask.cols, CV_8UC1, cv::Scalar(OBSTACLE_LABEL));
88  cv::bitwise_and(mask, obs_pattern, binary_mask);
89  binary_mask = binary_mask * (255.0/OBSTACLE_LABEL);
90  }else{
91  cv::threshold(mask, binary_mask, edges_threshold_, 255, 0);
92  }
93 
94  // Copy input disparity to another DisparityImage variable
95  stereo_msgs::DisparityImagePtr copy_disp = boost::make_shared<stereo_msgs::DisparityImage > ();
96  copy_disp->valid_window.x_offset = disp->valid_window.x_offset;
97  copy_disp->valid_window.y_offset = disp->valid_window.y_offset;
98  copy_disp->valid_window.width = disp->valid_window.width;
99  copy_disp->valid_window.height = disp->valid_window.height;
100  copy_disp->header = disp->header;
101  copy_disp->image.header = disp->header;
102  copy_disp->image.encoding = sensor_msgs::image_encodings::TYPE_32FC1;
103  copy_disp->image.height = disp->image.height;
104  copy_disp->image.width = disp->image.width;
105  copy_disp->image.step = disp->image.step;
106  copy_disp->T = disp->T;
107  copy_disp->f = disp->f;
108 
109  copy_disp->min_disparity = disp->min_disparity;
110  copy_disp->max_disparity = disp->max_disparity;
111  copy_disp->delta_d = disp->delta_d;
112 
113  // Create cv::Mat from the copies DisparityImage input
114  sensor_msgs::Image& d_image = copy_disp->image;
115  d_image.height = disparity32.rows;
116  d_image.width = disparity32.cols;
117  d_image.encoding = sensor_msgs::image_encodings::TYPE_32FC1;
118  d_image.step = d_image.width * sizeof(float);
119 
120  d_image.data.resize(d_image.step * d_image.height);
121 
122  cv::Mat_<float> dmat(d_image.height, d_image.width, (float*)&d_image.data[0], d_image.step);
123 
124  // Check data
125  ROS_ASSERT(dmat.data == &d_image.data[0]);
126 
127  disparity32.copyTo(dmat, binary_mask);
128 
129  // Publish obstacle disparity
130  masked_pub_.publish(copy_disp);
131  }
132 
133 };
134 
135 int main(int argc, char **argv)
136 {
137  ros::init(argc, argv, "image_masker");
138 
139  Masker im;
140 
141  ROS_INFO("Ready");
142  ros::spin();
143 }
CvImageConstPtr toCvShare(const sensor_msgs::ImageConstPtr &source, const std::string &encoding=std::string())
message_filters::sync_policies::ExactTime< stereo_msgs::DisparityImage, sensor_msgs::Image > ExSync
Definition: disp_masker.cpp:48
void publish(const boost::shared_ptr< M > &message) const
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
int edges_threshold_
Definition: disp_masker.cpp:46
ros::NodeHandle nh_
Definition: disp_masker.cpp:39
ROSCPP_DECL void spin(Spinner &spinner)
message_filters::Subscriber< stereo_msgs::DisparityImage > image_sub_
Definition: disp_masker.cpp:41
message_filters::Subscriber< sensor_msgs::Image > mask_sub_
Definition: disp_masker.cpp:42
Connection registerCallback(C &callback)
#define ROS_INFO(...)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
const std::string TYPE_32FC1
int main(int argc, char **argv)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
ros::Publisher masked_pub_
Definition: disp_masker.cpp:43
bool isfreeobs_
Definition: disp_masker.cpp:45
#define ROS_ASSERT(cond)
message_filters::Synchronizer< ExSync > sync
Definition: disp_masker.cpp:49
#define ROS_ERROR(...)
void callback(const stereo_msgs::DisparityImageConstPtr &disp, const sensor_msgs::ImageConstPtr &ma)
Definition: disp_masker.cpp:67


velo2cam_calibration
Author(s): Jorge Beltran , Carlos Guindel
autogenerated on Thu Feb 28 2019 03:24:25