您的位置:首页 > 编程语言 > MATLAB

MATLAB之camera 入门操作

2014-11-02 20:30 176 查看
查了好些资料,本想写个简要的入门教程,却发现未能及之一二,故写简要说明,自赏之!

包含video参数初始化、预览video、获取图片和处理、保存视频!

%this code includes three parts:initial and preview the camera,snap the
%image and process,save the video
clc
close all
%part 1 initial the camera
vidobj = videoinput('winvideo',1,'YUY2_640x480');
triggerconfig(vidobj,'manual');
%part 2 preview the camera
set(vid,'ReturnedColorSpace','grayscale');  %set gray
set(vid,'ReturnedColorSpace','rgb');%set
preview(vidobj);%preview the camera
start(vidobj);
%part 3 snap the image and process
tic
for i = 1:100
snapshot = getsnapshot(vidobj);
frame = ycbcr2rgb(snapshot);
frame = rgb2gray(frame);
thd = graythresh(frame)*0.8;
frame = im2bw(frame,thd);
frame = ~frame;
imshow(frame);
end
elapsedTime = toc
timePerFrame = elapsedTime/1000
effectiveFrameRate = 1/timePerFrame
%part 4 save the video
filename = 'temp';
nframe = 120;
nrate = 30;%initial the condition
writerObj = VideoWriter( [filename '.avi'] );
writerObj.FrameRate = nrate ;
open(writerObj);
figure;
for ii = 1: nframe
frame = getsnapshot(vidobj);
imshow(frame);
f.cdata = frame;
f.colormap = [];
writeVideo(writerObj,f);
end
close(writerObj);
%part 5 end the video
stop(vidobj);
delete(vidobj);
disp('end');
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  matlab video