MATLAB基于CascadeObjectDetector的人脸、眼睛、鼻子、嘴巴、上半身检测(9个模型)

%本文使用CascadeObjectDetector提供的官方Model实现对人脸的多个区域检测,
%当然detector也支持自定义模形(以xml形式加载)
img=imread('friend.jpg');
imshow(img);title('Original IMG');

detector = vision.CascadeObjectDetector;
% detector成员属性:
%(1) ClassificationModel(官方的分类器模型)
%
%   -'FrontalFaceCART'(默认值):  训练图片大小[20 20];检测直立和向前朝面的面.该模型由弱分类器组成,
%                                 基于分类和回归树分析 (cart)。这些分类器使用 haar 特征来编码面部特征。
%                                 基于块的分类器提供了在面部特征之间建立高阶依赖关系模型的能力。
%   -'FrontalFaceLBP':   训练图片大小[24 24];检测直立和向前朝面的面。该模型由弱分类器组成, 基于决策树桩。
%                         这些分类器使用本地二进制模式 (lbp) 来编码面部特征。lbp 功能可以提供抵御照明变化的鲁棒性。
%   -'UpperBody':   训练图片大小[18 22]:检测上身区域, 其定义为头部和肩部区域。此模型使用 haar 功能对头部和肩部区域的详细信息进行编码。
%                   因为它在头部周围使用了更多的功能, 所以这种模型对姿势变化 (例如头部旋转) 更可靠。
%   -'EyePairBig'/'EyePairSmall':  训练图片大小:[11 45]/[5 22];检测一双眼睛。'EyePairSmall'训练 "眼类" 模型。
%                                  这使得该模型能够检测到'EyePairBig'模型所能检测到的更小的眼睛。
%   -'LeftEye'/'RightEye':  训练图片大小:[12 18];分别检测左眼和右眼。这些模型是由弱分类器,
%                           基于决策树桩。这些分类器使用 haar 功能对详细信息进行编码。
%   -'LeftEyeCART'/'RightEyeCART':  训练图片大小:[20 20];分别检测左眼和右眼。构成这些模型的弱分类器是 cart-树。
%                                   与决策树桩相比, 基于 cart 树的分类器能够更好地建模高阶依赖关系。
%   -'ProfileFace':  训练图片大小:[20 20];检测直立面部轮廓。该模型由弱分类器组成, 基于决策树桩。
%                    这些分类器使用 haar 功能对人脸详细信息进行编码。
%   -'Mouth':  训练图片大小[15 25];检测口腔。该模型由弱分类器组成, 基于决策树桩, 利用 haar 特征对口腔细节进行编码。
%   -'Nose':  训练图片大小[15 18];该模型由弱分类器组成, 基于决策树桩, 使用 haar 特征对鼻子细节进行编码。
%
%(2) MinSize(最小可检测对象的大小):
%      最小可检测对象的大小, 指定为两个元素向量 [height weight]。对于包含对象的最小大小区域, 以像素为单位设置此属性。
%      该值必须大于或等于用于训练模型的图像大小。当您在处理图像之前知道最小对象大小时, 使用此属性可以减少计算时间。
%      如果不指定此属性的值, 探测器会将其设置为用于训练分类模型的图像的大小。
%
%(3) MaxSize(最大可检测对象的大小):
%     最大可检测对象的大小, 指定为两个元素向量 [height weight]。指定要检测的最大对象的大小 (以像素为单位)。
%     当您在处理图像之前知道最大对象大小时, 使用此属性可以减少计算时间。如果不为此属性指定值, 探测器会将其指定为size(IMG).
%
%(4) ScaleFactor(用于多尺度对象检测的缩放):
%     用于多尺度对象检测的缩放, 指定为大于1.0001的值。比例因子以增量MinSize缩放 minsize 和MaxSize之间的检测分辨率。
%     您可以使用以下方法将比例因子设置为理想值:  size(IMG)/(size(IMG)-0.5).
%     探测器使用以下关系以 "MinSize" 和MaxSize之间的增量缩放搜索区域:
%                        搜索区域= round(训练图片大小) * (ScaleFactor)^n))
%
%(5) MergeThreshold(检测阈值):
%     检测阈值, 指定为整数。阈值定义在对象周围有多个检测的区域中声明最终检测所需的条件。合并满足阈值的共置检测组,
%     以在目标对象周围生成一个边界框。通过要求在多尺度检测阶段多次检测目标对象, 提高此阈值可能有助于抑制错误检测。
%     将此属性设置为0, 将返回所有检测, 而不执行阈值或合并操作。此属性是可调整的。
%
%(6) UseROI(使用感兴趣的区域):
%     使用感兴趣的区域, 指定为true或false。将此属性设置为true可检测输入图像中感兴趣的矩形区域内的对象。 

