librealsense icon indicating copy to clipboard operation
librealsense copied to clipboard

MATLAB Live Streaming and More Examples

Open apoorva-sriv opened this issue 8 years ago • 10 comments

Required Info
Camera Model D435
Firmware Version 05.10.06.00
Operating System & Version Win 10
Platform PC
SDK Version 2.16.1
Language MATLAB

Issue Description

I am currently using this code (modified depth_example.m) to live stream depth: ` function depth_example() % Make Pipeline object to manage streaming pipe = realsense.pipeline(); % Make Colorizer object to prettify depth output colorizer = realsense.colorizer();

% Start streaming on an arbitrary camera with default settings
profile = pipe.start();

% Get streaming device's name
dev = profile.get_device();
name = dev.get_info(realsense.camera_info.name);

% -----------------------------First frame ----------------------------------
fs = pipe.wait_for_frames();
% Select depth frame
depth = fs.get_depth_frame();
% Colorize depth frame
color = colorizer.colorize(depth);

% Get actual data and convert into a format imshow can use
% (Color data arrives as [R, G, B, R, G, B, ...] vector)
data = color.get_data();
img = permute(reshape(data', [3, color.get_width(), color.get_height()]), [3 2 1]);

% Display image
h = imshow(img);

% ---------------------------------------------------------------------

% Get frames.
while true
    fs = pipe.wait_for_frames();
    % Select depth frame
    depth = fs.get_depth_frame();
    % Colorize depth frame
    color = colorizer.colorize(depth);
    
    % Get actual data and convert into a format imshow can use
    % (Color data arrives as [R, G, B, R, G, B, ...] vector)
    data = color.get_data();
    img = permute(reshape(data', [3, color.get_width(), color.get_height()]), [3 2 1]);
    
    % Display image
    set(h, 'CData', img);  % faster than imshow inside a loop
    drawnow;
end

% Stop streaming
pipe.stop();

end ` Is there a more efficient way to live stream in MATLAB? Also, is it possible to add more examples for common cases like changing camera settings and different streams like the ones for C++?

apoorva-sriv avatar Nov 10 '18 15:11 apoorva-sriv

This is what I have from modifying the depth_example:

`function stream_example()

%Make Pipeline object to manage streaming
pipe = realsense.pipeline();
%Make Colorizer object to prettify depth output
colorizer = realsense.colorizer();
%Start streaming on an arbitrary camera with default settings
profile = pipe.start()

%Set the timeframe for streaming in seconds
tic
    while toc < 5
        
%Wait for the frames
fs = pipe.wait_for_frames();
%Select depth frame
depth = fs.get_depth_frame();
%Colourize the depth frame 
color = colorizer.colorize(depth);
%Get actual data and convert into a format imshow can use
data = color.get_data();
%(Color data arrives as [R, G, B, R, G, B, ...] vector)
img = permute(reshape(data',[3,color.get_width(),color.get_height()]),[3 2 1]);
%Display the image
imshow(img)
end
end`

I'm currently working on getting the x,y,z data whilst streaming. In terms of camera settings, I currently set them in the SDK before streaming in Matlab, although I'm not sure if there is an update for setting the processing options from issue #2520

LouMagsClark avatar Nov 11 '18 23:11 LouMagsClark

Yes, that's similar to what I have.

Do you know how to stream the infrared and RGB frames as well?

apoorva-sriv avatar Nov 11 '18 23:11 apoorva-sriv

@apoorva2398 you can check out the config class, the enable stream API is what you're looking for. The C++ version might be a bit more legible, and its identical to the Matlab one.

lramati avatar Nov 12 '18 12:11 lramati

I keep getting an error at the third line when I run

pipe = realsense.pipeline(); config = realsense.config(); config.enable_stream(realsense.stream.depth, 1280, 720, realsense.format.z16, 30);

The error is Error using realsense.stream Cannot find a member of the 'realsense.stream' enumeration class that corresponds to each element of the given input argument.

Error in realsense.config/enable_stream (line 86) args = num2cell(int64([varargin{:}]));

Error in Sampling_IR1_and_Depth (line 5) config.enable_stream(realsense.stream.depth, 1280, 720, realsense.format.z16, 30)

I tried stepping into the code, but I couldn't debug it. Could you please tell me what is wrong?

apoorva-sriv avatar Nov 12 '18 17:11 apoorva-sriv

I think its an issue with the square brackets on the line in the error, where its trying to convert all the args into realsense.stream objects to fit in the list. My knowledge of Matlab isn't great so there are some errors of this kind in the m files of the wrapper. Replacing the line with a for loop that accomplishes the same thing should solve your issue

lramati avatar Nov 12 '18 18:11 lramati

Thank you! The updated config.m file works correctly.

apoorva-sriv avatar Nov 12 '18 21:11 apoorva-sriv

Great! I'll add it to the repository as soon as I get a chance

lramati avatar Nov 13 '18 01:11 lramati

Hi @apoorva2398 and @LouMagsClark ,

I am a new user of D415 and Matlab, I wanted to know if you managed to stream the infrared and RGB frames ?

jattiogbe avatar Mar 14 '19 14:03 jattiogbe

@jattiogbe Here is the code for streaming RGB frames:

%Make Pipeline object to manage streaming pipe = realsense.pipeline(); %Start streaming on an arbitrary camera with default settings profile = pipe.start()

%Set the timeframe for streaming in seconds tic while toc < inf

%Wait for the frames fs = pipe.wait_for_frames(); %Select color frame color = fs.get_color_frame(); %Get actual data and convert into a format imshow can use data = color.get_data(); %(Color data arrives as [R, G, B, R, G, B, ...] vector) img = permute(reshape(data',[3,color.get_width(),color.get_height()]),[3 2 1]); %Display the image imshow(img) end

Kind regards

DieterHeestermans avatar Mar 15 '19 15:03 DieterHeestermans

Great, Thank you ! It works perfectly .

Now I am trying to display one figure for each stream : depth and rgb. But of course, if I try to put the code for each stream in the same while loop , I have a new figure popping up every second. I also tried adding a new frameset or pipeline, but it won't work.

Do you know how to do it @DieterHeestermans ?

Best regards

jattiogbe avatar Mar 15 '19 15:03 jattiogbe