How to write a open cam loop?

조회 수: 6 (최근 30일)
Dekel Mashiach
Dekel Mashiach 2022년 6월 1일
답변: Pratyush Swain 2023년 12월 15일
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