基于头部跟踪初代码测试

#include "iostream"
#include "queue"
#include "Windows.h"

#include <opencv2/ml/ml.hpp>
#include "opencv2/opencv.hpp"
#include "Windows.h"
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/objdetect/objdetect.hpp"  
#include "opencv2/features2d/features2d.hpp"  
#include "opencv2/calib3d/calib3d.hpp"  
#include "opencv2/ml.hpp"
#include "opencv/cv.h"
#include "opencv/ml.h"
#define MAX 30
using namespace cv;
using namespace std;
 
//画直方图用  
int HistogramBins = 256;
float HistogramRange1[2] = { 0, 255 };
float *HistogramRange[1] = { &HistogramRange1[0] };

/*
* imagefile1:
* imagefile2:
* method: could be CV_COMP_CHISQR, CV_COMP_BHATTACHARYYA, CV_COMP_CORREL, CV_COMP_INTERSECT
*/

double CompareHist(IplImage *image1, IplImage *image2)
{

    CvHistogram *Histogram1 = cvCreateHist(1, &HistogramBins, CV_HIST_ARRAY, HistogramRange);
    CvHistogram *Histogram2 = cvCreateHist(1, &HistogramBins, CV_HIST_ARRAY, HistogramRange);

    cvCalcHist(&image1, Histogram1);
    cvCalcHist(&image2, Histogram2);

    cvNormalizeHist(Histogram1, 1);
    cvNormalizeHist(Histogram2, 1);

    // CV_COMP_CHISQR,CV_COMP_BHATTACHARYYA这两种都可以用来做直方图的比较,值越小,说明图形越相似  
    return cvCompareHist(Histogram1, Histogram2, CV_COMP_CHISQR);
    //printf("CV_COMP_BHATTACHARYYA : %.4f\n", cvCompareHist(Histogram1, Histogram2, CV_COMP_BHATTACHARYYA));


    // CV_COMP_CORREL, CV_COMP_INTERSECT这两种直方图的比较,值越大,说明图形越相似  
    //printf("CV_COMP_CORREL : %.4f\n", cvCompareHist(Histogram1, Histogram2, CV_COMP_CORREL));
    //printf("CV_COMP_INTERSECT : %.4f\n", cvCompareHist(Histogram1, Histogram2, CV_COMP_INTERSECT));

    //cvReleaseImage(&image1);
    //cvReleaseImage(&image2);
    cvReleaseHist(&Histogram1);
    cvReleaseHist(&Histogram2);
}
Mat Getmat(Mat mat, Rect rect)
{
    return mat(rect);
}
int main()
{

    VideoCapture video(0);

    if (!video.isOpened())
    {
        return -1;
    }
    Mat img;
    Mat img1,img2;
    int num = 0;
    Rect rect(270, 130, 120, 160), rected[MAX];
    //int diswidth[MAX] = { 1, 1, 0, -1, -1, -1, 0, 1, 2, 2, 0, -2, -2, -2, 0, 2};
    //int dishigth[MAX] = { 0, 1, 1, 1, 0, -1, -1, -1, 0, 2, 2, 2, 0, -2, -2, -2};
    int rang = 0;
    int diswidth[MAX];
    int dishigth[MAX];
    for (int i = -2; i <= 2; i++)
    {
        for (int j = -2; j <= 2; j++)
        {
            if (i == 0 && j == 0)
                continue;
            diswidth[rang] = i;
            dishigth[rang] = j;
            rang++;
        }
    }
    
    int mul = 2;
    double ans=0,minans;
    int id;
    IplImage frame1, frame2;
    IplImage *pframe1=NULL, *pframe2=NULL;
    IplImage *pgrayframe1 = NULL, *pgrayframe2 = NULL;
    /*
    cv::Mat img2;
    IplImage imgTmp = img2;
    IplImage *input = cvCloneImage(&imgTmp);
    */
    int i;
    //get first
    video >> img;
    img1 = Getmat(img, rect);
    pframe1 = &(IplImage(img1));
    pgrayframe1 = cvCreateImage(cvGetSize(pframe1), IPL_DEPTH_8U, 1);
    pgrayframe2 = cvCreateImage(cvGetSize(pframe1), IPL_DEPTH_8U, 1);
    cvCvtColor(pframe1, pgrayframe1, CV_BGR2GRAY);
    while (1)
    {
        video >> img;

        minans = 0x3f3f3f3f;
        id = 0;
        for (i = 0; i < rang; i++)
        {
            rected[i] = rect;
            rected[i].x += diswidth[i] * mul;
            rected[i].y += dishigth[i] * mul;

            rected[i].x = rected[i].x>0 ? rected[i].x : 0;
            rected[i].y = rected[i].y>0 ? rected[i].y : 0;
            rected[i].x = rected[i].x + rected[i].width <img.cols ? rected[i].x : img.cols-rected[i].width;
            rected[i].y = rected[i].y + rected[i].height<img.rows ? rected[i].y : img.rows-rected[i].height;
            img2 = Getmat(img, rected[i]);
            pframe2 = &(IplImage(img2));

            cvCvtColor(pframe2, pgrayframe2, CV_BGR2GRAY);

            ans = CompareHist(pgrayframe1, pgrayframe2);

            if (ans < minans)
            {
                minans = ans;
                id = i;
            }
        }
            
        rect = rected[id];
            
        img1 = Getmat(img, rect);
        //frame1 = img1;
        //pframe1 = cvCloneImage(&frame1);
        pframe1 = &(IplImage(img1));
        cvCvtColor(pframe1, pgrayframe1, CV_BGR2GRAY);
            
        rectangle(img, rect, Scalar(0, 255, 0), 1);
            
            
        //imshow("vi", img1);
        //imshow("vi1", img2);
        //printf("%.7lf\n", ans);
        //img1 = img(rect);
        imshow("video", img);
        

        if (waitKey(25) > 0)
        {
            break;
        }
    }

    return 0;
}

 

posted @ 2015-11-27 18:49  一样菜  阅读(495)  评论(0编辑  收藏  举报