Opencv中人體檢測

來源:互聯網
上載者:User

以前做演算法實現一直用的說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);得到檢測的結果。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.