Image transfer issue: cv_bridge
Hey everyone.
I'm a complete newbie in ROS and am attempting to create a simple publishing/subscribing image platform. Some ROS tutorials regarding this issue use a deprecated protocol (CvBridge) so I'm stuck.
Here is what I have so far:
Image server (adapted from image_transfer tutorial (can't post links)):
int main(int argc, char** argv)
{
ros::init(argc, argv, "image_publisher");
ros::NodeHandle nh;
image_transport::ImageTransport it(nh);
cv_bridge::CvImagePtr cv_ptr(new cv_bridge::CvImage);
cv_ptr->image = cv::imread("socio_32.jpg",CV_LOAD_IMAGE_COLOR);
image_transport::Publisher pub = it.advertise("/topic_imgtransport", 1);
sensor_msgs::ImagePtr msg = cv_ptr->toImageMsg();
ros::Rate loop_rate(5);
while (nh.ok())
{
pub.publish(msg);
ros::spinOnce();
loop_rate.sleep();
}
}
And the Image Client (taken from UsingCvBridgeToConvertBetweenROSImagesAndOpenCVImages):
class ImageConverter
{
ros::NodeHandle nh_;
image_transport::ImageTransport it_;
image_transport::Subscriber image_sub_;
image_transport::Publisher image_pub_;
public:
ImageConverter()
: it_(nh_)
{
image_pub_ = it_.advertise("out", 1);
image_sub_ = it_.subscribe("/topic_imgtransport", 1, &ImageConverter::imageCb, this);
cv::namedWindow(WINDOW);
}
~ImageConverter()
{
cv::destroyWindow(WINDOW);
}
void imageCb(const sensor_msgs::ImageConstPtr& msg)
{
cout<<"Hello callback"<<endl;
cv_bridge::CvImagePtr cv_ptr;
try
{
cv_ptr = cv_bridge::toCvCopy(msg, enc::BGR8);
}
catch (cv_bridge::Exception& e)
{
ROS_ERROR("cv_bridge exception: %s", e.what());
return;
}
if (cv_ptr->image.rows > 60 && cv_ptr->image.cols > 60)
cv::circle(cv_ptr->image, cv::Point(50, 50), 10, CV_RGB(255,0,0));
cv::imshow(WINDOW, cv_ptr->image);
cv::waitKey(3);
image_pub_.publish(cv_ptr->toImageMsg());
}
};
int main(int argc, char** argv)
{
ros::init(argc, argv, "image_converter");
ImageConverter ic;
ros::spin();
return 0;
}
This doesn't work. Here is the output when I run the client and then the server:
Hello callback
[ERROR] [1362578421.357583807]: cv_bridge exception: Unrecognized image encoding []
In the cv_ptr = cv_bridge::toCvCopy(msg, enc::BGR8);
line I tried several encoding types, namely "bgr8", "mono8" (as the documentation actually states) and the exception remains the same.
I can visualize the image in the server side, and rostopic echos the correct data about the image.
So obviously the problem is in the way I am receiving the image.
Any thoughts?
The problem is that it seems like the message you are sending does not fill the encoding field. Therefore, `cv_bridge` cannot know how to interpret the image. Can you run `rostopic echo /topic_imgtransport --noarr -n 1` and post the result in your question?