在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
在训练满足自己需求的检测器之前,需要对自己的图像数据进行处理,也就是勾出图片中要检测的目标。这个过程貌似有点麻烦,还好,不用担心,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
相关文章推荐
- 目标检测(从样本处理到训练检测)训练级联分类器
- 使用opnalpr训练目标检测级联分类器
- 目标检测(从样本处理到训练检测)训练级联分类器
- Opencv目标检测之级联分类器训练与测试
- Matlab在训练神经网络时既未达到迭代次数也未达到优化目标,但是突然停止
- 目标检测级联分类器训练
- YOLO V3 一步步训练自己的目标检测
- Matlab图像处理学习笔记(三):基于匹配的目标识别
- matlab训练神经网络时,使用mse(均方误差)以外的性能函数。
- 使用判别训练的部件模型进行目标检测 Object Detection with Discriminatively Trained Part Based Models
- 【Matlab制作目标切片脚本】
- SDM For Face Alignment 流程介绍及Matlab代码实现之训练篇
- matlab中libsvm训练的svm分类器model保存/读写文件接口
- matlab----标签图像分类训练工具
- Tensorflow训练自己的Object Detection模型并进行目标检测
- MATLAB求多目标优化问题——fminimax
- opencv训练级联分类器
- 目标跟踪:KCF代码分析(matlab版本)
- Faster Rcnn+ZF用自己的数据训练模型成功经验(matlab版)
- 新版Matlab中神经网络训练函数Newff的使用方法