matlab 调用dlib进行人脸检测

 

1:http://dlib.net/下载最新版本dlib源码,并解压

2:https://cmake.org/download/下载安装cmake工具

3:修改cmake_mex_wrapper文件

 

4:修改cmakelist.txt文件

 

5:使用cmake对dlib源码进行编译

 

6:添加库目录 D:\MATLAB\R2016b\extern\lib\win64\microsoft

 

7:添加附加库 libut.lib libmat.lib

 

:8:添加命令行参数/EXPORT:mexFunction

 

9:设置LandMarkDetectorDlib.mexw64所在路径

 

10:人脸检测代码

#include "dlib/matrix.h"
#include "dlib/image_processing/frontal_face_detector.h"
#include "dlib/image_processing/render_face_detections.h"
#include "dlib/image_processing.h"
using namespace dlib;
using namespace std;


/*!
    This file defines a function callable from MATLAB once you mex it. 

    It computes the same thing as the following MATLAB function:

        function [A, B] = example_mex_function(x, y, some_number)
            A = x+y;
            B = sum(sum(x+y));
            disp(['some_number: ' num2str(some_number)])
        end


    VALID INPUT AND OUTPUT ARGUMENTS
        The mex wrapper can handle the following kinds of input and output arguments:
            - Types corresponding to a MATLAB matrix
                - a dlib::matrix containing any kind of scalar value.
                - a dlib::array2d containing any kind of scalar value.
                - a dlib::vector containing any kind of scalar value.
                - a dlib::point
                - matrix_colmajor or fmatrix_colmajor
                  These are just typedefs for matrix containing double or float and using a
                  column major memory layout.  However, they have the special distinction
                  of being fast to use in mex files since they sit directly on top of
                  MATLAB's built in matrices.  That is, while other types of arguments copy
                  a MATLAB object into themselves, the matrix_colmajor and fmatrix_colmajor
                  do no such copy and are effectively zero overhead methods for working on
                  MATLAB's matrices.

            - RGB color images
                - dlib::array2d<dlib::rgb_pixel> can be used to represent 
                  MATLAB uint8 MxNx3 images.

            - Types corresponding to a MATLAB scalar
                - any kind of scalar value, e.g. double, int, etc.

            - Types corresponding to a MATLAB string 
                - std::string 
        
            - Types corresponding to a MATLAB cell array
                - a std::vector or dlib::array containing any of the above 
                  types of objects or std::vector or dlib::array objects.

            - matlab_struct and matlab_object.  These are special types defined in the
              call_matlab.h file and correspond to matlab structs and arbitrary matlab
              objects respectively.
!*/


// You can also define default values for your input arguments.  So
// here we say that if the user in MATLAB doesn't provide the "some_number" 
// then it will get a value of 3.141.  
#define ARG_5_DEFAULT 3.141

// Make a function named mex_function() and put your code inside it.
// Note that the return type should be void.  Use non-const reference
// arguments to return outputs.  Finally, mex_function() must have no
// more than 20 arguments.
void mex_function (
    const array2d<rgb_pixel>& img,
    matrix<double,1,4>& o_bboxes,
    matrix<double,68,2>& o_landmarks,
    int somenumber0=0,
    int somenumber=1

{
    static frontal_face_detector detector = get_frontal_face_detector();
    static int a = 1;
    static shape_predictor sp;
    if (a == 1)
    {
        cout << "初始化:mexw64需与shape_predictor_68_face_landmarks.dat路径一致" << endl;
        deserialize("shape_predictor_68_face_landmarks.dat") >> sp;
    }
    a++;
    //cout << a << endl;
    std::vector<rectangle> dets = detector(img);
    std::vector<full_object_detection> shapes;
    int maxFaceID = -1;
    int maxFaceArea = 0;
    for (unsigned long j = 0; j < dets.size(); ++j)
    {
        int curArea = dets[j].area();
        if (maxFaceArea < curArea)
        {
            maxFaceID = j;
            maxFaceArea = curArea;
        }
    }

    if (maxFaceID < 0)
        return;

    full_object_detection shape = sp(img, dets[maxFaceID]);
    //cout << "number of parts: " << shape.num_parts() << endl;

    o_bboxes(0, 0) = dets[maxFaceID].left();
    o_bboxes(0, 1) = dets[maxFaceID].top();
    o_bboxes(0, 2) = dets[maxFaceID].right();
    o_bboxes(0, 3) = dets[maxFaceID].bottom();
    for (int id = 0; id < shape.num_parts(); id++)
    {
        o_landmarks(id, 0) = shape.part(id).x();
        o_landmarks(id, 1) = shape.part(id).y();
    }
    somenumber = 1;
}

// #including this brings in all the mex boiler plate needed by MATLAB.
#include "mex_wrapper.cpp"
 

 

11: 试用

img = imread('faceExample.jpg');
[rect landmark] = LandMarkDetectorDlib(img,1);
figure,idisp(img)
hold on,plot(landmark(:,1),landmark(:,2),'r*')

 

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值