30 #include <sensor_msgs/Image.h> 31 #include <stereo_msgs/DisparityImage.h> 33 #include <opencv2/opencv.hpp> 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);
58 nh_.
param(
"isFreeobs", isfreeobs_,
false);
59 nh_.
param(
"edges_threshold", edges_threshold_, 16);
64 void callback(
const stereo_msgs::DisparityImageConstPtr &disp,
65 const sensor_msgs::ImageConstPtr &ma) {
66 cv::Mat mask, binary_mask, output;
77 static cv::Mat disparity32(cv_im->image.rows, cv_im->image.cols, CV_32FC1);
78 disparity32 = cv_im->image;
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);
87 cv::threshold(mask, binary_mask, edges_threshold_, 255, 0);
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;
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;
106 copy_disp->min_disparity = disp->min_disparity;
107 copy_disp->max_disparity = disp->max_disparity;
108 copy_disp->delta_d = disp->delta_d;
111 sensor_msgs::Image &d_image = copy_disp->image;
112 d_image.height = disparity32.rows;
113 d_image.width = disparity32.cols;
115 d_image.step = d_image.width *
sizeof(float);
117 d_image.data.resize(d_image.step * d_image.height);
119 cv::Mat_<float> dmat(d_image.height, d_image.width,
120 (
float *)&d_image.data[0], d_image.step);
125 disparity32.copyTo(dmat, binary_mask);
128 masked_pub_.
publish(copy_disp);
132 int main(
int argc,
char **argv) {
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
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)
ROSCPP_DECL void spin(Spinner &spinner)
message_filters::Subscriber< stereo_msgs::DisparityImage > image_sub_
message_filters::Subscriber< sensor_msgs::Image > mask_sub_
Connection registerCallback(C &callback)
bool param(const std::string ¶m_name, T ¶m_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_
message_filters::Synchronizer< ExSync > sync
void callback(const stereo_msgs::DisparityImageConstPtr &disp, const sensor_msgs::ImageConstPtr &ma)