注:本文非作者原创,主要内容均来自网络,但其正确性已经本博作者验证。
1.整个过程需要做如下几件事情:1)查询USB2.0Camera
的具体参数(imaqhwinfo)
2)创建视频输入对象(videoinput)
3)图像预览和显示(preview、stoppreview、closepreview和image)
4)获取视频图像(getsnapshot)
5)图像获取设备的获取和设置(get和set)
6)关闭视频对象(delete)
2.图像获取工具箱术语:图像获取设备:比如摄像头、扫描仪图像获取适配器:主要的目的是通过驱动在Matlab和图像获取设备之间传递信息ROI:region-of-interest
感兴趣区域3.常用函数1)getselectedsource
2)imaqfind
3)isvalid
4)peekdata
5)getdata
6)imaqmontage
4.查询USB2.0Camera
的具体参数(imaqhwinfo)
>>
info=imaqhwinfoinfo =
InstalledAdaptors:
{'coreco' 'winvideo'}
MATLABVersion:
'7.10(R2010a)'
ToolboxName: 'Image Acquisition Toolbox'
ToolboxVersion: '3.5(R2010a)'
>>
win_info=imaqhwinfo('winvideo')win_info =
AdaptorDllName:'D:Program
FilesMATLABR2010Atooboximaqadaptorswin32mwwinvideoimaq.dll'AdaptorDllVersion: '3.5 (R2010a)'AdaptorName: 'winvideo'DeviceIDs:
{[1]}
DeviceInfo: [1x1
struct]
> win_info.DeviceIDs
ans =
[1]
>> dev_win_info=win_info.DeviceInfo
dev_win_info =
DefaultFormat: 'YUV2_160x120' DeviceFileSupported: 0
DeviceName:
'USB 口 口 口 口'DeviceID:
1ObjectConstructor: 'videoinput('winvideo',
1)'SupportedFormats: {'YUV2_160x120' 'YUV2_176x144'
'YUV2_320x240' 'YUV2_352x288'
'YUV2_640x480'}>> dev_win_info.SupportedFormats
ans='YUV2_160x120'
'YUV2_176x144' 'YUV2_320x240' 'YUV2_352x288'
'YUV2_640x480'5.视频预览、采集和保存(1)创建视频输入对象 obj =
videoinput(adaptorname,deviceID,format)adaptorname:适配器名称,首次可以使用不带参数的imaqhwinfo函数获取deviceID:设备ID号,首次可以通过imaqhwinfo函数获取format:视频采集格式,可以通过DeviceInfo的SupportedFormats获取,不填写则使用默认格式>> obj =
videoinput('winvideo',1,'YUV2_640x480')Summary of Video Input Object Using
'USB 口口口口'.
Acquisition
Source(s): input1 is
available.
Acquisition
Parameters: 'input1' is the
current selected source.
10 frames
per trigger using the selected source.
'YUV2_640x480'video data to be logged upon
START.
Grabbing
first of every 1 frame(s).
Log data to
'memory' on trigger.
Trigger
Parameters: 1 'immediate'
trigger(s) on START.
Status: Waiting for START.
0 frames
acquired since starting.
0 frames
available for GETDATA.
(2)打开视频预览窗口
himage=preview(obj,himage)obj:视频采集对象himage:视频预览窗口对应的句柄,也就是说在指定的句柄对象中预览视频,该参数可以空缺。至于预览窗口的关闭和停止可以使用colsepreview和stoppreview函数。下述代码可以产生预览:vidRes =
get(obj, 'VideoResolution');
nBands = get(obj,
'NumberOfBands');
figure()%指定预览窗体显示的figure
axes()%指定预览窗口显示的坐标系hImage = image( zeros(vidRes(2), vidRes(1), nBands) );
preview(obj,
hImage);
(3)图像捕捉、显示和保存
frame =
getsnapshot(obj);
imshow(frame);
imwrite(fame,'snap.jpg','jpg');
注:frame是H×W×B的矩阵。H图像高度,由ROIPosition指定;w图像宽度,由ROIPosition指定;B索线个数,由NumberOfBands指定(4)最简单采集实验,输入如下代码,可以得到预览下的默认格式的摄像头捕捉窗口
obj=videoinput('winvideo',1);
preview(obj);
(5)输入如下代码可返回视频信息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;
返回信息Name Size Bytes Class
sources 1x1 726 videosource object
Grand total is 30 elements using 726 bytes
Display Summary for Video Source Object:
Index: SourceName: Selected:
1 'input1' 'on'
General Settings:
Parent = [1x1 videoinput]
Selected = on
SourceName = input1Tag =
Type = videosource
Device Specific Properties:
BacklightCompensation = on
Brightness = -16
Contrast = 120
FrameRate = 30.0000
Gamma = 60
Hue = 0
Saturation = 40
Sharpness = 3
(6)输入如下代码
clc;
clf;
clear all;
imaqmem(30000000); %申请内存空间
�APTOR: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);