gpt4 book ai didi

c++ - 有人可以解释一下 openCV 中的 detectMultiScale

转载 作者:太空宇宙 更新时间:2023-11-03 23:01:43 25 4
gpt4 key购买 nike

我一直在尝试 openCV 中的 objectDetection..

遵循了几个步骤..

  1. 将其调整为 64x64 分辨率
  2. 将其更改为灰度
  3. 获取用于对象检测的 XML
  4. 绘制矩形边缘图案

然而,我无法实现..

这是我的代码:

#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:我在代码中注释了一些没有用的行。请让我知道我是否可以合并相同的内容。

最佳答案

找到答案..!我错过了 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:只有当输入是通过图像而不是网络摄像头或视频时,该程序才适用。

关于c++ - 有人可以解释一下 openCV 中的 detectMultiScale,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16276313/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com