%% 测试1:FrontalFaceCART
   bboxes=detector(img);
   FrontalFaceCART=insertObjectAnnotation(img,'rectangle',bboxes,'Face');
   figure(1);imshow(FrontalFaceCART);title('FrontalFaceCART');
   
%% 测试2:FrontalFaceLBP
    release(detector);
    detector.ClassificationModel='FrontalFaceLBP';
    bboxes=detector(img);
    FrontalFaceLBP=insertObjectAnnotation(img,'rectangle',bboxes,'Face');
    figure(2);imshow(FrontalFaceLBP);title('FrontalFaceLBP');
    
%% 测试3:UpperBody
    release(detector);
    detector.ClassificationModel='UpperBody';
    detector.MergeThreshold=3;%适当增加合并阈值
    bboxes=detector(img);
    UpperBody=insertObjectAnnotation(img,'rectangle',bboxes,'UpperBody');
    figure(3);imshow(UpperBody);title('UpperBody');
    detector.MergeThreshold=4;%恢复合并阈值
%% 测试4:EyePairBig/EyePairSmall
    release(detector);
    detector.ClassificationModel='EyePairBig';
    bboxes=detector(img);
    EyePairBig=insertObjectAnnotation(img,'rectangle',bboxes,'Eyes');
    figure(4);subplot(211);imshow(EyePairBig);title('EyePairBig');
    release(detector);
    detector.ClassificationModel='EyePairSmall';
    bboxes=detector(img);
    EyePairSmall=insertObjectAnnotation(img,'rectangle',bboxes,'Eyes');
    figure(4);subplot(212);imshow(EyePairSmall);title('EyePairSmall');
    
%% 测试5:LeftEye/RightEye   
    release(detector);
    detector.ClassificationModel='LeftEye';
    detector.MergeThreshold=10;%适当增加合并阈值
    bboxes=detector(img);
    LeftEye=insertObjectAnnotation(img,'rectangle',bboxes,'LeftEye');
    figure(5);subplot(211);imshow(LeftEye);title('LeftEye');
    release(detector);
    detector.ClassificationModel='RightEye';
    bboxes=detector(img);
    RightEye=insertObjectAnnotation(img,'rectangle',bboxes,'RightEye');
    figure(5);subplot(212);imshow(RightEye);title('RightEye');    
    detector.MergeThreshold=4;%恢复合并阈值
    
%% 测试6:LeftEyeCART/RightEyeCART   
    release(detector);
    detector.ClassificationModel='LeftEyeCART';
    bboxes=detector(img);
    LeftEyeCART=insertObjectAnnotation(img,'rectangle',bboxes,'LeftEye');
    figure(6);subplot(211);imshow(LeftEyeCART);title('LeftEye');
    release(detector);
    detector.ClassificationModel='RightEyeCART';
    bboxes=detector(img);
    RightEyeCART=insertObjectAnnotation(img,'rectangle',bboxes,'RightEye');
    figure(6);subplot(212);imshow(RightEyeCART);title('RightEye');   
    
%% 测试7:ProfileFace   
    release(detector);
    detector.ClassificationModel='ProfileFace';
    bboxes=detector(img);
    ProfileFace=insertObjectAnnotation(img,'rectangle',bboxes,'Face');
    figure(7);imshow(ProfileFace);title('ProfileFace');
    
%% 测试8:Mouth  
    release(detector);
    detector.ClassificationModel='Mouth';
    detector.MergeThreshold=100;%增加合并阈值
    bboxes=detector(img);
    Mouth=insertObjectAnnotation(img,'rectangle',bboxes,'Mouth');
    figure(8);imshow(Mouth);title('Mouth'); 
    detector.MergeThreshold=4;
    
%% 测试9:Nose
    release(detector);
    detector.ClassificationModel='Nose';
    detector.MergeThreshold=10;;%增加合并阈值
    bboxes=detector(img);
    Nose=insertObjectAnnotation(img,'rectangle',bboxes,'Nose');
    figure(9);imshow(Nose);title('Nose'); 

   

实验结果:

  -FrontalFaceCART:

 

-FrontalFaceLBP:

-UpperBody:

-EyePairBig/EyePairSmall:

-LeftEye/RightEye 

-LeftEyeCART/RightEyeCART

-ProfileFace

-Mouth 

-Nose

 

  • 23
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 15
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ephemeroptera

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值