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


velo2cam_calibration
Author(s): Jorge Beltran , Carlos Guindel
autogenerated on Fri Feb 26 2021 03:40:57