• Hog+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;
    }
  • 相关阅读:
    一些命令
    DB-5:使用PowerDesigner连接数据库并生成ER图
    [原创]OpenEuler20.03安装配置PostgreSQL13.4详细图文版
    oss 视频转码
    java lambda groupby 的map顺序问题
    Springboot得到RabbitMQ队列消息的数量
    Rabbitmq 定时任务 (代码实现)
    Rabbitmq 定时任务
    Mac navicat 15
    Mac 重置 idea
  • 原文地址:https://www.cnblogs.com/polly333/p/4498446.html
Copyright © 2020-2023  润新知