matlab -读取摄像头保存视频文件

 

matlab 读取摄像头并保存avi视频文件

代码:

%%
%打开摄像头预览窗口
clc  
clear all
close all
% objects = imaqfind %find video input objects in memory
% delete(objects) %delete a video input object from memory
vid = videoinput('winvideo', 1, 'YUY2_640x480');%创建ID为1的摄像头的视频对象,视频格式是 YUY2_640x480,这表示视频的分辨率为640x480。  
set(vid,'ReturnedColorSpace','rgb');  %设置色彩为rgb/grayscale
%set(vid, 'ReturnedColorSpace', 'grayscale');
%%
filename = 'test1';       %保存视频的名字  
nframe = 300;            %视频的帧数  
nrate = 30;              %每秒的帧数  
preview(vid);              
set(1,'visible','off');  
writerObj = VideoWriter( [filename '.avi'] );  
writerObj.FrameRate = nrate;    
open(writerObj);    
figure;  
for i = 1:nframe  
    frame = getsnapshot(vid);  
    imshow(frame);  
    f.cdata = frame;  
    f.colormap = colormap([]) ;  
    writeVideo(writerObj,f);  
end  
close(writerObj);  
closepreview  

当出现以下错误时:

winvideo: The device associated with device ID 1 is already in use.  A new videoinput object cannot be created for this device
while it is in use.

添加这两句代码

% objects = imaqfind %find video input objects in memory
% delete(objects) %delete a video input object from memory

这样就保存好了一个test1.avi视频文件 。

 

 

 

 

你可能感兴趣的:(matlab摄像头)