2017-05-27 47 views
0

我想提取面部圖像的所有面部地標,並將該圖像保存在我的目錄中。在我的情況下,我必須遵循這些步驟。人臉地標檢測算法,輸出圖像消失C++,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; 
    } 
} 

回答

1

您沒有拿着圖像窗口。一個簡單的方法做是

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

另外,我沒有看到你在哪裏輸出,你在問題中提到保存到當前目錄中的任何代碼。你可以看看imwrite。