%% 读取摄像头
%法一:
imaqhwinfo
imaqhwinfo('winvideo')
%imaqhwinfo
src = videoinput( 'winvideo', 1 );%由imaqhwinfo('winvideo')查看到DeviceIDs为1
figure( 'Name', '摄像头:' );
uicontrol( 'String', 'Close', 'Callback', 'close(gcf)' );%gcf为current figure handle
%即为当前图像句柄
vidRes = get( src, 'VideoResolution' );%返回分辨率,即摄像头数据的宽和高
nBands = get( src, 'NumberOfBands' );%返回摄像头数据的通道数,我这里返回的是3
hImage = image( zeros(vidRes(2), vidRes(1), nBands) );
preview(src, hImage);%以hImage的尺寸格式显示摄像头数据
%% 读取摄像头
%法二:
%imaqhwinfo
src = videoinput( 'winvideo', 1 );%此处第二个参数1可以省略
preview( src );%以默认格式显示数据
法三:
imaqInfo = imaqhwinfo;
imaqInfo.InstalledAdaptors
winvideoinfo = imaqhwinfo('winvideo');
device1 = winvideoinfo.DeviceInfo; %或者device1 = winvideoinfo.DeviceInfo(1)
device1.SupportedFormats
% Access an image acquisition device.
vidobj = videoinput('winvideo',1,'YUY2_640x480');
sources = vidobj.Source;
whos sources
set(vidobj,'SelectedSourceName','input1');
sources;
selectedsrc = getselectedsource(vidobj);
get(selectedsrc);
delete(vidobj);
clear vidobj;
法四:
clc;
clf;
clear all;
imaqmem(30000000); %申请内存空间
%ADAPTOR:MATLAB与视频设备之间的接口,主要的目的是传递信息
vid = videoinput('winvideo', 1, 'YUY2_640x480');
preview(vid);
start(vid);
h=figure('NumberTitle','off','Name','视频', 'MenuBar','none','color','c','Position', [0, 0, 1, 1], 'Visible', 'on'); %新建窗口
set(h,'doublebuffer','on','outerposition',get(0,'screensize'));
h1=axes('Position', [0.02, 0.1, 0.4, 0.8],'Parent',h); %新建显示窗口
hold on;
axis off;
while ishandle(h) %判断是否有效的图像对象句柄
a=getsnapshot (vid); % 捕获图像
flushdata(vid); %清除数据获取引擎的所有数据、置属性SamplesAvailable为0
imshow(a); %显示图像
drawnow; % 实时更新图像
end;
delete(vid);
法五:
vid=videoinput('winvideo',1,'YUY2_640x480');%语法:vid=videoinput(adaptorname,deviceID,format)
set(vid,'TriggerRepeat',Inf);%TriggerRepeat重复触发
set(vid,'FramesPerTrigger',1);%FramesPerTrigger每特定的帧数去触发捕获选定使用的视频源
set(vid,'FrameGrabInterval',1);%FrameGrabInterval帧的抓取时间间隔
%设置返回色彩 rgb正常颜色(YUY2格式颜色发红),grayscale是灰度
set(vid,'ReturnedColorSpace','rgb');
set(vid,'ReturnedColorSpace','grayscale');
%获取分辨率,色彩数目等参数
vidRes=get(vid,'VideoResolution');%视频分辨率
nBands=get(vid,'NumberOfBands');%色彩数目
hImage=image(zeros(vidRes(2),vidRes(1),nBands));
% himage:视频预览窗口对应的句柄,也就是说在指定的句柄对象中预览视频,该参数可以空缺。至于预览窗口的关闭和停止可以使用colsepreview和stoppreview函数
preview(vid,hImage);
法六:
vid=videoinput('winvideo',1,'YUY2_640x480');%语法:vid=videoinput(adaptorname,deviceID,format)
get(vid)%使用get函数查看视频输入对象的属性
get(getselectedsource(vid))%查看当前输入对象的源对象信息
% 设置输入对象属性方式
set(vid,'TriggerRepeat',Inf);
vid.FrameGrabInterval = 5;
% 设置源对象属性方式
vid_src = getselectedsource(vid);
set(vid_src,'Tag','motion detection setup');
figure; % Ensure smooth display
set(gcf,'doublebuffer','on'); % 用于是图像平滑
start(vid) % 启动对像
while(vid.FramesAcquired<=100)
data = getdata(vid,2); % 获取两帧图像数据
diff_im = imabsdiff(data(:,:,:,1),data(:,:,:,2)); % 两帧相减
imshow(diff_im);
end
stop(vid)
%清除工作
delete(vid)
clear
close(gcf)
发表评论 取消回复