Ask Your Question

How to publish opencv Mat image in ROS Fuerte

asked 2013-04-23 23:21:40 -0500

KarthikMurugan gravatar image

I went through the (tutorial) about conversion between ROS and OpenCV images.... To publish opencv image, it has to be converted to ROS image msg.

Now what I have is just a Mat image,

Mat opencv_img ;

I tried :

cv_bridge::CvImagePtr cv_ptr;

cv_ptr->image = opencv_img.clone();

ros::Publisher pub;


I get an error :-

 T* boost::shared_ptr<T>::operator->() const [with T = cv_bridge::CvImage]: Assertion `px != 0' failed.
Aborted (core dumped)
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2013-04-23 23:40:51 -0500

130s gravatar image
edit flag offensive delete link more


I followed as said in the answer.... cv_bridge::CvImagePtr cv_ptr(new CvImage() ); Then , opencv_img.copyTo(cv_ptr->image);

KarthikMurugan gravatar image KarthikMurugan  ( 2013-04-24 03:31:08 -0500 )edit

But even then I get error :- error: expected type-specifier before ‘CvImage’
error: expected ‘)’ before ‘CvImage’

KarthikMurugan gravatar image KarthikMurugan  ( 2013-04-24 03:37:14 -0500 )edit

usr/include/boost/smart_ptr/shared_ptr.hpp: In constructor ‘boost::shared_ptr<T>::shared_ptr(Y*) [with Y = int, T = cv_bridge::CvImage]’: /home/karthik/Desktop/pcl/sandbox/Image_processing/src/example.cpp:116:47: instantiated from here

KarthikMurugan gravatar image KarthikMurugan  ( 2013-04-24 03:43:04 -0500 )edit

/usr/include/boost/smart_ptr/shared_ptr.hpp:187:50: error: cannot convert int * to cv_bridge::CvImage * in initialization

KarthikMurugan gravatar image KarthikMurugan  ( 2013-04-24 03:43:55 -0500 )edit

Maybe you should use the constructor of cv_bridge::CvImage to create a new instance of this class instead of using its pointer (CvImagePtr), but pay attention to fill all the necessary headers rightly before publishing.

Mike Gao gravatar image Mike Gao  ( 2013-04-30 07:42:25 -0500 )edit

How to fill the headers ?? There are members such as seq ,time, frame id... what should I put into these ??

KarthikMurugan gravatar image KarthikMurugan  ( 2013-04-30 18:40:52 -0500 )edit

cv_bridge::CvImage cv_img; cv_img.header.stamp = ros::Time::now(); cv_img.header.frame_id = "your_camera_frame"; cv_img.encoding = "mono8"; (or "bgr8") cv_img.image = img_topub; img_pub_.publish(cv_img.toImageMsg());

Mike Gao gravatar image Mike Gao  ( 2013-05-03 01:59:36 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2013-04-23 23:21:40 -0500

Seen: 1,937 times

Last updated: Apr 23 '13