Can someone explain about detectMultiScale in openCV

人走茶凉 提交于 2019-12-06 16:50:32
Frankenstein

Found the answer..! I've missed out the specified arguments for detectMultiScale function.

It's working fine..Rectified code as follows

#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;
}

And finally this worked..!

PS: This program holds good only when the input is taken through image unlike WebCam or Video.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!