c++ - Face Landmark Detection算法,输出图像消失C++,Dlib

标签 c++ opencv dlib

我正在尝试提取面部图像的所有面部标志并将该图像保存在我的目录中。就我而言,我必须遵循这些步骤。

  1. 输入图片
  2. 提取人脸ROI
  3. 然后使用Dlib提取人脸关键点
  4. 为图片添加叠加层
  5. 将该图像保存在我的目录中

我试过这个 C++ 代码,它提供了我需要的输出。但在显示叠加图像后,它很快就消失了。谁能告诉我这段代码有什么问题。

#include <dlib\opencv\cv_image.h>
#include <opencv2/highgui/highgui.hpp>
#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/image_processing/render_face_detections.h>
#include <dlib/image_processing.h>
#include <dlib/gui_widgets.h>

using namespace dlib;
using namespace std;
using namespace cv;

int main()
{
    try
    {

        cv::Mat src1;
        src1 = imread("obama.jpg", CV_LOAD_IMAGE_COLOR);

        //image_window win, win_faces;
        image_window win;
        Mat atom_image = Mat::zeros(500, 500, CV_8UC3);


        // Load face detection and pose estimation models.
        frontal_face_detector detector = get_frontal_face_detector();
        shape_predictor pose_model;
        deserialize("shape_predictor_68_face_landmarks.dat") >> pose_model;

            //cv::Mat temp;
            //temp = src1;

            cv_image<bgr_pixel> cimg(src1);
            cv_image<bgr_pixel> black(atom_image);
            // Detect faces 
            std::vector<rectangle> faces = detector(cimg);

            cout << "Number of faces detected: " << faces.size() << endl;

            // Find the pose of each face.
            std::vector<full_object_detection> shapes;
            //for (int i = 0; i < faces.size(); i++) {
                //shapes.push_back(pose_model(cimg, faces[i]));


                full_object_detection shape = pose_model(cimg, faces[0]);


                shapes.push_back(pose_model(cimg, faces[0]));


                //const full_object_detection& d = shapes[0];
                //ofstream outputfile;
                //outputfile.open("data1.txt");


                cout << "pixel position of first part:  " << shape.part(2) << endl;

            // Display it all on the screen
            //win.clear_overlay();
            win.set_image(cimg);
            win.add_overlay(render_face_detections(shapes));

    }
    catch (serialization_error& e)
    {
        cout << "You need dlib's default face landmarking model file to run this example." << endl;
        cout << "You can get it from the following URL: " << endl;
        cout << "   http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2" << endl;
        cout << endl << e.what() << endl;
    }
    catch (exception& e)
    {
        cout << e.what() << endl;
    }
} 

最佳答案

您没有拿着图像窗口。一个简单的方法是

win.set_image(cimg);
win.add_overlay(render_face_detections(shapes));
waitKey(0);

此外,我没有看到您在问题中提到的将输出保存到当前目录的任何代码。你可以看看 imwrite。

关于c++ - Face Landmark Detection算法,输出图像消失C++,Dlib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44213746/

相关文章:

c++ - 在 Matlab 中调用 c++ 例程

c++ - 删除从 C++ 中的 void 强制转换的指针是否安全?

接受派生类对象的派生类中的 C++ Enforce 函数

c - 当我使用 OpenMP(C/C++) 运行我的代码 OpenCV 代码时,出现 fatal error

android - 打开 CV 示例提示以下载 OpenCV 管理器

c++ - 更改后如何将默认名称设置为窗口?

c++ - 如何将 dlib 作为我的项目的一部分包含在 CLion 中?

C++:Libcurl curl_easy_init() 给出访问冲突错误并使程序崩溃

python - dlib 不使用 CUDA

c++ - dlib 19.6 多分类器训练数据