getDefaultPeopleDetector()的HOGDescriptor返回空向量在opencv

getDefaultPeopleDetector() of HOGDescriptor returns null vector in opencv

本文关键字:向量 opencv 返回 HOGDescriptor getDefaultPeopleDetector      更新时间:2023-10-16

这里我试图检测视频帧中的人。我正在使用HOGDescriptor和SVM。但是getDefaultPeopleDetector()返回一个空向量。我不明白为什么?所以,如果有人能提出一个解决方案。下面是代码:

    int main()
       {
         VideoCapture cap(0);
         if(!cap.isOpened())
            {
               cout<<"Cannot open Camera";
               system("pause");
               return -1;
            } 
         cout<<"Camera Open";
         cvNamedWindow("rahul",CV_WINDOW_AUTOSIZE);
         HOGDescriptor hog;
         static vector <float>    detector=HOGDescriptor::getDefaultPeopleDetector();
        if(!detector.size());
          {
            cout<<"No detector";
            system("pause");
             return -1;
           }
         hog.setSVMDetector(detector);
         while(1)
          {
            Mat frame;
            bool status=cap.read(frame);
            if(!status)
             {
               cout<<"cannot read frame";
                 break;
              }
            vector<Rect> found, found_filtered;
             hog.detectMultiScale(frame, found, 0, Size(8,8), Size(32,32), 1.05, 2);
            size_t i, j;
            for (i=0; i<found.size(); i++) 
              {
                  Rect r = found[i];
                  for (j=0; j<found.size(); j++) 
                      if (j!=i && (r & found[j]) == r)
                         break;
                      if (j== found.size())
                      found_filtered.push_back(r);
                }
             for (i=0; i<found_filtered.size(); i++) 
                  {
                     Rect r = found_filtered[i];
                     r.x += cvRound(r.width*0.1);
                     r.width = cvRound(r.width*0.8);
                     r.y += cvRound(r.height*0.07);
                     r.height = cvRound(r.height*0.8);
                     rectangle(frame, r.tl(), r.br(), Scalar(0,255,0), 3);        
                   } 
              imshow("rahul",frame);
              if(waitKey(30)==27)
                 {
                    cout<<"escape";
                    break;
                  }
          }

       return 0;
     }

根据我的代码,我总是在输出处得到"No detector",即detector.size()为null。

我算是解决了这个问题。所以,如果有人遇到这个问题,可以试试这个。

首先检查在objdetect/src中是否存在hog.cpp。它就在我的箱子里。如果它存在,那么在代码中包含hog.cpp和文件的完整路径。

当我包含这个文件时,它在precomp.hpp(包含在hog.hpp中)中给出了另一个错误。错误是找不到cvconfig.h。我检查了文件夹,它没有cvconfig.h。

所以,我在precomp.hpp中注释了#include "cvconfig.h"行,然后一切正常

我认为你只是需要修复

static vector <float>    detector=HOGDescriptor::getDefaultPeopleDetector();

static vector <float>    detector=HOGDescriptor::hog.getDefaultPeopleDetector();