MATLAB: How to publish an image to ROS from Matlab

MATLABROS Toolboxrosmatlabsensor_msgs/image

How can an image be sent from matlab to ROS using the ROS I/O package?

Best Answer

Thanks to Remo in this discussion https://groups.google.com/forum/#!topic/ros-sig-matlab/y8xaZ_OWNlM this is an easy way to publish an image from matlab to ROS: roscore = rosmatlab.roscore(11311); node = rosmatlab.node('NODE',roscore.RosMasterUri); publisher = rosmatlab.publisher('/matlab_image','sensor_msgs/Image',node); msg_im_pub = rosmatlab.message('sensor_msgs/Image', node);
% Read some image here image = imread('example_image.jpg'); [height,width,channels] = size(image); image = permute(image,[3 2 1]); % Flip dimensions image = image(:); % Vectorize
% Populate image message msg_im_pub.setStep(width*channels); msg_im_pub.setWidth(width); msg_im_pub.setHeight(height); msg_im_pub.setEncoding('rgb8'); % RGB image msg_im_pub.setIsBigendian(false);
% Create java buffer to save the image array messageBuffer = org.ros.internal.message.MessageBuffers.dynamicBuffer(); stream = org.jboss.netty.buffer.ChannelBufferOutputStream(messageBuffer); stream.write(image); msg_im_pub.setData(stream.buffer().copy()); stream.buffer().clear(); % Clear java buffer
publisher.publish(msg_im_pub);
with the ease of use of java buffers
thanks Remo for you aportation