光流法 OPENCV

#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/ml/ml.hpp"
#include <opencv/cv.h>
#include <iostream>
#include <math.h>
#include <stdio.h>
using namespace std;
using namespace cv;
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* img=cvLoadImage("C:\\flow_a.png", 1);
	IplImage * previmage=cvCreateImage( cvGetSize( img ), IPL_DEPTH_8U, 1);  
	cvConvertImage(img,previmage,CV_CVTIMG_FLIP);  

	CvSize frame_size = cvGetSize( img );
	
	
	IplImage* cur = cvLoadImage("C:\\flow_b.png",1);
	IplImage * curimage=cvCreateImage( cvGetSize(cur), IPL_DEPTH_8U, 1);  
	cvConvertImage(cur,curimage,CV_CVTIMG_FLIP);  

	/*
	1:找到特征值
	2:利用tomais计算光流的方法 =_= cvGoodFeaturesToTrack
	3:显示出来,画线即可,线的两个顶点是 features[1]-features[2]
	*/

	IplImage* erg_image,*temp_image;
	allocateOnDemand( &erg_image, frame_size, IPL_DEPTH_32F, 1 );
	allocateOnDemand( &temp_image, frame_size, IPL_DEPTH_32F, 1 );
	int number_of_corners = 400;
	CvPoint2D32f corners[400];//特征值存储在这个数组中...
	cvGoodFeaturesToTrack(previmage,erg_image,temp_image,corners,&number_of_corners,0.01,0.01,NULL);
	cout<<number_of_corners<<" 一共有这么多个特征点"<<endl;
	

	CvPoint2D32f corners2[400];//特征值存储在这个数组中.
    char optical_flow_found_feature[400];
	float optical_flow_feature_error[400];
	CvSize optical_flow_window = cvSize(3,3);
	CvTermCriteria optical_flow_termination_criteria
			= cvTermCriteria( CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 20, .3 );
	IplImage *pyramid1,*pyramid2;
	allocateOnDemand( &pyramid1, frame_size, IPL_DEPTH_8U, 1 );
	allocateOnDemand( &pyramid2, frame_size, IPL_DEPTH_8U, 1 );
	cvCalcOpticalFlowPyrLK(previmage, curimage, pyramid1, pyramid2, corners, corners2, number_of_corners, optical_flow_window, 5, optical_flow_found_feature, optical_flow_feature_error, optical_flow_termination_criteria, 0 );
		
	//imshow("corners",prev);
	//画出特征值
	for(int i= 0 ; i< number_of_corners; i ++)
	{
		if(optical_flow_found_feature[i] == 0 )
			continue;	
		Point p,q;
		p = corners[i];
		q = corners2[i];
			//circle(prev,corners[i],1,Scalar(0,255,255),3,8,0);
			cvLine(cur,p,q,Scalar(255,0,0),1,CV_AA,0);
			cvCircle(cur,q,3,Scalar(0,255,255));
	}
	cvShowImage("corners",cur);
	waitKey(0);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值