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

一、前言

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

二、简单的代码

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/video/video.hpp>
using namespace cv;

#include <iostream>
#include <vector>
#include <cmath>

#include <windows.h>  

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_<Vec2f> _in = in;
	//遍历吧,少年
	for (int i = 0; i < in.rows; ++i){
		float *data = out.ptr<float>(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 <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/gpu/gpu.hpp>
#include <opencv2/gpu/gpumat.hpp>

using namespace cv;
#include <iostream>
#include <windows.h> 

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<float>(i);
		float * ptr_y = in_x.ptr<float>(i);
		float *data = _out.ptr<float>(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;
			}
		}

	}

}
六、结果









  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值