MATLAB之camera 入门操作

查了好些资料,本想写个简要的入门教程,却发现未能及之一二,故写简要说明,自赏之!

包含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');

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值