Watch preview while recording snapshots?

조회 수: 4 (최근 30일)
Katie
Katie 2025년 6월 3일
답변: Abhiram 2025년 6월 9일
Hello! I am trying to watch a video from my IP camera live while also recording the output. My code, below, is successful at showing the first frame, and will record for my specified duration. I have just removed the IP address out of caution. The preview screen and recording will end when I hit the quit key.
Is this watching the preview and recording snapshots in real time possible? How would I implement this?
Thanks!
cam = ipcam('http://<URL>','USER','PASS'); % calls the camera, obscured for privacy :)
KbName('UnifyKeyNames');
quit = KbName('q');
preview(cam) % how to continue viewing live footage until closePreview?
frames = 200;
videoRecord = VideoWriter('camera_test', 'MPEG-4');
open(videoRecord)
for i =1:frames
img = snapshot(cam);
writeVideo(videoRecord,img)
[keyIsDown,~,keyCode]=KbCheck(-1);
if (keyIsDown==1 && keyCode(quit))
KbReleaseWait();
break
end
frames = frames+1;
end
closePreview(cam);
close(videoRecord)

채택된 답변

Abhiram
Abhiram 2025년 6월 9일
Hi @Katie,
To watch a live preview of the IP camera while recording frames in real time in MATLAB, you can create a custom live preview & recording loop which manually shows each frame in a figure using “imshow” or “image” and update it in real time. A sample implementation is mentioned below:
cam = ipcam('http://<URL>','USER','PASS'); % your IP cam
KbName('UnifyKeyNames');
quit = KbName('q');
framesToCapture = 200;
videoRecord = VideoWriter('camera_test', 'MPEG-4');
open(videoRecord);
% Create a figure window for live preview
hFig = figure('Name', 'Live Camera Feed', 'NumberTitle', 'off');
set(hFig, 'KeyPressFcn', @(obj, evt) assignin('base','keyPressed',evt.Key));
assignin('base','keyPressed','');
for i = 1:framesToCapture
if ~isvalid(hFig) % Exit if figure is closed
break;
end
img = snapshot(cam);
if i == 1
hImage = imshow(img); % Show first frame
else
set(hImage, 'CData', img); % Update image data
end
writeVideo(videoRecord, img); % Record frame
drawnow; % Force update of GUI
% Check for 'q' key to quit early
keyPressed = evalin('base','keyPressed');
if strcmp(keyPressed, 'q')
break;
end
end
close(videoRecord);
clear cam;
close(hFig);
disp('Recording finished.');

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Video capture and display에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by