您的位置:首页 > 编程语言 > MATLAB

在Matlab下训练级联目标检测器

2017-10-24 14:51 591 查看
在Matlab下,vision.CascadeObjectDetector可以用来进行目标检测,Matlab系统中自带了一些已经训练好的分类器用于目标检测,目前比较成熟的检测器有人脸检测,鼻子检测,人眼检测和上半身检测。但是仅仅是这些检测器是不能应用于其它领域的。还好,Matlab提供了trainCascadeObjectDetector函数,可根据自已的需求重新训练一个检测器,用于目标检测。基本的流程如下:



在训练满足自己需求的检测器之前,需要对自己的图像数据进行处理,也就是勾出图片中要检测的目标。这个过程貌似有点麻烦,还好,不用担心,Matlab自带有这样的工具,这个工具就是Training Image Labeler。怎样打开这个工具呢?如下图所示:





然后点击Add Images,便可把图片载入到当前界面,然后用框图勾出你要检测的目标,勾出的框可以多个可以重叠,完成后,直接点击Save Session或Export  ROIs,保存即可。保存的结果是一个结构体,域名field names分别为imageFilename和objectBoundingBoxes,分别保存了图像的存储路径与图像待检测目标的ROI坐标[x y width height]。这一步处理完成后,相当于训练集处理完成,接下来便是用它来训练检测器。

怎样训练检测器呢?前边已经说啦,提到过一个函数,那就是trainCascadeObjectDetector函数,它是Matlab的built-in函数。这个函数有很多输入参数,但我们只需输入三个参数即可,其他的都有默认值。目前,该函数支持Haar,LBP,HOG特征检测器,默认为HOG目标检测器。

trainCascadeObjectDetector(outputXMLFileName, positiveInstances, negativeImages)

其中,outputXMLFileName为训练好的检测器保存文件,是XML文件,positiveInstances为正样本,也就是图片中含有待检测的目标,它也就是前边那一步处理后,Save Session或Export ROIs时保存的结构体,它是一个矩阵文件。negativeImages为负样本,也就是图片中不含有待检测的目标,通常为背景图片。同正样本一样,negativeImages为负样本的存储路径。具体详情请参考

help trainCascadeObjectDetector
下面是Matlab自带的一个训练检测器到检测的示例:

%Example - Training a stop sign detector

% Load the positive samples data from a mat file
% The file names and bounding boxes are contained in an array
% of structures named 'data'. The samples were created using
% trainingImageLabeler app.
load('stopSigns.mat');

% Add the image directory to the MATLAB path
imDir = fullfile(matlabroot, 'toolbox', 'vision', 'visiondata',...
'stopSignImages');
addpath(imDir);

% Specify folder with negative images
negativeFolder = fullfile(matlabroot, 'toolbox', 'vision', ...
'visiondata', 'nonStopSigns');

% Train a cascade object detector called 'stopSignDetector.xml'
% using HOG features.
% NOTE: The command below can take several minutes to run
trainCascadeObjectDetector('stopSignDetector.xml', data, ...
negativeFolder, 'FalseAlarmRate', 0.1, 'NumCascadeStages', 5);

% Use the newly trained classifier to detect a stop sign in an image
detector = vision.CascadeObjectDetector('stopSignDetector.xml');

img = imread('stopSignTest.jpg'); % read the test image

bbox = step(detector, img); % detect a stop sign

% Insert bounding box rectangles and return marked image
detectedImg = insertObjectAnnotation(img, 'rectangle', bbox, 'stop sign');

figure; imshow(detectedImg); % display the detected stop sign

rmpath(imDir); % remove the image directory from the path

实验结果:



参考:

1.
Train a Cascade Object Detector
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息