Can someone explain about detectMultiScale in openCV

非 Y 不嫁゛ 提交于 2019-12-10 11:52:43

问题


I've been trying objectDetection in openCV..

Followed a few steps..

  1. Resizing it to 64x64 resolution
  2. Changing it to gray scale
  3. Fetching XML for object detection
  4. Drawing rectangle fringing the pattern

Yet, I couldn't achieve it..

Here's my code :

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

The object size printed by me shows 0 unfortunately :( Hence, it doesn't go to the for loop.. Can anyone help me out..

Thanks in advance

PS : I've commented some lines in the code which were not useful. Please lemme know if I can incorporate the same.


回答1:


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.



来源:https://stackoverflow.com/questions/16276313/can-someone-explain-about-detectmultiscale-in-opencv

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