----------------------------边学边写边学习-------------------------------------
版本:2014a
调用摄像头
a = imaqhwinfo
如果出现下面的警告说明你没安装扩展工具。
警告: No Image Acquisition adaptors found. Image acquisition adaptors may be available as downloadable support packages. Open Support Package Installer to install additional vendors.
这时候Support Package Installer在MATLAB里面有下划线,然后你点开它,MATLAB会提供大概13个软件包,这时候选择OS Generic Video Interface下载安装就OK了 (要求注册账号,随便用个邮箱注册下就可以了,不需要付费)。
(matlab查看摄像头详细信息 请看 https://blog.csdn.net/hmg25/article/details/4126122 )
下面就是调用笔记本电脑摄像头并打开图像
(%如果使用USB摄像头,一般为2,笔记本自带摄像头为1)
vidDevice = imaq.VideoDevice(\'winvideo\', 1, \'YUY2_640x480\', ... \'ROI\', [1 1 640 480], ... \'ReturnedColorSpace\', \'rgb\' );
preview(vidDevice);
人脸检测我们用的是matlab的机器视觉工具箱(瞬间觉得matlab真心强大)
VJ算法的目的是检测人脸,但是其思想同样可以用于检测其他物体,只需进行训练即可。
VJ算法在Matlab里面实现的时候,已经训练好了正脸、侧脸、上半身、眼睛、嘴、鼻子,这些都是可以直接检测,不需训练,直接调用CascadeObjectDetector函数即可。
下面是检测人脸和上半身的例子
% Example 1: Face detection % ---------------------------- faceDetector = vision.CascadeObjectDetector; % Default: finds faces I = imread(\'visionteam.jpg\'); bboxes = step(faceDetector, I); % Detect faces % Annotate detected faces IFaces = insertObjectAnnotation(I, \'rectangle\', bboxes, \'Face\'); figure, imshow(IFaces), title(\'Detected faces\'); % Example 2: Upper body detection % -------------------------------------- bodyDetector = vision.CascadeObjectDetector(\'UpperBody\'); bodyDetector.MinSize = [60 60]; bodyDetector.MergeThreshold = 10; I2 = imread(\'visionteam.jpg\'); bboxBody = step(bodyDetector, I2); % Detect upper bodies % Annotate detected upper bodies IBody = insertObjectAnnotation(I2, \'rectangle\', ... bboxBody, \'Upper Body\'); figure, imshow(IBody), title(\'Detected upper bodies\');
至于调用摄像头进行人脸识别,肯定是 调用摄像头的过程中对每一帧图像分别进行识别,然后再在图像中框出来。
这就要求 速度 要足够快。所以检测的时候就要压缩你图像的像素了。
下面放代码
faceDetector = vision.CascadeObjectDetector(); %enable viola jones algorithm bbox = [100 100 100 100]; vidDevice = imaq.VideoDevice(\'winvideo\', 1, \'YUY2_640x480\', ... \'ROI\', [1 1 640 480], ... \'ReturnedColorSpace\', \'rgb\' ); %set(vidDevice.DeviceProperties, \'FrameRate\', \'30\'); boxInserter = vision.ShapeInserter(\'BorderColor\',\'Custom\',... \'CustomBorderColor\',[255 255 0]); textInserter = vision.TextInserter(\'%d\',\'LocationSource\',\'Input port\',\'Color\',[255,255, 0],\'FontSize\',12); nFrame =300; vidInfo = imaqhwinfo(vidDevice); vidHeight = vidInfo.MaxHeight; vidWidth = vidInfo.MaxWidth; videoPlayer = vision.VideoPlayer(\'Position\',[300 100 640+30 480+30]); for k = 1:nFrame % start recording with 300 frames %tic; % timer start videoFrame = step(vidDevice); % enable the image capture by webcam bbox = 4 * faceDetector.step(imresize(videoFrame, 1/4)); % boost video\'s fps videoOut = step(boxInserter, videoFrame, bbox); % highlight the boxes of face at video %release(boxInserter); step(videoPlayer, videoOut); % display the video live in video player end
一共执行了300帧,下面放图。
请发表评论