光流插帧(C++版)
光流插帧好多都是Python版的,这次来翻译成C++版供大家参考。
#include <stdio.h>
#include <highgui.h>
#include <opencv2/opencv.hpp>
#include <cv.h>
#include <math.h>
#include <iostream>
using namespace std;
using namespace cv;
int main(int argc, char **argv)
{
//读取两张图片
Mat img_1 = imread(argv[1], CV_LOAD_IMAGE_COLOR);
Mat img_2 = imread(argv[2], CV_LOAD_IMAGE_COLOR);
//灰度转换
Mat img_1_gray, img_2_gray;
cvtColor(img_1, img_1_gray, CV_BGR2GRAY);
cvtColor(img_2, img_2_gray, CV_BGR2GRAY);
//定义mapx和mapy用于存放向量表
Mat flow, mapx, mapy, new_img;
mapx.create(img_2.size(), CV_32FC1);
mapy.create(img_2.size(), CV_32FC1);
//计算稠密光流
calcOpticalFlowFarneback(img_1_gray, img_2_gray, flow, 0.5, 1, 30,3, 5, 1.2, 0);
//计算向量表
for(float m=0; m<flow.rows; m++)
{
for(float n=0; n<flow.cols; n++)
{
Point2f flow_x_y = flow.at<Point2f>(m,n);
mapx.at<float>(m,n) = n - flow.at<Point2f>(m,n).x;
mapy.at<float>(m,n) = m - flow.at<Point2f>(m,n).y;
}
}
//插帧显示
remap(img_2, new_img, mapx, mapy, INTER_LINEAR);
imshow("new_img", new_img);
waitKey(0);
}