Watch preview while recording snapshots?

8 ビュー (過去 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 Center および File ExchangeVideo capture and display についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by