【问题标题】:OpenCV How to get width and height of face from imageOpenCV如何从图像中获取人脸的宽度和高度
【发布时间】:2013-09-24 10:17:46
【问题描述】:

您好,我有大量从视频中提取的人脸图像。我想简单地测量图像中每张脸的宽度和高度。当我使用 HAAR 分类器时,它会返回检测到的方形脸,无论脸是胖还是瘦。

有一些改进是理想的,虽然我很高兴只是第一步。

至少我正在寻找示例来测量图像中检测到的人脸的宽度和高度。 理想情况下,这些人脸首先会被旋转为面朝前,以测量实际人脸的宽度和高度,而不是可能旋转的人脸的像素宽度。

示例代码将不胜感激。

【问题讨论】:

    标签: opencv face-recognition


    【解决方案1】:

    这是面部检测code。下面两行将打印检测到的人脸的宽度和高度。

    "detectAndDisplay" 的第一个 for 循环中添加这两行。

    cout << "face width = " << faces[i].width << endl;
    cout << "face Height = " << faces[i].height << endl;
    

    这里是修改后的代码:

    #include "opencv2/objdetect/objdetect.hpp"
     #include "opencv2/highgui/highgui.hpp"
     #include "opencv2/imgproc/imgproc.hpp"
    
     #include <iostream>
     #include <stdio.h>
    
     using namespace std;
     using namespace cv;
    
     /** Function Headers */
     void detectAndDisplay( Mat frame );
    
     /** Global variables */
     String face_cascade_name = "haarcascade_frontalface_alt.xml";
     CascadeClassifier face_cascade;
     string window_name = "Capture - Face detection";
     RNG rng(12345);
    
     /** @function main */
     int main( int argc, const char** argv )
     {
       CvCapture* capture;
       Mat frame = imread ( "C:\\Desktop\\1.jpg" );
    
       //-- 1. Load the cascades
       if( !face_cascade.load( face_cascade_name ) ){ printf("--(!)Error loading\n"); return -1; };
    
    
       //-- 3. Apply the classifier to the frame
           if( !frame.empty() )
           { detectAndDisplay( frame ); }
           else
           { printf(" --(!) No captured frame -- Break!"); }
    
       return 0;    
    
       }
    
    
    
    /** @function detectAndDisplay */
    void detectAndDisplay( Mat frame )
    {
      std::vector<Rect> faces;
      Mat frame_gray;
    
      cvtColor( frame, frame_gray, CV_BGR2GRAY );
      equalizeHist( frame_gray, frame_gray );
    
      //-- Detect faces
      face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );
    
      for( int i = 0; i < faces.size(); i++ )
      {
        cv :: rectangle ( frame, faces[i], Scalar( 255, 0, 255 ), 4 );
        cout << "face width = " << faces[i].width << endl;
        cout << "face Height = " << faces[i].height << endl;
    
      }
      //-- Show what you got
      imshow( window_name, frame );
    
      waitKey( 0 );
     }
    

    【讨论】:

    • 这不总是为您提供方脸结果吗?我需要边到边,从上到下巴。
    • 您可以修改此代码以满足您的要求。不要指望被勺子喂食
    猜你喜欢
    • 2016-01-03
    • 1970-01-01
    • 2019-05-20
    • 1970-01-01
    • 2011-05-31
    • 1970-01-01
    • 2014-07-15
    • 2015-09-04
    • 2010-11-18
    相关资源
    最近更新 更多