面部标志检测算法,输出图像消失C ++,Dlib

时间:2017-05-27 06:29:46

标签: c++ opencv dlib

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

  1. 输入图片
  2. 提取面部投资回报率
  3. 然后使用Dlib提取面部地标
  4. 向图片添加叠加层
  5. 将该图片保存在我的目录中
  6. 我试过这个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;
        }
    } 
    

1 个答案:

答案 0 :(得分:1)

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

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

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