CascadeClassifier openCV

时间:2016-10-14 04:22:43

标签: c++ opencv c++11

我用C ++编写了这个程序,使用OpenCV来检测我拥有的pedestrians.xml文件。程序应读取所有输入图像,并在行人所在的输出图像上显示蓝色矩形。但是代码给了我错误。有谁能告诉我为什么会出现这些错误?

#include <opencv2/core/core.hpp> 
#include <opencv2/highgui/highgui.hpp> 
#include <opencv2/imgproc/imgproc.hpp> 
#include <opencv2/features2d/features2d.hpp>
#include<iostream>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;
using namespace cv;

void detectAndDisplay( Mat frame );

 /** Global variables */
 String pedestrians_name = "hogcascade_pedestrians.xml";
 //CascadeClassifier pedestrians;
 string window_name = "Capture - pedestrians detection";
 RNG rng(12345);

 /** @function main */
 int main( int argc, const char** argv )
 {
   CvCapture* capture;
   Mat frame;

   //-- 1. Load the cascades
   //if( !pedestrians.load( pedestrians_name ) ){ printf("--(!)Error loading\n"); return -1; };
    Mat image = imread("ped1.jpg"); 
    Mat image_keypoints;  
    cvtColor(image, image_keypoints, CV_BGR2GRAY);

   return 0;
 }

/** @function detectAndDisplay */
void detectAndDisplay( Mat frame )
{
  std::vector<Rect> pedestrians;
  Mat frame_gray;

  cvtColor( frame, frame_gray, CV_BGR2GRAY );
  equalizeHist( frame_gray, frame_gray );

  //-- Detect faces
  pedestrians.detectMultiScale( frame_gray, pedestrians, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );
  void CascadeClassifier::detectMultiScale(const Mat& image, vector<Rect>& objects, double scaleFactor=1.1, int minNeighbors=3, int flags=0, Size minSize=Size(), Size maxSize=Size());

  for( int i = 0; i < pedestrians.size(); i++ )
  {
    Point center( pedestrians[i].x + pedestrians[i].width*0.5, pedestrians[i].y + pedestrians[i].height*0.5 );
    ellipse( frame, center, Size( pedestrians[i].width*0.5, pedestrians[i].height*0.5), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 );

    Mat faceROI = frame_gray( pedestrians[i] );
    std::vector<Rect> eyes;

    //-- In each face, detect eyes
    /*eyes_cascade.detectMultiScale( faceROI, eyes, 1.1, 2, 0 |CV_HAAR_SCALE_IMAGE, Size(30, 30) );

    for( size_t j = 0; j < eyes.size(); j++ )
     {
       Point center( faces[i].x + eyes[j].x + eyes[j].width*0.5, faces[i].y + eyes[j].y + eyes[j].height*0.5 );
       int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 );
       circle( frame, center, radius, Scalar( 255, 0, 0 ), 4, 8, 0 );
     }*/
  }
  //-- Show what you got
  imshow( window_name, frame );
 }

这些是错误:

cascade.cpp: In function âvoid detectAndDisplay(cv::Mat)â:
cascade.cpp:46: error: âclass std::vector<cv::Rect_<int>, std::allocator<cv::Rect_<int> > >â has no member named âdetectMultiScaleâ
cascade.cpp:46: error: âCV_HAAR_SCALE_IMAGEâ was not declared in this scope
cascade.cpp:47: error: âCascadeClassifierâ has not been declared
cascade.cpp:47: error: invalid use of qualified-name â<declaration error>::detectMultiScaleâ

3 个答案:

答案 0 :(得分:0)

您需要添加opencv_objdetect库。并包括&#34; opencv2 / objdetect.hpp&#34;。

答案 1 :(得分:0)

创建一个Cascade分类器对象并加载xml文件obj.load(xml文件路径)

答案 2 :(得分:0)

首先,在main()中你从未调用过detectAndDisplay()吗?如果你想执行它,你需要调用它......

其次,在使用detectMultiScale之前,需要在detectAndDisplay()中声明并加载级联分类器。

CascadeClassifier pedestrians;
if( !pedestrians.load( pedestrians_name ) ){
printf("--(!)Error loading\n");
return;}
//.....
pedestrians.detectMultiScale( frame_gray, pedestrians, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );