Farneback光流法检测前景的一个例子

一、前言

本文使用opencv的calcOpticalFlowFarneback光流法计算图像的运动光流,并显示计算得的光流强度,视频大小为640*480,但速度很慢,计算速度为300ms左右一帧。不知使用GPU版的光流法能快多少。

二、简单的代码

#include 
#include 
#include 
#include 
using namespace cv;

#include 
#include 
#include 

#include   

static VideoCapture cap;
static  unsigned int frame_count = 0;

void compute_absolute_mat(const Mat& in , Mat & out );


int main()
{
	cap.open("35450-47160.avi");

	if (!cap.isOpened()){
		std::cout << "视频读取失败!" << std::endl;
		return -1;
	}

	Mat img ,gray,prvGray, optFlow ,absoluteFlow, img_for_show;
	while (1){
		cap >> img;
		if (img.empty()) break;

		cvtColor(img , gray ,CV_BGR2GRAY);
		if (prvGray.data){
			calcOpticalFlowFarneback(prvGray, gray, optFlow, 0.5, 3, 15, 3, 5, 1.2, 0);	//使用论文参数		
			normalize(absoluteFlow, img_for_show, 0, 255, NORM_MINMAX, CV_8UC1);
			
			imshow("opticalFlow", img_for_show);
			imshow("resource", img);
		}
		cv::swap(prvGray, gray);

		waitKey(1);
	}
	
	return 0;
}

void compute_absolute_mat(const Mat& in, Mat & out)
{
	if (out.empty()){
		out.create(in.size(), CV_32FC1);
	}

	const Mat_ _in = in;
	//遍历吧,少年
	for (int i = 0; i < in.rows; ++i){
		float *data = out.ptr(i);
		for (int j = 0; j < in.cols; ++j){
			double s = _in(i, j)[0] * _in(i, j)[0] + _in(i, j)[1] * _in(i, j)[1];
			if (s>1){
				data[j] = std::sqrt(s);
			}
			else{
				data[j] = 0.0;
			}
			
		}
	}
}
三、结果

四、gpu版光流法

使用opencv中基于GPU实现的光流法gpu::FarnebackOpticalFlow,速度上为50ms左右一帧,提升好几倍

五、代码

#include 
#include 
#include 
#include 
#include 

using namespace cv;
#include 
#include  

void compute_absolute_mat(Mat &in_x, Mat &in_y, Mat& _out);

int main()
{
	const std::string fname("1.avi");

	VideoCapture cap(fname);
	if (!cap.isOpened()){
		std::cerr << "无法打开视频";
		return -1;
	}

	Mat img;
	Mat prev, curr, flowx, flowy,  fb,fb_show;;
	gpu::GpuMat curr_gpu, prev_gpu, flowx_gpu, flowy_gpu , fb_gpu;
	gpu::FarnebackOpticalFlow fbOptFlow;
	while (cap.read(img)){
		cvtColor(img, curr , CV_BGR2GRAY);
		if (!prev.empty()){
			curr_gpu.upload(curr);
			prev_gpu.upload(prev);

			fbOptFlow(curr_gpu, prev_gpu, flowx_gpu, flowy_gpu);	
			flowx_gpu.download(flowx);
			flowy_gpu.download(flowy);
			compute_absolute_mat(flowx, flowy, fb);
			normalize(fb, fb_show, 0, 255, NORM_MINMAX, CV_8UC1);

			curr_gpu.release();
			prev_gpu.release();
			flowx_gpu.release();
			flowy_gpu.release();
			fb_gpu.release();
		
			imshow("gpu_opticalFlow", fb_show);
			imshow("resource", img);
			waitKey(1);

		}
		cv::swap(prev, curr);
		
	}

	return 0;
}

void compute_absolute_mat(Mat &in_x, Mat &in_y , Mat& _out)
{
	if (_out.empty()) _out.create(in_x.size(), CV_32FC1);

	for (int i = 0; i < _out.rows; ++i){
		float * ptr_x = in_x.ptr(i);
		float * ptr_y = in_x.ptr(i);
		float *data = _out.ptr(i);
		for (int j = 0; j < _out.cols; ++j){
			double s = ptr_x[j] * ptr_x[j] + ptr_y[j] * ptr_y[j];
			if (s>1){
				data[j] = std::sqrt(s);
			}
			else{
				data[j] = 0.0;
			}
		}

	}

}
六、结果
Farneback光流法检测前景的一个例子_第1张图片









你可能感兴趣的:(opencv)