opencv 之 cvCalcOpticalFlowHS函數求光流 讀取視頻


首先:頭文件要加  #include "opencv2/legacy/legacy.hpp"   不加cvCalcOpticalFlowHS函數沒法調用

大體步驟:

1、 抓取視頻 img1 、 img2

2、求 光流 

3、根據光流數據 畫線

#include <cv.h>
#include <highgui.h>
#include "opencv2/legacy/legacy.hpp"
#include <math.h>
//#include "opencv2/core/core_c.h"
//#include "opencv2/core/core.hpp"
//#define _CRT_SECURE_NO_WARNINGS 1
//static const double pi = 3.14159265358979323846;
using namespace cv;
using namespace std;

/*
inline static void allocateOnDemand( IplImage **img, CvSize size, int depth, int channels)   //分配需求
{
    if ( *img != NULL ) return;
    *img = cvCreateImage( size, depth, channels );
    if ( *img == NULL )
    {
        fprintf(stderr, "Error: Couldn't allocate image. Out of memory?\n");
        exit(-1);
    }
}*/

int main()
{   
    IplImage * frame=NULL;
    int i, j, dx, dy, rows, cols;
    IplImage *src_img1=NULL, *src_img2=NULL, *dst_img1=NULL, *dst_img2=NULL;
    CvMat *velx, *vely;
    CvTermCriteria criteria;

    CvCapture *input_video = cvCreateFileCapture("demo-cut.avi");//cvCaptureFromFile SampleVideo.avi demo-cut.avi kunshan.avi
    if (input_video == NULL)
    {

    /* Either the video didn't exist OR it uses a codec OpenCV
    * doesn't support.
    */
    fprintf(stderr, "Error: Can't open video.\n");
    return -1; //就是函數結束后,返回個-1,經常用於錯誤返回 
    }
    cvQueryFrame( input_video ); 
    /* Read the video's frame size out of the AVI. */ //讀取視頻幀大小
    CvSize frame_size;
    frame_size.height =    (int) cvGetCaptureProperty( input_video, CV_CAP_PROP_FRAME_HEIGHT ); //獲得視頻獲取結構的屬性
    frame_size.width =    (int) cvGetCaptureProperty( input_video, CV_CAP_PROP_FRAME_WIDTH );

    long number_of_frames;
    /* Go to the end of the AVI (ie: the fraction is "1") *///設置視頻獲取屬性
    cvSetCaptureProperty( input_video, CV_CAP_PROP_POS_AVI_RATIO, 1. );
    /* Now that we're at the end, read the AVI position in frames */
    number_of_frames = (int) cvGetCaptureProperty( input_video, CV_CAP_PROP_POS_FRAMES ); 
    /* Return to the beginning *///設置視頻獲取屬性
    cvSetCaptureProperty( input_video, CV_CAP_PROP_POS_FRAMES, 0. ); //單位為幀數的位置(只對視頻文件有效

    long current_frame = 0;
    //進入循環
    while(true)
    {
        cvSetCaptureProperty( input_video, CV_CAP_PROP_POS_FRAMES, current_frame );
        frame = cvQueryFrame( input_video );
        dst_img2 =(IplImage *) cvClone (frame);
        if (frame == NULL)
        {
            /* Why did we get a NULL frame? We shouldn't be at the end. */
            fprintf(stderr, "Error: Hmm. The end came sooner than we thought.\n");
            return -1;
        }
        //allocateOnDemand( &frame1_1C, frame_size, IPL_DEPTH_8U, 1 );
//        cvConvertImage(frame, frame1_1C, CV_CVTIMG_SWAP_RB); 
        IplImage* frame1_1C = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 1);//創建目標圖像
        cvCvtColor(frame,frame1_1C,CV_BGR2GRAY);//cvCvtColor(src,des,CV_BGR2GRAY)

    //    cvCvtColor(frame,frame1_1C,CV_BGR2GRAY); 
       //  frame1_1C=frame;

        frame = cvQueryFrame( input_video );
        if (frame == NULL)
        {
            fprintf(stderr, "Error: Hmm. The end came sooner than we thought.\n");
            return -1;
        }
        IplImage* frame2_1C = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 1);//創建目標圖像
        cvCvtColor(frame,frame2_1C,CV_BGR2GRAY);//cvCvtColor(src,des,CV_BGR2GRAY)

    

    cols = frame1_1C->width;
    rows = frame1_1C->height;
    velx = cvCreateMat (rows, cols, CV_32FC1);
    vely = cvCreateMat (rows, cols, CV_32FC1);
    cvSetZero (velx);
    cvSetZero (vely);
    criteria = cvTermCriteria (CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 20, 0.1);

    // (2)計算(HS)
    cvCalcOpticalFlowHS (frame1_1C, frame2_1C, 0, velx, vely, 100.0, criteria);
    //(3) 畫圖

    for (i = 0; i < cols; i += 5) {
        for (j = 0; j < rows; j += 5) {
            dx = (int) cvGetReal2D (velx, j, i);
            dy = (int) cvGetReal2D (vely, j, i);
        if ((5<dx<10)&&(5<dy<10))
                cvLine (dst_img2, cvPoint (i, j), cvPoint (i + dx, j + dy), CV_RGB (255, 0, 0), 1, CV_AA, 0);
        }
    }

    cvNamedWindow ("ImageHS", 1);
    cvShowImage ("ImageHS", dst_img2);

    int key_pressed;
    key_pressed = cvWaitKey(30);
    if (key_pressed == 'b' || key_pressed == 'B') break;
    else 
        current_frame=current_frame+2;
    /* Don't run past the front/end of the AVI. */
    //if (current_frame < 0) current_frame = 0;
    if (current_frame >= number_of_frames - 1) current_frame = number_of_frames - 2;

    
    }


    cvDestroyWindow ("ImageHS");
//    cvDestroyWindow ("ImageLK");
    cvReleaseImage (&src_img1);
    cvReleaseImage (&src_img2);
    cvReleaseImage (&dst_img1);
    cvReleaseImage (&dst_img2);
    cvReleaseMat (&velx);
    cvReleaseMat (&vely);
}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM