Opencv中人体检测

来源:互联网 发布:淘宝上欧货是什么意思 编辑:程序博客网 时间:2024/06/10 15:54

以前做算法实现一直用的说matlab,最近做目标跟踪的算法,开始使用Opencv,发现Opencv真的很强大,集合了很多算法,封装了很多函数,使用方法跟matlab很类似,但又比matlab的效率高。下面说我使用hog算法进行人体检测的代码,跟Opencv中的peopledetect.cpp类似,不过我修改了一些代码,可以检测连续的视频序列。

代码如下:

#include <fstream>#include <string>#include <cv.h>#include <highgui.h>#include <ml.h>#include <iostream>#include <fstream>#include <string>#include <vector>#include "cvaux.h"#include <iostream>#include <stdio.h>#include <string.h>#include <ctype.h>using namespace cv;using namespace std;int main(){Mat img0,img1;Mat img2;//Mat img3;int i;FILE* f = 0;char _filename[1024];//imshow("hello",img3);//waitKey(0);HOGDescriptor hog;//CvLatentSvmDetector* detector = cvLoadLatentSvmDetector("result.xml");CvSVM svm;svm.load("result.xml");//hog.load("result.xml");//int sv_num= svm.get_support_vector_count(); hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());//hog.setSVMDetector(detector);Mat img4;for(i=46;i<180;i++){char file[1024];sprintf(file,"D:\\My Documents\\Visual Studio 2008\\Projects\\cc\\CS-%d.bmp",i);char file4[1024];sprintf(file4,"D:\\My Documents\\Visual Studio 2008\\Projects\\cc\\CS-%d.bmp",i-1);Mat img3;img3 = imread(file4);img4 = imread(file,1);//cvSub((CvMat)img2,(CvMat)img3,img);//img = img4-img3;Mat img(img4.rows,img4.cols,CV_8UC1);for(int n=0;n<img4.rows;n++){for(int j=0;j<img4.cols;j++){short e,d;short temp;e = *(img4.data+n*img4.step+j*3);d = *(img3.data+n*img3.step+j*3);//c = *(img2.data+i);temp = e-d;if(abs(temp)<3)temp=0;*(img.data+n*img.step+j) = (uchar)e;}}//cv::subtract//char* filename = _filename;char* filename = file;if(0){if(!fgets(filename, (int)sizeof(_filename)-2, f))break;//while(*filename && isspace(*filename))// ++filename;if(filename[0] == '#')continue;int l = strlen(filename);while(l > 0 && isspace(filename[l-1]))--l;filename[l] = '\0';img = imread(filename);}printf("%s:\n", filename);if(!img.data)continue;fflush(stdout);vector<Rect> found, found_filtered;double t = (double)getTickCount();// run the detector with default parameters. to get a higher hit-rate// (and more false alarms, respectively), decrease the hitThreshold and// groupThreshold (set groupThreshold to 0 to turn off the grouping completely).int can = img.channels();hog.detectMultiScale(img, found, 0, Size(8,8), Size(32,32), 1.05, 2);t = (double)getTickCount() - t;printf("tdetection time = %gms\n", t*1000./cv::getTickFrequency());size_t i, j;for( i = 0; i < found.size(); i++ ){Rect r = found[i];for( j = 0; j < found.size(); j++ )if( j != i && (r & found[j]) == r)break;if( j == found.size() )found_filtered.push_back(r);}for( i = 0; i < found_filtered.size(); i++ ){Rect r = found_filtered[i];// the HOG detector returns slightly larger rectangles than the real objects.// so we slightly shrink the rectangles to get a nicer output.r.x += cvRound(r.width*0.1);r.width = cvRound(r.width*1);r.y += cvRound(r.height*0.07);r.height = cvRound(r.height*0.8);rectangle(img, r.tl(), r.br(), cv::Scalar(0,255,0), 3);}imshow("people detector", img);//int c = waitKey(0) & 255;//if( c == 'q' || c == 'Q' || !f)//break;waitKey(1);//cvDestoryWindow("people detector");}if(f)fclose(f);return 0;}


其中hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());得到svm的检测算子。

hog.detectMultiScale(img, found, 0, Size(8,8), Size(32,32), 1.05, 2);得到检测的结果。

原创粉丝点击