c++ 有人能解释一下openCV中的detectMultiScale吗

jei2mxaa  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(120)

我一直在尝试openCV中的对象检测。
跟着几个步骤。
1.将其调整为64x64分辨率
1.把它改成灰度
1.获取XML以进行对象检测
1.绘制矩形镶边图案
但是,我没能做到。
下面是我的代码:

#include<iostream>
#include "cv.h"
#include "highgui.h"
#include<vector>

using namespace cv;
using namespace std;

int main()
{
    IplImage* img;
    img = cvLoadImage( "hindi3.jpg" );

    vector<cv::Rect> objects;

    // ***Resize image to 64x64 resolution***

    IplImage *resizeImage = cvCreateImage(cvSize(64,64),8,3);

    cvResize(img,resizeImage,CV_INTER_LINEAR);

    cvShowImage("Resize",resizeImage);
    cvWaitKey(0);

    // ***Convert image to grayscale***

    IplImage *grayImage = cvCreateImage(cvGetSize(resizeImage),8,1);

    cvCvtColor(resizeImage,grayImage,CV_BGR2GRAY);

    cvShowImage("gray",grayImage);
    cvWaitKey(0);

    // ***Getting the XML (Cascade xml generated thru haarTraining)***

    CvMemStorage* storage = cvCreateMemStorage(0);
    cout<<"Memory created\n";

    cv::CascadeClassifier cascade;
    cascade.load("cascade.xml");
    //CvHaarClassifierCascade* cascade = (CvHaarClassifierCascade*)cvLoad( "cascade.xml" );
    cout<<"cascade.xml loaded successfully\n";
    double scale = 1.3;

    static CvScalar colors[] = { {{0,0,255}}, {{0,128,255}}, {{0,255,255}},
    {{0,255,0}}, {{255,128,0}}, {{255,255,0}}, {{255,0,0}}, {{255,0,255}} };

    // ***Detect objects***

    cvClearMemStorage( storage );
    objects.clear();
    //CvSeq* objects = cvHaarDetectObjects( grayImage, cascade, storage, 1.1, 4, 0, cvSize( 40, 50 ));
    //cascade.detectMultiScale(grayImage, objects, 1.2, 4, CV_HAAR_DO_CANNY_PRUNING, cvSize(30, 30));
    cascade.detectMultiScale(grayImage, objects, 1.2, 4, CV_HAAR_SCALE_IMAGE, cvSize(30, 30));

    // ***Draw a rectangle outside recognized pattern***

    cout<<"Object size : "<<objects.size();
    for( vector<cv::Rect>::const_iterator r = objects.begin(); r != objects.end(); r++)
    {            //rectangle(img, *r, Scalar(0,0,255), 2, 8, 0);
                 cvRectangle( grayImage, cvPoint( r->x, r->y ), cvPoint( r->x + r->width, r->y + r->height ),Scalar(0,0,255));
                 cout<<"In the loop\n";
    }

    cvNamedWindow( "Output" );
    cvShowImage( "Output", grayImage );
    cvWaitKey(0);

    cvReleaseImage(&resizeImage);
    cvReleaseImage(&grayImage);
    cvReleaseImage( &img );

    return 0;
}

字符串
我打印出来的对象大小显示为0,因此,它没有进入for循环.
PS:我已经在代码中注解了一些没有用的行。请让我知道我是否可以合并相同的。

weylhg0b

weylhg0b1#

找到答案了..!我遗漏了detectMultiScale函数的指定参数。
它的工作正常..纠正代码如下

#include<iostream>
#include "cv.h"
#include "highgui.h"
#include<vector>

using namespace cv;
using namespace std;

int main()
{

    IplImage* img;
    img = cvLoadImage( "test.jpg" );

    vector<cv::Rect> objects;

    /*** Resizing is optional***
    *****************************

    IplImage *resizeImage = cvCreateImage(cvSize(64,64),8,3);

    cvResize(img,resizeImage,CV_INTER_LINEAR);
    cvShowImage("Resize",resizeImage);
    cvWaitKey(0);*/

    /*** Change image into grayscale***
    **********************************/

    IplImage *grayImage = cvCreateImage(cvGetSize(img),8,1);
    cvCvtColor(img,grayImage,CV_BGR2GRAY);

    //cvEqualizeHist(grayImage,grayImage); This is optional

    cvShowImage("gray",grayImage);
    cvWaitKey(0);

    CvMemStorage* storage = cvCreateMemStorage(0);
    cout<<"Memory created\n";

    /*** Load the XML generated through haartraining***
    **************************************************/

    cv::CascadeClassifier cascade;
    cascade.load("cascade.xml");
    //CvHaarClassifierCascade* cascade = (CvHaarClassifierCascade*)cvLoad( "cascade.xml" );
    cout<<"cascade.xml loaded successfully\n";
    double scale = 1.3;

    static CvScalar colors[] = { {{0,0,255}}, {{0,128,255}}, {{0,255,255}},
    {{0,255,0}}, {{255,128,0}}, {{255,255,0}}, {{255,0,0}}, {{255,0,255}} };

    /*** Detect objects***
    **********************/

    cvClearMemStorage( storage );
    objects.clear();
    //CvSeq* objects = cvHaarDetectObjects( grayImage, cascade, storage, 1.1, 4, 0, cvSize( 40, 50 ));
    //cascade.detectMultiScale(grayImage, objects, 1.2, 4, CV_HAAR_DO_CANNY_PRUNING, cvSize(30, 30)); if captured through WebCam
    cascade.detectMultiScale(grayImage, objects, 1.1, 3, CV_HAAR_SCALE_IMAGE | CV_HAAR_DO_CANNY_PRUNING,cvSize(0,0), cvSize(100,100));

    cout<<"Object size : "<<objects.size();

    /***Draw Rectangle outside recognized pattern***
    ***********************************************/

    for( vector<cv::Rect>::const_iterator r = objects.begin(); r != objects.end(); r++)
    {            //rectangle(img, *r, Scalar(0,0,255), 2, 8, 0);
                 cvRectangle( grayImage, cvPoint( r->x, r->y ), cvPoint( r->x + r->width, r->y + r->height ),Scalar(0,0,255));
                 cout<<"In the loop\n";
    }

    cvNamedWindow( "Output" );
    cvShowImage( "Output", grayImage );
    cvWaitKey(0);

    //cvReleaseImage(&resizeImage); If resized
    cvReleaseImage(&grayImage);
    cvReleaseImage( &img );

    return 0;
}

字符串
终于成功了!
PS:这个程序只有当输入是通过图像拍摄时才有效,不像网络摄像头或视频。

相关问题