MATLAB Answers

Need help to capture and save image at every 5 seconds from both camera?

2 views (last 30 days)
M Faisal Riyad
M Faisal Riyad on 28 Oct 2020
Commented: M Faisal Riyad on 2 Nov 2020
Hello,
I wrote the following code - which can read 2 USB camera connected to my PC. Now i want to create a loop or function that will tell each camera to take a snapshot at every 10 seconds and save them in the disk. How do i do that?
clc; clear;
close all; objects = imaqfind %find video input objects in memory
delete(objects) %delete a video input object from memory
vid1 = videoinput('winvideo', 1, 'MJPG_1280x720');
vid2 = videoinput('winvideo', 3, 'RGB24_1824x1216');
%camera 1 - for force sensor beam
vid1.FramesPerTrigger = 1;
vid1.TriggerRepeat = inf;
triggerconfig(vid1, 'manual');
vid1.TimerFcn = 'trigger(vid1)';
vid1.TimerPeriod = 5;
%camera 2 - strain measurement
vid2.FramesPerTrigger = 1;
vid2.TriggerRepeat = inf;
triggerconfig(vid2, 'manual');
vid2.TimerFcn = 'trigger(vid1)';
vid2.TimerPeriod = 5;
%view the camera
preview(vid1);
preview(vid2);
snapshot1 = getsnapshot(vid1);
snapshot2 = getsnapshot(vid2);

Answers (1)

Rishik Ramena
Rishik Ramena on 2 Nov 2020
Edited: Rishik Ramena on 2 Nov 2020
For continuous capture, you can use the getsnapshot function inside a loop with a pause function. For saving the snapshot to the disk you can use imwrite. Here's a sample snippet.
i = 1;
while true % run for indefinitely long time
snapshot1 = getsnapshot(vid1);
snapshot2 = getsnapshot(vid2);
imwrite(snapshot1, ['camera1_' num2str(i) '.png']); %save the snapshots for camera1
imwrite(snapshot2, ['camera2_' num2str(i) '.png']); %save the snapshots for camera2
pause(10); % pause for 10 seconds
i = i+1;
end
If you're worried about the timing overheads in these do have a look this example.

Community Treasure Hunt

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

Start Hunting!

Translated by