Clear Filters
Clear Filters

How to connect a Webcam to Simulink

11 views (last 30 days)
I initialized a video input object in a simulink callback function (initialization function), and I am reading the video feed from the webcam through a MATLAB function block. However, the function block does not identify the video object, but the video object is present in the base workspace.
Simulink InitFcn callback block
load('stereoParams.mat');
vidobj=videoinput('winvideo',1);%,'MJPG_1280x480');
triggerconfig(vidobj, 'manual');
start(vidobj);
%save('vidobj.mat', 'vidobj'); ---- this is used to save it and read it
%using FromFile block.
stopFcn:
delete(vidobj);
disp("Closed");
MATLAB function block: (tried the same thing in interpreted MATLAB function block)
function disparityMap = camera(steps, data)
if steps>=20
% vidobj = getVariable(mdwks, 'vidobj');
img=getsnapshot(vidobj);
imgL = img(:,1:640,:);
imgR = img(:,641:1280,:);
[JL,JR] = rectifyStereoImages(imgL,imgR,stereoParams);
IL = rgb2gray(JL);
IR = rgb2gray(JR);
disparityMap = disparitySGM(IL,IR, 'DisparityRange',[0,64]);%, 'InitialMagnification',50);
% imshow(disparityMap,[0,64],);
disp("running")
else
disparityMap = [0,0;0,0];
end
end
How can I read this video object from the base workspace? It is not a timeseries data. It is a object which update with time from camera.
I tried "from workspace" and "from file" blocks to read the video object, but I get this error.
Error: "from workspace"
Invalid structure-format variable specified as workspace input in 'PID_lengthChanges_joystic_copy/From Workspace'. If the input signal is a bus signal, the variable must be a structure of MATLAB timeseries objects. Otherwise, the variable must include 'time' and 'signals' fields, and the 'signals' field must be a structure with a 'values' field.
Error: "from file"
Simulink does not support loading the input data in file 'vidobj.mat'. For data saved using MAT file versions prior to 7.3, Simulink can only load two-dimensional arrays consisting of one-dimensional, double, noncomplex samples. To load data of any other type, complexity or dimension, use a timeseries object and save the file using MAT file version 7.3 or later. For example, use: 'save file_name -v7.3 timeseries_object'.

Accepted Answer

Biral Pradhan
Biral Pradhan on 5 Jun 2022
I understand, you have a video input from an external hardware using Windows Video adaptor, and want to import it into Simulink. I would kindly like to inform you that "Image Acquisition Toolbox™ Support Package for OS Generic Video Interface" allows this functionality. There is a block, called "From Video Device", that lets you capture image and video data streams from image acquisition devices, such as cameras and frame grabbers, in order to bring the image data into a Simulink model.
For more information, kindly follow the following documentation links.

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!