opencv 人脸和行人识别

之前运行haar特征的adaboost算法人脸检测一直出错,加上今天的HOG&SVM行人检测程序,一直报错。

今天总算发现自己犯了多么白痴的错误——是因为外部依赖项lib文件没有添加完整,想一头囊死啊

做程序一定要心如止水!!! 仔细查找!!!

 

1.人脸识别程序:

  1. #include "cv.h"   
  2. #include "highgui.h"   
  3.   
  4. #include <stdio.h>   
  5. #include <stdlib.h>   
  6. #include <string.h>   
  7. #include <assert.h>   
  8. #include <math.h>   
  9. #include <float.h>   
  10. #include <limits.h>   
  11. #include <time.h>   
  12. #include <ctype.h>   
  13. using namespace std;  
  14.   
  15. static CvMemStorage* storage = 0;  
  16. static CvHaarClassifierCascade* cascade = 0;  
  17.   
  18. void detect_and_draw( IplImage* image );  
  19.   
  20. const char* cascade_name =  
  21. "G:/OpenCV2.3.1/data/haarcascades/haarcascade_frontalface_alt.xml";  
  22. /* "haarcascade_profileface.xml";*/  
  23.   
  24. int main()  
  25. {  
  26.     CvCapture* capture = 0;  
  27.   
  28.     cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );  
  29.   
  30.     if( !cascade )  
  31.     {  
  32.         fprintf( stderr, "ERROR: Could not load classifier cascade/n" );  
  33.         //fprintf( stderr,   
  34.             //"Usage: facedetect --cascade=/"<cascade_path>"/[filename|camera_index]/n" );   
  35.         return -1;  
  36.     }  
  37.     storage = cvCreateMemStorage(0);  
  38.   
  39.   
  40.     cvNamedWindow( "result", 1 );  
  41.   
  42.   
  43.     const char* filename = "H:/test/face05.jpg";  
  44.     IplImage* image = cvLoadImage(filename );  
  45.   
  46.     if( image )  
  47.     {  
  48.         detect_and_draw( image );  
  49.         cvWaitKey(0);  
  50.         cvReleaseImage( &image );  
  51.     }  
  52.   
  53.     cvDestroyWindow("result");  
  54.     cvWaitKey(0);  
  55.     return 0;  
  56. }  
  57.   
  58. void detect_and_draw( IplImage* img )  
  59. {  
  60.     static CvScalar colors[] =   
  61.     {  
  62.         {{0,0,255}},  
  63.         {{0,128,255}},  
  64.         {{0,255,255}},  
  65.         {{0,255,0}},  
  66.         {{255,128,0}},  
  67.         {{255,255,0}},  
  68.         {{255,0,0}},  
  69.         {{255,0,255}}  
  70.     };  
  71.   
  72.     double scale = 1.3;  
  73.     IplImage* gray = cvCreateImage( cvSize(img->width,img->height), 8, 1 );  
  74.     IplImage* small_img = cvCreateImage( cvSize( cvRound (img->width/scale),  
  75.         cvRound (img->height/scale)),  
  76.         8, 1 );  
  77.     int i;  
  78.   
  79.     cvCvtColor( img, gray, CV_BGR2GRAY );  
  80.     cvResize( gray, small_img, CV_INTER_LINEAR );  
  81.     cvEqualizeHist( small_img, small_img );  
  82.     cvClearMemStorage( storage );  
  83.   
  84.     if( cascade )  
  85.     {  
  86.         double t = (double)cvGetTickCount();  
  87.         CvSeq* faces = cvHaarDetectObjects( small_img, cascade, storage,  
  88.             1.1, 2, 0/*CV_HAAR_DO_CANNY_PRUNING*/,  
  89.             cvSize(30, 30) );  
  90.         t = (double)cvGetTickCount() - t;  
  91.         printf( "detection time = %gms/n", t/((double)cvGetTickFrequency()*1000.) );  
  92.         for( i = 0; i < (faces ? faces->total : 0); i++ )  
  93.         {  
  94.             CvRect* r = (CvRect*)cvGetSeqElem( faces, i );  
  95.             CvPoint center;  
  96.             int radius;  
  97.             center.x = cvRound((r->x + r->width*0.5)*scale);  
  98.             center.y = cvRound((r->y + r->height*0.5)*scale);  
  99.             radius = cvRound((r->width + r->height)*0.25*scale);  
  100.             cvCircle( img, center, radius, colors[i%8], 3, 8, 0 );  
  101.         }  
  102.     }  
  103.   
  104.     cvShowImage( "result", img );  
  105.     cvReleaseImage( &gray );  
  106.     cvReleaseImage( &small_img );  
  107. }   
