1

我刚刚将 HOG Descriptor 添加到我的对象检测代码中以跟踪行人。下面是我的代码:

#include"stdafx.h"
#include<vector>
#include<iostream>
#include<opencv2/opencv.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/objdetect/objdetect.hpp>

int main(int argc, char *argv[])
{
    cv::Mat frame;                                              
    cv::Mat fg;     
    cv::Mat blurred;
    cv::Mat thresholded;
    cv::Mat gray;
    cv::Mat blob;
    cv::Mat bgmodel;                                            
    cv::namedWindow("Frame");   
    cv::namedWindow("Background Model");
    cv::namedWindow("Blob");
    cv::VideoCapture cap("campus3.avi");    

    cv::BackgroundSubtractorMOG2 bgs;                           

        bgs.nmixtures = 3;
        bgs.history = 1000;
        bgs.varThresholdGen = 15;
        bgs.bShadowDetection = true;                            
        bgs.nShadowDetection = 0;                               
        bgs.fTau = 0.5;                                         

    std::vector<std::vector<cv::Point>> contours;               

    cv::HOGDescriptor human;
    assert(human.load("hogcascade_pedestrians.xml"));

    for(;;)
    {
        cap >> frame;                                           

        cv::GaussianBlur(frame,blurred,cv::Size(3,3),0,0,cv::BORDER_DEFAULT);

        bgs.operator()(blurred,fg);                         
        bgs.getBackgroundImage(bgmodel);                                

        cv::threshold(fg,thresholded,70.0f,255,CV_THRESH_BINARY);

        cv::Mat elementCLOSE(5,5,CV_8U,cv::Scalar(255,255,255));
        cv::morphologyEx(thresholded,thresholded,cv::MORPH_CLOSE,elementCLOSE);

        cv::findContours(thresholded,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE);
        cv::cvtColor(thresholded,blob,CV_GRAY2RGB);
        cv::drawContours(blob,contours,-1,cv::Scalar(1),CV_FILLED,8);

        cv::cvtColor(frame,gray,CV_RGB2GRAY);
        cv::equalizeHist(gray, gray);

        int cmin = 50; 
        int cmax = 1000;
        std::vector<cv::Rect> rects;
        std::vector<std::vector<cv::Point>>::iterator itc=contours.begin();

        while (itc!=contours.end()) {   

                if (itc->size() > cmin && itc->size() < cmax){ 

                        human.detectMultiScale(gray, rects);
                        for (unsigned int i=0;i<rects.size();i++) {
                            cv::rectangle(frame, cv::Point(rects[i].x, rects[i].y),
                            cv::Point(rects[i].x+rects[i].width, rects[i].y+rects[i].height),
                            cv::Scalar(0, 255, 0));
                         }

                        ++itc;
                    }else{++itc;}
        }

        cv::imshow("Frame",frame);
        cv::imshow("Background Model",bgmodel);
        cv::imshow("Blob",blob);
        if(cv::waitKey(30) >= 0) break;
    }
    return 0;
}

在我的代码中,我使用 findcontours 最小化区域并使用morphologyEX(关闭)使区域大于对象,然后我使用 HOGDescriptors.detectMultiScale 来检测轮廓区域中的人。但是我在运行程序时收到了错误消息。这是我的错误信息:

“OpenCV 错误:断言失败 (dsize.area() || (inv_scale_x > 0 && inv_scale_y > 0)) 在未知函数,文件 C:\OpenCV\modules\imgproc\src\imgwarp.cpp,第 1726 行”

我试图在没有 findcontours 的情况下直接检测MultiScale,但同样的错误消息发生在我身上!那么如何解决这个问题呢?

我会很感激这里的任何帮助。谢谢!:)

===============

编辑:解决!

我变了...

cv::HOGDescriptor body;

至...

cv::CascadeClassifier body;

它就像一个魅力!它可以检测到行人!:)

但是还有一个问题,这个程序运行很慢!太迟钝了!:))

4

1 回答 1

0

我已经解决了这个问题!

我变了...

cv::HOGDescriptor body;

至...

cv::CascadeClassifier body;

它就像一个魅力!:)

于 2013-06-13T08:56:27.393 回答