学习OpenCV——行人识别&人脸识别
来源:互联网 发布:常用数据统计的软件是 编辑:程序博客网 时间:2024/06/10 01:28
之前运行haar特征的adaboost算法人脸检测一直出错,加上今天的HOG&SVM行人检测程序,一直报错。
今天总算发现自己犯了多么白痴的错误——是因为外部依赖项lib文件没有添加完整,想一头囊死啊
做程序一定要心如止水!!! 仔细查找!!!
转自:http://blog.csdn.net/sangni007/article/details/7453987
1.人脸识别程序:
- #include "cv.h"
- #include "highgui.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <assert.h>
- #include <math.h>
- #include <float.h>
- #include <limits.h>
- #include <time.h>
- #include <ctype.h>
- using namespace std;
- static CvMemStorage* storage = 0;
- static CvHaarClassifierCascade* cascade = 0;
- void detect_and_draw( IplImage* image );
- const char* cascade_name =
- "G:/OpenCV2.3.1/data/haarcascades/haarcascade_frontalface_alt.xml";
- /* "haarcascade_profileface.xml";*/
- int main()
- {
- CvCapture* capture = 0;
- cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );
- if( !cascade )
- {
- fprintf( stderr, "ERROR: Could not load classifier cascade/n" );
- //fprintf( stderr,
- //"Usage: facedetect --cascade=/"<cascade_path>"/[filename|camera_index]/n" );
- return -1;
- }
- storage = cvCreateMemStorage(0);
- cvNamedWindow( "result", 1 );
- const char* filename = "H:/test/face05.jpg";
- IplImage* image = cvLoadImage(filename );
- if( image )
- {
- detect_and_draw( image );
- cvWaitKey(0);
- cvReleaseImage( &image );
- }
- cvDestroyWindow("result");
- cvWaitKey(0);
- return 0;
- }
- void detect_and_draw( IplImage* img )
- {
- 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}}
- };
- double scale = 1.3;
- IplImage* gray = cvCreateImage( cvSize(img->width,img->height), 8, 1 );
- IplImage* small_img = cvCreateImage( cvSize( cvRound (img->width/scale),
- cvRound (img->height/scale)),
- 8, 1 );
- int i;
- cvCvtColor( img, gray, CV_BGR2GRAY );
- cvResize( gray, small_img, CV_INTER_LINEAR );
- cvEqualizeHist( small_img, small_img );
- cvClearMemStorage( storage );
- if( cascade )
- {
- double t = (double)cvGetTickCount();
- CvSeq* faces = cvHaarDetectObjects( small_img, cascade, storage,
- 1.1, 2, 0/*CV_HAAR_DO_CANNY_PRUNING*/,
- cvSize(30, 30) );
- t = (double)cvGetTickCount() - t;
- printf( "detection time = %gms/n", t/((double)cvGetTickFrequency()*1000.) );
- for( i = 0; i < (faces ? faces->total : 0); i++ )
- {
- CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
- CvPoint center;
- int radius;
- center.x = cvRound((r->x + r->width*0.5)*scale);
- center.y = cvRound((r->y + r->height*0.5)*scale);
- radius = cvRound((r->width + r->height)*0.25*scale);
- cvCircle( img, center, radius, colors[i%8], 3, 8, 0 );
- }
- }
- cvShowImage( "result", img );
- cvReleaseImage( &gray );
- cvReleaseImage( &small_img );
- }
#include "cv.h"#include "highgui.h"#include <stdio.h>#include <stdlib.h>#include <string.h>#include <assert.h>#include <math.h>#include <float.h>#include <limits.h>#include <time.h>#include <ctype.h>using namespace std;static CvMemStorage* storage = 0;static CvHaarClassifierCascade* cascade = 0;void detect_and_draw( IplImage* image );const char* cascade_name ="G:/OpenCV2.3.1/data/haarcascades/haarcascade_frontalface_alt.xml";/* "haarcascade_profileface.xml";*/int main(){CvCapture* capture = 0;cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );if( !cascade ){fprintf( stderr, "ERROR: Could not load classifier cascade/n" );//fprintf( stderr,//"Usage: facedetect --cascade=/"<cascade_path>"/[filename|camera_index]/n" );return -1;}storage = cvCreateMemStorage(0);cvNamedWindow( "result", 1 );const char* filename = "H:/test/face05.jpg";IplImage* image = cvLoadImage(filename );if( image ){detect_and_draw( image );cvWaitKey(0);cvReleaseImage( &image );}cvDestroyWindow("result");cvWaitKey(0);return 0;}void detect_and_draw( IplImage* img ){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}}};double scale = 1.3;IplImage* gray = cvCreateImage( cvSize(img->width,img->height), 8, 1 );IplImage* small_img = cvCreateImage( cvSize( cvRound (img->width/scale),cvRound (img->height/scale)),8, 1 );int i;cvCvtColor( img, gray, CV_BGR2GRAY );cvResize( gray, small_img, CV_INTER_LINEAR );cvEqualizeHist( small_img, small_img );cvClearMemStorage( storage );if( cascade ){double t = (double)cvGetTickCount();CvSeq* faces = cvHaarDetectObjects( small_img, cascade, storage,1.1, 2, 0/*CV_HAAR_DO_CANNY_PRUNING*/,cvSize(30, 30) );t = (double)cvGetTickCount() - t;printf( "detection time = %gms/n", t/((double)cvGetTickFrequency()*1000.) );for( i = 0; i < (faces ? faces->total : 0); i++ ){CvRect* r = (CvRect*)cvGetSeqElem( faces, i );CvPoint center;int radius;center.x = cvRound((r->x + r->width*0.5)*scale);center.y = cvRound((r->y + r->height*0.5)*scale);radius = cvRound((r->width + r->height)*0.25*scale);cvCircle( img, center, radius, colors[i%8], 3, 8, 0 );}}cvShowImage( "result", img );cvReleaseImage( &gray );cvReleaseImage( &small_img );}
2.行人检测程序
- #include <cv.h>
- #include <highgui.h>
- #include <string>
- #include <iostream>
- #include <algorithm>
- #include <iterator>
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- using namespace cv;
- using namespace std;
- void help()
- {
- printf(
- "\nDemonstrate the use of the HoG descriptor using\n"
- " HOGDescriptor::hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());\n"
- "Usage:\n"
- "./peopledetect (<image_filename> | <image_list>.txt)\n\n");
- }
- int main(int argc, char** argv)
- {
- Mat img;
- FILE* f = 0;
- char _filename[1024];
- if( argc == 1 )
- {
- printf("Usage: peopledetect (<image_filename> | <image_list>.txt)\n");
- return 0;
- }
- img = imread(argv[1]);
- if( img.data )
- {
- strcpy(_filename, argv[1]);
- }
- else
- {
- f = fopen(argv[1], "rt");
- if(!f)
- {
- fprintf( stderr, "ERROR: the specified file could not be loaded\n");
- return -1;
- }
- }
- HOGDescriptor hog;
- hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());//得到检测器
- namedWindow("people detector", 1);
- for(;;)
- {
- char* filename = _filename;
- if(f)
- {
- 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).
- 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*0.8);
- 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;
- }
- if(f)
- fclose(f);
- return 0;
- }
#include <cv.h> #include <highgui.h> #include <string> #include <iostream> #include <algorithm> #include <iterator>#include <stdio.h>#include <string.h>#include <ctype.h>using namespace cv;using namespace std;void help(){printf("\nDemonstrate the use of the HoG descriptor using\n"" HOGDescriptor::hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());\n""Usage:\n""./peopledetect (<image_filename> | <image_list>.txt)\n\n");}int main(int argc, char** argv){ Mat img; FILE* f = 0; char _filename[1024]; if( argc == 1 ) { printf("Usage: peopledetect (<image_filename> | <image_list>.txt)\n"); return 0; } img = imread(argv[1]); if( img.data ) { strcpy(_filename, argv[1]); } else { f = fopen(argv[1], "rt"); if(!f) { fprintf( stderr, "ERROR: the specified file could not be loaded\n"); return -1; } } HOGDescriptor hog; hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());//得到检测器 namedWindow("people detector", 1); for(;;) { char* filename = _filename; if(f) { 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). 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*0.8); 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; } if(f) fclose(f); return 0;}
注意:可能会出现tbb_debug.dll的问题,在G:\OpenCV2.3.1\build\common\tbb\ia32\vc10中找到tbb.dll改名为tbb_debug.dll 加到程序绝对目录下即可
还有其他的解决方式:http://blog.csdn.net/scut1135/article/details/7329398
- 学习OpenCV——行人识别&人脸识别
- 学习OpenCV——行人识别&人脸识别(总算运行出来了)
- 学习OpenCV——行人识别&人脸识别(总算运行出来了)
- 学习OpenCV——行人识别&人脸识别(总算运行出来了)
- OpenCV的行人识别&人脸识别
- OpenCV的行人识别&人脸识别
- opencv 人脸和行人识别
- opencv 人脸识别和行人检测
- opencv 行人识别
- 学习opencv——人脸识别
- OpenCV — 人脸识别
- 【OpenCV】人脸识别——识别是谁
- 从人脸识别到行人重识别,下一个风口
- 红外视频——行人识别
- 行人识别
- OpenCV学习笔记(五十五)——用OpenCV做人脸识别和性别识别contrib
- [置顶] OpenCV — 人脸识别
- svm 行人识别 训练 基于Opencv
- 小练2
- Linux--Shell 编程-bash,命令替换,if分支嵌套,运算,输入输出
- 学习OpenCV——SVM 手写数字检测
- 如何修复未格式化的坏块?
- 文章3:libcurl基本编程概述
- 学习OpenCV——行人识别&人脸识别
- c++字符串反转
- 文章4:multipart/form-data详细介绍
- 和十期一起装系统
- 串行接口(交叉线和直连线的区别)
- 考进中科院计算所:我的经历和体会
- 学习OpenCV——车牌检测(定位)
- UML学习(一)
- Mshflexgrid控件填写数据时的添加方式的改变,加快数据读取速度.