----------------------------邊學邊寫邊學習-------------------------------------
版本: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幀,下面放圖。

