How to connect a Webcam to Simulink
Show older comments
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
More Answers (0)
Categories
Find more on Computer Vision Toolbox 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!