【MATLAB与机器学习】MATLAB访问本机摄像头

GerwelsJI 2020-06-09

% MATLAB控制摄像头操作

% 获取本机中已经安装的硬件资源列表
imaqhwinfo

% 建立videoinput对象
obj = videoinput(‘winvideo‘);

% 设置属性
set(obj, ‘FramesPerTrigger‘, 1);
set(obj, ‘TriggerRepeat‘, Inf);

% 建立界面
hf = figure(‘Units‘, ‘Normalized‘, ‘Menubar‘, ‘None‘, ...
    ‘NumberTitle‘, ‘off‘, ‘Name‘, ‘演示拍照系统‘);
ha = axes(‘Parent‘, hf, ‘Units‘, ‘Normalized‘, ...
    ‘Position‘, [.05 .2 .85 .7]);
axis off
hb1 = uicontrol(‘Parent‘, hf, ‘Units‘, ‘Normalized‘, ...
    ‘Position‘, [.25 .05 .2 .1], ‘String‘, ‘预览‘, ...
    ‘Callback‘, ...
    [‘objRes = get(obj, ‘‘VideoResolution‘‘);‘ ...
     ‘nBands = get(obj, ‘‘NumberOfBands‘‘);‘ ...
     ‘hImage = image(zeros(objRes(2), objRes(1), nBands));‘ ...
     ‘preview(obj, hImage);‘]);
hb2 = uicontrol(‘Parent‘, hf, ‘Units‘, ‘Normalized‘, ...
    ‘Position‘, [.55 .05 .2 .1], ‘String‘, ‘拍照‘, ...
    ‘Callback‘, ‘imwrite(getsnapshot(obj), ‘‘im.jpg‘‘)‘);

相关推荐