如何在程序中使用xml文件

时间:2013-12-07 06:18:23

标签: c++ xml opencv

我需要使用Haar-Cascade-Classifier进行车辆检测。代码如下:

#include <stdio.h>

#include <cv.h>

#include <highgui.h>

CvHClassifieaarrCascade *cascade;

CvMemStorage            *storage;

void detect(IplImage *img);
int main(int argc, char** argv)
{
 CvCapture *capture;
 IplImage  *frame;
 int input_resize_percent = 100;
 if(argc < 3)
{
std::cout << "Usage " << argv[0] << " cascade.xml video.avi" << std::endl;
return 0;
}
if(argc == 4)
{
input_resize_percent = atoi(argv[3]);
std::cout << "Resizing to: " << input_resize_percent << "%" << std::endl;
}
cascade = (CvHaarClassifierCascade*) cvLoad(argv[1], 0, 0, 0);
storage = cvCreateMemStorage(0);
capture = cvCaptureFromAVI(argv[2]);
assert(cascade && storage && capture);
cvNamedWindow("video", 1);
IplImage* frame1 = cvQueryFrame(capture);
frame = cvCreateImage(cvSize((int)((frame1->width*input_resize_percent)/100) ,(int)    
((frame1->height*input_resize_percent)/100)), frame1->depth, frame1->nChannels);
const int KEY_SPACE  = 32;
const int KEY_ESC    = 27;
int key = 0;
do
{
frame1 = cvQueryFrame(capture);
if(!frame1)
break;
cvResize(frame1, frame);
detect(frame);
key = cvWaitKey(10);
if(key == KEY_SPACE)
key = cvWaitKey(0);
if(key == KEY_ESC)
break;
}while(1);
cvDestroyAllWindows();
cvReleaseImage(&frame);
cvReleaseCapture(&capture);
cvReleaseHaarClassifierCascade(&cascade);
cvReleaseMemStorage(&storage);
return 0;
}
void detect(IplImage *img)
{
CvSize img_size = cvGetSize(img);
CvSeq *object = cvHaarDetectObjects(
img,
cascade,
storage,
1.1, //1.1,//1.5, //-------------------SCALE FACTOR
1, //2        //------------------MIN NEIGHBOURS
0, //CV_HAAR_DO_CANNY_PRUNING
cvSize(0,0),//cvSize( 30,30), // ------MINSIZE
img_size //cvSize(70,70)//cvSize(640,480)  //---------MAXSIZE
);
std::cout << "Total: " << object->total << " cars" << std::endl;
for(int i = 0 ; i < ( object ? object->total : 0 ) ; i++)
{
CvRect *r = (CvRect*)cvGetSeqElem(object, i);
cvRectangle(img,
cvPoint(r->x, r->y),
cvPoint(r->x + r->width, r->y + r->height),
CV_RGB(255, 0, 0), 2, 8, 0);
}
cvShowImage("video", img);
}

如何使用cout中存在的xml文件

1 个答案:

答案 0 :(得分:1)

对于Iplimage类型的图像,它是:

     CvHaarClassifierCascade* cascade = (CvHaarClassifierCascade*)cvLoad( "haarcascade_frontalface_alt2.xml" );

这里'cascade'是您必须应用的分类器的名称,“haarcascade_frontalface_alt2.xml”是我的.xml(这里我使用的是面部检测器)

对于Mat类型的图像,它是:

    String face_cascade_name = "haarcascade_frontalface_alt2.xml";
    CascadeClassifier face_cascade;

但是在使用它时确保您已将.xml文件复制到项目文件夹以及Project Solution的Source Files文件夹中,否则它将无效。