How to live stream on 2 axes from the same camera?

3 views (last 30 days)
I'm building app on appdesigner in which I try to stream live videos from the same camera on 2 axes
with the following code:
classdef imApp < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
StartButton matlab.ui.control.Button
UIAxes1 matlab.ui.control.UIAxes
UIAxes2 matlab.ui.control.UIAxes
end
properties (Access = private)
cam; % Description
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: StartButton
function StartButtonPushed(app, event)
app.cam = webcam;
frame = snapshot(app.cam);
im1 = image(app.UIAxes1, zeros(size(frame),'uint8'));
im2 = image(app.UIAxes2, zeros(size(frame),'uint8'));
axis(app.UIAxes1,'image');
axis(app.UIAxes2,'image');
preview(app.cam,im1);
preview(app.cam,im2);
% pause;
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 800 800];
app.UIFigure.Name = 'UI Figure';
% Create StartButton
app.StartButton = uibutton(app.UIFigure, 'push');
app.StartButton.ButtonPushedFcn = createCallbackFcn(app, @StartButtonPushed, true);
app.StartButton.Position = [271 430 100 22];
app.StartButton.Text = 'Start';
% Create UIAxes
app.UIAxes1 = uiaxes(app.UIFigure);
title(app.UIAxes1, 'Title')
xlabel(app.UIAxes1, 'X')
ylabel(app.UIAxes1, 'Y')
app.UIAxes1.Position = [51 35 511 363];
%Create second axis
app.UIAxes2 = uiaxes(app.UIFigure);
title(app.UIAxes2, 'Title')
xlabel(app.UIAxes2, 'X')
ylabel(app.UIAxes2, 'Y')
app.UIAxes2.Position = [600 35 511 363];
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = untitled
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
Unfortunately, it only stream on one axe and not both on the same time, how to make it stream the same video on the same time on both axes ?

Answers (1)

Nivedita
Nivedita on 12 Sep 2023
Hi Guy Hanzon!
I understand that you are trying to stream live video from the same camera at the same time on two different axes in your app.
You can achieve it by modifying your “ButtonPushed” callback function in the following manner:
% Button pushed function: StartButton
function StartButtonPushed(app, event)
app.cam = webcam;
frame = snapshot(app.cam);
% Create image objects for each axis
im1 = image(app.UIAxes1, zeros(size(frame), 'uint8'));
axis(app.UIAxes1, 'image');
im2 = image(app.UIAxes2, zeros(size(frame), 'uint8'));
axis(app.UIAxes2, 'image');
% Start streaming the video
while true
frame = snapshot(app.cam);
set(im1, 'CData', frame);
set(im2, 'CData', frame);
drawnow;
end
end
  • The “while” loop continuously captures frames from the webcam and updates the image data for both the image objects (“im1” and “im2”).
  • The “set” function is used to update the image data with the captured frame.
  • The “drawnow” function updates the display and ensures that the image is shown on the axes immediately.
For more information on “while” loop, and “set” and “drawnow” functions, refer to the following documentation links:
I hope this helps!
Regards,
Nivedita.

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!