opticalflow + openCV
2017-01-03 14:49
246 查看
#include "stdafx.h"
#include <iostream>
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
#define UNKNOWN_FLOW_THRESH 1e9
// Color encoding of flow vectors from:
// http://members.shaw.ca/quadibloc/other/colint.htm
// This code is modified from:
// http://vision.middlebury.edu/flow/data/
void makecolorwheel(vector<Scalar> &colorwheel)
{
int RY = 15;
int YG = 6;
int GC = 4;
int CB = 11;
int BM = 13;
int MR = 6;
int i;
for (i = 0; i < RY; i++) colorwheel.push_back(Scalar(255,255*i/RY,0));
for (i = 0; i < YG;i++) colorwheel.push_back(Scalar(255-255*i/YG,255,0));
for (i = 0; i < GC; i++) colorwheel.push_back(Scalar(0,255,255*i/GC));
for (i = 0; i < BM; i++) colorwheel.push_back(Scalar(255*i/BM,0,255));
for (i = 0; i < MR; i++) colorwheel.push_back(Scalar(255,0,255-255*i/MR));
}
void motionToColor(Mat flow,Mat &color)
{
if (color.empty())
color.create(flow.rows, flow.cols, CV_8UC3);
static vector<Scalar> colorwheel; //Scalar r,g,b
if(colorwheel.empty())
makecolorwheel(colorwheel);
// determine motion range:
float maxrad = -1;
// Find max flow to normalize fx and fy
for (int i= 0; i < flow.rows; ++i)
{
for (int j = 0; j < flow.cols; ++j)
{
Vec2f flow_at_point=flow.at<Vec2f>(i,j);
float fx=flow_at_point[0];
float fy = flow_at_point[1];
if ((fabs(fx)>UNKNOWN_FLOW_THRESH)||(fabs(fy)>UNKNOWN_FLOW_THRESH))
continue;
float rad = sqrt(fx * fx + fy * fy);
maxrad = maxrad > rad ? maxrad : rad;
}
}
for(int i= 0; i < flow.rows; ++i)
{
for(int j = 0;j<flow.cols;++j)
{
uchar *data =color.data+color.step[0]* i+ color.step[1] * j;
Vec2f flow_at_point=flow.at<Vec2f>(i,j);
float fx =flow_at_point[0]/maxrad;
float fy = flow_at_point[1] / maxrad;
if ((fabs(fx)>UNKNOWN_FLOW_THRESH)||(fabs(fy)>UNKNOWN_FLOW_THRESH))
{
data[0] = data[1]=data[2]=0;
continue;
}
float rad = sqrt(fx*fx+fy*fy);
float angle = atan2(-fy, -fx)/CV_PI;
float fk = (angle + 1.0)/ 2.0 * (colorwheel.size()-1);
int k0 = (int)fk;
int k1=(k0 + 1) % colorwheel.size();
float f = fk - k0;
//f = 0; // uncomment to see original color wheel
for (int b = 0; b < 3; b++)
{
float col0 = colorwheel[k0][b] / 255.0;
float col1 = colorwheel[k1][b] / 255.0;
float col = (1 - f) * col0 + f * col1;
if (rad <= 1)
col = 1 - rad*(1 - col);// increase saturation with radius
else
col *= .75;// out of range
data[2 - b] =(int)(255.0 * col);
}
}
}
}
int main(int, char**)
{
VideoCapture cap;
cap.open(0);
cap.open("../22.avi");
if(!cap.isOpened())
return -1;
Mat prevgray, gray, flow, cflow,frame;
Mat motion2color;
cv::Size default_size;
default_size.width = 235;
default_size.height = 189;
namedWindow("flow", 1);
for(;;) //死循环
{
double t = (double)cvGetTickCount();
cap>>frame;
cv::resize(frame, frame, default_size);
cvtColor(frame, gray, CV_BGR2GRAY);
imshow("original", frame);
if( prevgray.data )
{
calcOpticalFlowFarneback(prevgray, gray, flow, 0.5, 3, 15, 3, 5, 1.2, 0);
motionToColor(flow, motion2color);
imshow("flow", motion2color);
}
if(waitKey(10)>=0)
break;
std::swap(prevgray, gray);
t=(double)cvGetTickCount() -t;
cout <<"cost time:"<<t/((double)cvGetTickFrequency()*1000.)<<endl;
}
}
#include <iostream>
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
#define UNKNOWN_FLOW_THRESH 1e9
// Color encoding of flow vectors from:
// http://members.shaw.ca/quadibloc/other/colint.htm
// This code is modified from:
// http://vision.middlebury.edu/flow/data/
void makecolorwheel(vector<Scalar> &colorwheel)
{
int RY = 15;
int YG = 6;
int GC = 4;
int CB = 11;
int BM = 13;
int MR = 6;
int i;
for (i = 0; i < RY; i++) colorwheel.push_back(Scalar(255,255*i/RY,0));
for (i = 0; i < YG;i++) colorwheel.push_back(Scalar(255-255*i/YG,255,0));
for (i = 0; i < GC; i++) colorwheel.push_back(Scalar(0,255,255*i/GC));
for (i = 0; i < BM; i++) colorwheel.push_back(Scalar(255*i/BM,0,255));
for (i = 0; i < MR; i++) colorwheel.push_back(Scalar(255,0,255-255*i/MR));
}
void motionToColor(Mat flow,Mat &color)
{
if (color.empty())
color.create(flow.rows, flow.cols, CV_8UC3);
static vector<Scalar> colorwheel; //Scalar r,g,b
if(colorwheel.empty())
makecolorwheel(colorwheel);
// determine motion range:
float maxrad = -1;
// Find max flow to normalize fx and fy
for (int i= 0; i < flow.rows; ++i)
{
for (int j = 0; j < flow.cols; ++j)
{
Vec2f flow_at_point=flow.at<Vec2f>(i,j);
float fx=flow_at_point[0];
float fy = flow_at_point[1];
if ((fabs(fx)>UNKNOWN_FLOW_THRESH)||(fabs(fy)>UNKNOWN_FLOW_THRESH))
continue;
float rad = sqrt(fx * fx + fy * fy);
maxrad = maxrad > rad ? maxrad : rad;
}
}
for(int i= 0; i < flow.rows; ++i)
{
for(int j = 0;j<flow.cols;++j)
{
uchar *data =color.data+color.step[0]* i+ color.step[1] * j;
Vec2f flow_at_point=flow.at<Vec2f>(i,j);
float fx =flow_at_point[0]/maxrad;
float fy = flow_at_point[1] / maxrad;
if ((fabs(fx)>UNKNOWN_FLOW_THRESH)||(fabs(fy)>UNKNOWN_FLOW_THRESH))
{
data[0] = data[1]=data[2]=0;
continue;
}
float rad = sqrt(fx*fx+fy*fy);
float angle = atan2(-fy, -fx)/CV_PI;
float fk = (angle + 1.0)/ 2.0 * (colorwheel.size()-1);
int k0 = (int)fk;
int k1=(k0 + 1) % colorwheel.size();
float f = fk - k0;
//f = 0; // uncomment to see original color wheel
for (int b = 0; b < 3; b++)
{
float col0 = colorwheel[k0][b] / 255.0;
float col1 = colorwheel[k1][b] / 255.0;
float col = (1 - f) * col0 + f * col1;
if (rad <= 1)
col = 1 - rad*(1 - col);// increase saturation with radius
else
col *= .75;// out of range
data[2 - b] =(int)(255.0 * col);
}
}
}
}
int main(int, char**)
{
VideoCapture cap;
cap.open(0);
cap.open("../22.avi");
if(!cap.isOpened())
return -1;
Mat prevgray, gray, flow, cflow,frame;
Mat motion2color;
cv::Size default_size;
default_size.width = 235;
default_size.height = 189;
namedWindow("flow", 1);
for(;;) //死循环
{
double t = (double)cvGetTickCount();
cap>>frame;
cv::resize(frame, frame, default_size);
cvtColor(frame, gray, CV_BGR2GRAY);
imshow("original", frame);
if( prevgray.data )
{
calcOpticalFlowFarneback(prevgray, gray, flow, 0.5, 3, 15, 3, 5, 1.2, 0);
motionToColor(flow, motion2color);
imshow("flow", motion2color);
}
if(waitKey(10)>=0)
break;
std::swap(prevgray, gray);
t=(double)cvGetTickCount() -t;
cout <<"cost time:"<<t/((double)cvGetTickFrequency()*1000.)<<endl;
}
}
相关文章推荐
- OpenCV中CalcOpticalFlowFarneback()函数分析
- OpenCV图像处理-光流法-原理分析1-calcOpticalFlowFarneback
- LK OpticalFlow+OpenCV3
- OpenCV Python calcOpticalFlowFarneback
- OpenCV中CalcOpticalFlowFarneback()函数分析
- opencv中CalcOpticalFlowPyrLK实现的光流法理解
- 光流(optical flow) 光流法(optical flow method) 光流场(optical flow field)
- Optical Flow related Tutorials
- MAC中Windows下配置Anaconda3+Tensorflow+OpenCV
- windows+python3.6.3+OpenCV3.3.0+TensorFlow+PyCharm最全安装
- Ubuntu16.04 +cuda8.0+cudnn+opencv+caffe+theano+tensorflow配置明细
- undefined reference to `cv::calcOpticalFlowFarnebackcalcOpticalFlowFarneback
- Optical_Flow(4)
- calcOpticalFlowFarneback
- Farneback 光流算法详解与 calcOpticalFlowFarneback 源码分析
- 解读: FlowNet learning optical flow with convolutional networks
- CalcOpticalFlowPyrLK
- OpenCV编程-> “cvCalcOpticalFlowHS”: 找不到标识符
- FlowNet: Learning Optical Flow with Convolutional Networks
- Windows 下安装 tensorflow & keras & opencv 的避坑指南!