no data image transport

asked 2020-06-19 15:59:05 -0500

dinesh gravatar image

updated 2020-06-19 15:59:33 -0500

When i run the image transport publisher from a launch file and pass an image as parameter it is not publishing the data. But wheni run the same node form rosrun command by passing the img parameter it publish data. Here is the image transport ros node:

#include <ros/ros.h>
#include <image_transport/image_transport.h>
#include <opencv2/highgui/highgui.hpp>
#include <cv_bridge/cv_bridge.h>

int main(int argc, char** argv)
{
  ros::init(argc, argv, "image_publisher");
  ros::NodeHandle nh;
  std::string img;

  ros::param::get("~img", img);
  if(img.empty()) {
      ROS_WARN("No image passed, abort!");
      return 0;
  }
  ROS_INFO("Got parameter : %s", img.c_str());

  image_transport::ImageTransport it(nh);
  image_transport::Publisher pub = it.advertise("/cv_camera/image_raw", 1);
  cv::Mat image = cv::imread(img, CV_LOAD_IMAGE_COLOR);
  cv::waitKey(30);
  sensor_msgs::ImagePtr msg = cv_bridge::CvImage(std_msgs::Header(), "bgr8", image).toImageMsg();

  ros::Rate loop_rate(5);
  while (nh.ok()) {
    pub.publish(msg);
    ros::spinOnce();
    loop_rate.sleep();
  }
  nh.deleteParam("img");
}

i run this node as rosrun my_image_transport my_publisher _image:=girl.jpg and it publish the image data sucesffully as i can view it with ros echo command. but when i launch the same node form ros launch file the data is empty but the parameter is passed:

<?xml version="1.0"?>
<launch>
    <arg name="img" default="girl.jpg"/>
    <node pkg="my_image_transport" type="my_publisher" name="my_publisher" output="screen">
        <param name="~img" value="$(arg img)"/>
     </node>
<!--    <node pkg="rviz" type="rviz" name="rviz"/>
    <node pkg="pure_vision" type="pure_vision" name="pure_vision" output="screen"/>-->
</launch>
edit retag flag offensive close merge delete