2011/ros11: getSensorImg_IPC.m

File getSensorImg_IPC.m, 1.0 KB (added by cteo, 7 years ago)

helper files

Line 
1% function to get one image from ROS via IPC and return it to the matlab
2% function
3% Author: Ching Lik Teo
4% Date: 05-17-2011
5% sid: the sensor id
6% mode: 'read' for
7
8function [img, rgb_msg]=getSensorImg_IPC(sid, mode)
9% inituialize
10img=[];
11
12% receive an RGB image
13rgb_msg = sensor_msgs_Image(mode,sid, 1);
14% always check to see we actually received a message
15if(~isempty(rgb_msg))
16
17    % convert from signed to unsigned
18    rgb_msg.data(rgb_msg.data < 0) = rgb_msg.data(rgb_msg.data < 0) + 256;
19
20    % scale it to 0.0<->1.0 to make matlab happy
21    a = rgb_msg.data ./ 255;
22
23    % convert 1D R,G,B, vectors into 2D image planes
24    % flip them horizontally about the center
25    a_r = fliplr(reshape(a(1:3:end),640,480));
26    a_g = fliplr(reshape(a(2:3:end),640,480));
27    a_b = fliplr(reshape(a(3:3:end),640,480));
28
29    % glue the image planes together to make an RGB image
30    a_r(:,:,2) = a_g;
31    a_r(:,:,3) = a_b;
32   
33    % rotate the image 90 degrees
34    img = imrotate(a_r,90);
35   
36end