• 基于头部跟踪初代码测试


    #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
    ", cvCompareHist(Histogram1, Histogram2, CV_COMP_BHATTACHARYYA));
    
    
        // CV_COMP_CORREL, CV_COMP_INTERSECT这两种直方图的比较,值越大,说明图形越相似  
        //printf("CV_COMP_CORREL : %.4f
    ", cvCompareHist(Histogram1, Histogram2, CV_COMP_CORREL));
        //printf("CV_COMP_INTERSECT : %.4f
    ", 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
    ", ans);
            //img1 = img(rect);
            imshow("video", img);
            
    
            if (waitKey(25) > 0)
            {
                break;
            }
        }
    
        return 0;
    }
  • 相关阅读:
    rabbitMq交换机direct、topics
    rabbitMq及安装、fanout交换机-分发(发布/订阅)
    rabbitmq
    .sh文件启动 jenkins
    雪花算法、二进制运算
    收银台数据库存储AES加解密
    数据库事物
    excel技巧
    日期计算、正则、sequence、索引、表连接、mybatis
    oracle常用函数介绍
  • 原文地址:https://www.cnblogs.com/mypsq/p/5001520.html
Copyright © 2020-2023  润新知