Hog+Camshift的人体跟踪

这几天跑了Opencv中的camshift算法,发现目标需要自己去选,够费劲的。

突然想要,是hog进行人体检测,然后用shift去跟踪是不是效果会好些。

camshift的跟踪还好,主要是hog的检测,opencv中hog检测的误检率还是很高的。

程序我直接拿来了opencv中现成的代码,也就是个简单的demo。大家看看,感觉下效果就可以了

如果需要更精确的效果,程序还需要很多优化,毕竟opencv中的camshift算法紧紧是重心的跟踪迭代。

程序如下:

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

Rect r ;
int track_object = 0;

Rect ObjectDectd(IplImage* frame,int object,Rect r);
IplImage* MeanSift(IplImage *frame,Rect r);

int main()
{
	int number = 0;
	CvCapture* capture = 0;
	capture = cvCaptureFromAVI("112218.avi");

	if( !capture )
    {
        fprintf(stderr,"Could not initialize capturing...\n");
        return -1;
    }

	cvNamedWindow( "HogSiftDemo", 1 );

	for(;;)
	{
		cout<<number<<endl;
		number++;
		IplImage* frame = 0;
        frame = cvQueryFrame( capture );
		//frame = cvLoadImage("D:\\My Documents\\Visual Studio 2008\\Projects\\hogmeansift\\Debug\\crop001009.png");

		if(track_object == 0)
		{
			r = ObjectDectd(frame,track_object,r);
			if(r.x!=0)
				track_object  = -1;
		}
		else frame = MeanSift(frame,r);

		//cvRectangle(frame, r.tl(), r.br(), cv::Scalar(0,255,0), 3);
		cvShowImage("HogSiftDemo", frame);
		waitKey(1);

	}
	cvReleaseCapture( &capture );
    cvDestroyWindow("HogSiftDemo");

	return 0;
}

Rect ObjectDectd(IplImage* frame,int object,Rect r)
{
	HOGDescriptor hog;
	hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());
	Mat img;
	img = frame;	
	fflush(stdout);
	vector<Rect> found, found_filtered;
	double t = (double)getTickCount();
	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;
	if(found.size()!=0)
	{
		//object = 1;
		for( i = 0; i < found.size(); i++ )
		{
			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++ )
		{
			r = found_filtered[i];
			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);
			//cvRectangle(frame, r.tl(), r.br(), cv::Scalar(0,255,0), 3);
		}
	}
	return r;
}

IplImage *image = 0, *hsv = 0, *hue = 0, *mask = 0, *backproject = 0, *histimg = 0;
CvHistogram *hist = 0;

int backproject_mode = 0;
int select_object = 0;
//int track_object = 0;
int show_hist = 1;
CvPoint origin;
CvRect selection;
CvRect track_window;
CvBox2D track_box;
CvConnectedComp track_comp;
int hdims = 16;
float hranges_arr[] = {0,180};
float* hranges = hranges_arr;
int vmin = 10, vmax = 256, smin = 30;
int i, bin_w, c;

CvScalar hsv2rgb( float hue )
{
    int rgb[3], p, sector;
    static const int sector_data[][3]=
        {{0,2,1}, {1,2,0}, {1,0,2}, {2,0,1}, {2,1,0}, {0,1,2}};
    hue *= 0.033333333333333333333333333333333f;
    sector = cvFloor(hue);
    p = cvRound(255*(hue - sector));
    p ^= sector & 1 ? 255 : 0;

    rgb[sector_data[sector][0]] = 255;
    rgb[sector_data[sector][1]] = 0;
    rgb[sector_data[sector][2]] = p;

    return cvScalar(rgb[2], rgb[1], rgb[0],0);
}

IplImage* MeanSift(IplImage *frame,Rect r)
{
	if( !image )
    {
        /* allocate all the buffers */
        image = cvCreateImage( cvGetSize(frame), 8, 3 );
        image->origin = frame->origin;
        hsv = cvCreateImage( cvGetSize(frame), 8, 3 );
        hue = cvCreateImage( cvGetSize(frame), 8, 1 );
        mask = cvCreateImage( cvGetSize(frame), 8, 1 );
        backproject = cvCreateImage( cvGetSize(frame), 8, 1 );
        hist = cvCreateHist( 1, &hdims, CV_HIST_ARRAY, &hranges, 1 );
       // histimg = cvCreateImage( cvSize(320,200), 8, 3 );
        //cvZero( histimg );
    }

    cvCopy( frame, image, 0 );
    cvCvtColor( image, hsv, CV_BGR2HSV );

    if( track_object )
    {
        int _vmin = vmin, _vmax = vmax;

        cvInRangeS( hsv, cvScalar(0,smin,MIN(_vmin,_vmax),0),
                    cvScalar(180,256,MAX(_vmin,_vmax),0), mask );
        cvSplit( hsv, hue, 0, 0, 0 );

        if( track_object < 0 )
        {
			selection.height = r.height;
			selection.width = r.width;
			selection.x = r.x;
			selection.y = r.y;
           
			float max_val = 0.f;
            cvSetImageROI( hue, selection );
            cvSetImageROI( mask, selection );
            cvCalcHist( &hue, hist, 0, mask );
            cvGetMinMaxHistValue( hist, 0, &max_val, 0, 0 );
            cvConvertScale( hist->bins, hist->bins, max_val ? 255. / max_val : 0., 0 );
            cvResetImageROI( hue );
            cvResetImageROI( mask );
            track_window = selection;
            track_object = 1;
        }

        cvCalcBackProject( &hue, backproject, hist );
        cvAnd( backproject, mask, backproject, 0 );
        cvCamShift( backproject, track_window,
                    cvTermCriteria( CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 10, 1 ),
                    &track_comp, &track_box );
        track_window = track_comp.rect;

        if( backproject_mode )
            cvCvtColor( backproject, image, CV_GRAY2BGR );
        if( !image->origin )
            track_box.angle = -track_box.angle;
        //cvEllipseBox( image, track_box, CV_RGB(255,0,0), 3, CV_AA, 0 );
		Rect r;		
		r.x = track_comp.rect.x;
		r.width = track_comp.rect.height;
		r.y = track_comp.rect.y;
		r.height = track_comp.rect.width;
		cvRectangle(image, r.tl(), r.br(), cv::Scalar(0,255,0), 3);
    }

    if( select_object && selection.width > 0 && selection.height > 0 )
    {
        cvSetImageROI( image, selection );
        cvXorS( image, cvScalarAll(255), image, 0 );
        cvResetImageROI( image );
    }
	return image;
}


你可能感兴趣的:(Hog+Camshift的人体跟踪)