#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.行人检测程序

  1. #include <cv.h>    
  2. #include <highgui.h>      
  3. #include <string>    
  4. #include <iostream>    
  5. #include <algorithm>    
  6. #include <iterator>   
  7.   
  8. #include <stdio.h>   
  9. #include <string.h>   
  10. #include <ctype.h>   
  11.   
  12. using namespace cv;  
  13. using namespace std;  
  14.   
  15. void help()  
  16. {  
  17.     printf(  
  18.             "\nDemonstrate the use of the HoG descriptor using\n"  
  19.             "  HOGDescriptor::hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());\n"  
  20.             "Usage:\n"  
  21.             "./peopledetect (<image_filename> | <image_list>.txt)\n\n");  
  22. }  
  23.   
  24. int main(int argc, char** argv)  
  25. {  
  26.     Mat img;  
  27.     FILE* f = 0;  
  28.     char _filename[1024];  
  29.       
  30.     if( argc == 1 )  
  31.     {  
  32.         printf("Usage: peopledetect (<image_filename> | <image_list>.txt)\n");  
  33.         return 0;  
  34.     }  
  35.       
  36.     img = imread(argv[1]);  
  37.   
  38.     if( img.data )  
  39.     {  
  40.         strcpy(_filename, argv[1]);  
  41.     }  
  42.     else  
  43.     {  
  44.         f = fopen(argv[1], "rt");  
  45.         if(!f)  
  46.         {  
  47.             fprintf( stderr, "ERROR: the specified file could not be loaded\n");  
  48.             return -1;  
  49.         }  
  50.     }  
  51.   
  52.     HOGDescriptor hog;  
  53.     hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());//得到检测器   
  54.     namedWindow("people detector", 1);  
  55.   
  56.     for(;;)  
  57.     {  
  58.         char* filename = _filename;  
  59.         if(f)  
  60.         {  
  61.             if(!fgets(filename, (int)sizeof(_filename)-2, f))  
  62.                 break;  
  63.             //while(*filename && isspace(*filename))   
  64.             //  ++filename;   
  65.             if(filename[0] == '#')  
  66.                 continue;  
  67.             int l = strlen(filename);  
  68.             while(l > 0 && isspace(filename[l-1]))  
  69.                 --l;  
  70.             filename[l] = '\0';  
  71.             img = imread(filename);  
  72.         }  
  73.         printf("%s:\n", filename);  
  74.         if(!img.data)  
  75.             continue;  
  76.           
  77.         fflush(stdout);  
  78.         vector<Rect> found, found_filtered;  
  79.         double t = (double)getTickCount();  
  80.         // run the detector with default parameters. to get a higher hit-rate   
  81.         // (and more false alarms, respectively), decrease the hitThreshold and   
  82.         // groupThreshold (set groupThreshold to 0 to turn off the grouping completely).   
  83.         hog.detectMultiScale(img, found, 0, Size(8,8), Size(32,32), 1.05, 2);  
  84.         t = (double)getTickCount() - t;  
  85.         printf("tdetection time = %gms\n", t*1000./cv::getTickFrequency());  
  86.         size_t i, j;  
  87.         for( i = 0; i < found.size(); i++ )  
  88.         {  
  89.             Rect r = found[i];  
  90.             for( j = 0; j < found.size(); j++ )  
  91.                 if( j != i && (r & found[j]) == r)  
  92.                     break;  
  93.             if( j == found.size() )  
  94.                 found_filtered.push_back(r);  
  95.         }  
  96.         for( i = 0; i < found_filtered.size(); i++ )  
  97.         {  
  98.             Rect r = found_filtered[i];  
  99.             // the HOG detector returns slightly larger rectangles than the real objects.   
  100.             // so we slightly shrink the rectangles to get a nicer output.   
  101.             r.x += cvRound(r.width*0.1);  
  102.             r.width = cvRound(r.width*0.8);  
  103.             r.y += cvRound(r.height*0.07);  
  104.             r.height = cvRound(r.height*0.8);  
  105.             rectangle(img, r.tl(), r.br(), cv::Scalar(0,255,0), 3);  
  106.         }  
  107.         imshow("people detector", img);  
  108.         int c = waitKey(0) & 255;  
  109.         if( c == 'q' || c == 'Q' || !f)  
  110.             break;  
  111.     }  
  112.     if(f)  
  113.         fclose(f);  
  114.     return 0;  
  115. }  
#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.youkuaiyun.com/scut1135/article/details/7329398

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值