How to write a open cam loop?

12 views (last 30 days)
Dekel Mashiach
Dekel Mashiach on 1 Jun 2022
Answered: Pratyush Swain on 15 Dec 2023
Hey; I'm trying to converte a code from reading a video to open cam ; hope someone can help me.
videoName = 'tt1.mp4';
videoReader = VideoReader(videoName);
timeStamp = 0.06667; % time from the beginning of the video
videoReader.CurrentTime = timeStamp; % point to the chosen frame
rgbImage = readFrame(videoReader); % read frame at timeStamp seconds
figure
imshow(rgbImage) % display frame
  4 Comments
Image Analyst
Image Analyst on 1 Jun 2022
See attached sample code. Adapt as needed to do whatever algorithm you want.
Dekel Mashiach
Dekel Mashiach on 1 Jun 2022
thanks. I need something like that but what can I write instead of Videoreader?
cam = webcam('Microsoft® LifeCam HD-3000');
preview(cam)
% videoName = 'tt1.mp4';
% videoReader = VideoReader(videoName);
timeStamp = 0.06667; % time from the beginning of the video
videoReader.CurrentTime = timeStamp; % point to the chosen frame

Sign in to comment.

Answers (1)

Pratyush Swain
Pratyush Swain on 15 Dec 2023
Hi Dekel,
I understand you are trying to convert the code from reading a video to opening a camera and reading from it.The "VideoReader" function in MATLAB is designed for reading video files, not for capturing live video streams from webcams.For the purpose of acquiring image from camera device, we have the leverage the "snapshot" function.
Please refer to the example implementation as follows:
% Check available webcams
camList = webcamlist;
% Connect to the first webcam
cam = webcam(1);
% Configure camera settings (optional)
cam.Resolution = '640x480';
% Create a figure to show the captured images
hFigure = figure('Name', 'Webcam Stream', 'NumberTitle', 'off', 'CloseRequestFcn', @(src, event)setappdata(gcf, 'stopLoop', true));
% Set a flag for the loop to check
setappdata(hFigure, 'stopLoop', false);
% Create an axes object in the figure for displaying the image
hAxes = axes('Parent', hFigure);
% Loop to continuously capture images
while true
% Check if the loop should stop (when the figure is closed)
if getappdata(hFigure, 'stopLoop')
break;
end
% Capture a single image
img = snapshot(cam);
% Display the captured image on the axes
imshow(img, 'Parent', hAxes);
% Pause for a brief moment to allow the display to update
pause(0.1);
% Allow other callbacks to process
drawnow;
end
% Close the preview if it was started
closePreview(cam);
% Clear the webcam object when done
clear('cam');
% Delete the figure if it is still open
if isvalid(hFigure)
delete(hFigure);
end
The above workflow will help to retreive images from the webcam or camera device and stream them continuosly over figure window. Please make sure to install all the support packages necessary to access mobile device sensors from MATLAB , install either MATLAB Support Package for Apple iOS Sensors or MATLAB Support Package for Android Sensors.
For more information on "snapshot" function, please refer to
Hope this helps.

Categories

Find more on MATLAB Support Package for IP Cameras 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!