How to write a open cam loop?

4 次查看(过去 30 天)
Dekel Mashiach
Dekel Mashiach 2022-6-1
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 个评论
Image Analyst
Image Analyst 2022-6-1
See attached sample code. Adapt as needed to do whatever algorithm you want.
Dekel Mashiach
Dekel Mashiach 2022-6-1
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

请先登录,再进行评论。

回答(1 个)

Pratyush Swain
Pratyush Swain 2023-12-15
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.

类别

Help CenterFile Exchange 中查找有关 MATLAB Support Package for IP Cameras 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by