安装驱动https://blog.csdn.net/zzx2016zzx/article/details/82725712
使用的是matlab2016a版本,摄像头驱动需提前下载安装
本例显示如何使用快照功能从USB摄像头获取实时图像。
用于USB摄像头的MATLAB®支持包可将任何符合USB视频类(UVC)的网络摄像头的实时图像带入MATLAB®。
识别可用的摄像头
webcamlist函数提供了MATLAB®可以访问的当前系统上的网络摄像头单元阵列。
camList = webcamlist
camList =
'Logitech HD Webcam C310'
设置连接到网络摄像头
网络摄像头对象表示MATLAB®和USB网络摄像头之间的连接。 要创建与网络摄像头的连接,请使用网络摄像头功能并指出要连接的摄像头。 您可以按照网络摄像头列表返回的名称或索引指定摄像头。 本示例使用“Logitech HD Webcam C310”相机。 建立连接后,可以使用点(。)表示法访问特定的属性值。
% Connect to the webcam.
cam = webcam(1)
cam =
webcam with properties:
Name: 'Logitech HD Webcam C310'
Resolution: '640x480'
AvailableResolutions: {1x19 cell}
Brightness: 128
Sharpness: 24
Saturation: 32
Gain: 17
Exposure: -4
WhiteBalance: 9930
Contrast: 32
ExposureMode: 'auto'
BacklightCompensation: 1
预览视频流
要打开“视频预览”窗口,请使用预览功能。 视频预览窗口显示设备的实时视频流。
preview(cam);
此处 略图
获取一个帧
要获取单个帧,请使用快照功能。
img = snapshot(cam);
% Display the frame in a figure window.
image(img);
获取多个帧
一个常见的任务是重复获取单个图像,处理它,然后存储结果。 为此,应该在循环中调用快照。
for idx = 1:5
img = snapshot(cam);
image(img);
end
清理
一旦不再需要连接,清除相关的变量。
clear cam
clear;
clc;
camList = webcamlist
cam = webcam(1)
preview(cam);
img = snapshot(cam);
% Display the frame in a figure window.
figure(111),image(img);
for idx = 1:5
figure(idx+1),
img = snapshot(cam);
image(img);
end
clear cam
本示例演示如何使用快照功能获取实时图像并将视频记录到磁盘。
用于USB摄像头的MATLAB®支持包可将任何符合USB视频类(UVC)的网络摄像头的实时图像带入MATLAB®。
设置连接到网络摄像头
使用网络摄像头功能创建与相机的连接。 本示例使用“Logitech HD Webcam C310”相机。
% Connect to the webcam.
cam = webcam
cam =
webcam with properties:
Name: 'Logitech HD Webcam C310'
Resolution: '640x480'
AvailableResolutions: {1x19 cell}
WhiteBalance: 0
Contrast: 32
Exposure: -6
BacklightCompensation: 1
Gain: 32
Brightness: 128
Saturation: 32
ExposureMode: 'auto'
Sharpness: 24
vidWriter = VideoWriter('frames.avi');
open(vidWriter);
获取和存储帧
以下循环将获取的帧写入指定的AVI文件以供将来处理。
for index = 1:20
% Acquire frame for processing
img = snapshot(cam);
% Write frame to video
writeVideo(vidWriter, img);
end
清理
一旦不再需要连接,清除相关的变量。
close(vidWriter);
clear cam
% Connect to the webcam.
cam = webcam
vidWriter = VideoWriter('frames.avi');
open(vidWriter);
for index = 1:20
% Acquire frame for processing
img = snapshot(cam);
% Write frame to video
writeVideo(vidWriter, img);
end
close(vidWriter);
clear cam