基于opencv的行人检测和人脸检测代码(摄像头,视频,图像)

更新时间:2023-05-07 11:13:01 阅读量: 实用文档 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

//摄像头的

#include "cv.h"

#include"highgui.h"

#include"stdio.h"

#ifdef _EiC

#define WIN32

#endif

static CvMemStorage* storage = 0;//设存储器,返回空间头指针

static CvHaarClassifierCascade* cascade = 0;

void detect_and_draw( IplImage* image );

const char* cascade_name =

"haarcascade_frontalface_alt.xml";//人脸检测分类器

int main( int argc, char** argv )

{

CvCapture* capture = 0;

IplImage *frame, *frame_copy = 0;

int optlen = strlen("--cascade=");

const char* input_name;

if( argc > 1 && strncmp( argv[1], "--cascade=", optlen ) == 0 )

{

cascade_name = argv[1] + optlen;

input_name = argc > 2 ? argv[2] : 0;

}

else

{

cascade_name = "F:\\C++程序\\haar人脸检测\\haarcascade_frontalface_alt2.xml";//分类器路径input_name = argc > 1 ? argv[1] : 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=\"\" [filename|camera_index]\n" );

return -1;

}

storage = cvCreateMemStorage(0);

capture = cvCaptureFromCAM( !input_name ? 0 : input_name[0] - '0' );//读取摄像头

//if(!capture)//如果没有摄像头读取视频文件

//capture = cvCaptureFromA VI("检测.avi");

//cvNamedWindow( "result", 1);//创建窗口

if( capture )

{

for(;;)

{

if( !cvGrabFrame( capture ))//从摄像头中抓取帧

break;

frame = cvRetrieveFrame( capture );//读取上边抓取的帧

if( !frame )

break;

if( !frame_copy )

frame_copy = cvCreateImage( cvSize(frame->width,frame->height),

IPL_DEPTH_8U, frame->nChannels );

if( frame->origin == IPL_ORIGIN_TL )

cvCopy( frame, frame_copy, 0 );

else

cvFlip( frame, frame_copy, 0 );

detect_and_draw( frame_copy );

if( cvWaitKey( 10 ) >= 0 )

break;

}

cvReleaseImage( &frame_copy );

cvReleaseCapture( &capture );

}

cvDestroyWindow("result");

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 );

}

//该部分为行人检测代码是opencv给的例程,但是运行的时候要注意一些版本兼容问题,绝对没有问题

#include

#include

#include

#include

#include

#include

#include

#include

#include

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 ( | .txt)\n\n");

}

int main(int argc, char** argv)

{

Mat img;

FILE* f = 0;

char _filename[1024];

if( argc == 1 )

{

printf("Usage: peopledetect ( | .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 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;

}

本文来源:https://www.bwwdw.com/article/qxde.html

Top