2017-03-01 75 views
0

我需要将所有捕获的图像保存在MATLAB中,但我可以一次保存一张图片。如何在MATLAB中保存所有顺序捕获的图像?

mycam = webcam(); 
img = snapshot(mycam); 
imwrite(img,'img.jpg'); 

如果有人知道如何保存在MATLAB中一次拍摄的所有图片,请帮我看看代码。

+1

使用循环。 –

+1

在使用循环之前,请确保每次迭代时文件名都是不同的,或者您只是要覆盖同一个文件。 – rayryeng

+0

谢谢你的回复。 我用过,但没有得到所需的结果。一直保存序列的最后一帧。 – prithvi17

回答

0

正如他们已经告诉过您的,您应该使用for循环与sprintf函数,以便不覆盖以前的图像。尝试使用以下命令:

%capture the frames 
    for i =1:n;% n is the number of frames you want to capture 
     frames{i} = getsnapshot(mycam); 
    end 
    %save in the current folder 
    for i = 1:n; 
     imwrite(frames{i}, sprintf('imageName%d.jpg',i)) 
    end 

您将所有捕获的帧保存在当前文件夹中。

+0

感谢您的回复 – prithvi17

0

我会将图像保存为电影,然后再访问这些帧。这是未经测试的,但它会这样工作。

mycam = webcam(); 
% if you know the number of images use that here 
% a movie is just a collection of frames 
% if not then just don't initialize F 
F(nFrames) = struct('cdata', [], 'colormap, []); 
for i = 1:nFrames 
    F(i) = im2frame(snapshot(mycam)); 
end 
% save F 
movie2avi(F, 'MyMovie.avi', 'compression', 'None'); 

然后,你可以加载电影,看看帧。本例使用旧的movie2aviVideoWriter也是一种选择

v = VideoWriter('MyMovie.avi'); 
open(v); 
for i = 1:nFrames 
    writeVideo(v, snapshot(mycam)); 
end 
close(v); 

同样未经检验的,因为我没有连接到此计算机的网络摄像头。但它适用于动画图形。请参阅doc readFrame以读取帧的方式

+0

感谢您的回复 – prithvi17