DVE C++ OpenCV代码
时间: 2023-11-14 14:57:46 浏览: 155
第12页-基于labview的振动信号采集系统设计
以下是使用OpenCV实现DVE效果的示例代码:
```c++
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
if (argc != 3)
{
cout << "Usage: ./dve <input_video> <output_video>" << endl;
return -1;
}
VideoCapture cap(argv[1]);
if (!cap.isOpened())
{
cout << "Error opening video file" << endl;
return -1;
}
int frame_width = cap.get(CAP_PROP_FRAME_WIDTH);
int frame_height = cap.get(CAP_PROP_FRAME_HEIGHT);
int fps = cap.get(CAP_PROP_FPS);
Size frame_size(frame_width, frame_height);
VideoWriter output_video(argv[2], VideoWriter::fourcc('M', 'J', 'P', 'G'), fps, frame_size, true);
Mat frame;
Mat gray_frame;
Mat prev_frame;
Mat curr_frame;
cap.read(frame);
cvtColor(frame, prev_frame, COLOR_BGR2GRAY);
while (true)
{
cap.read(frame);
if (frame.empty())
break;
cvtColor(frame, curr_frame, COLOR_BGR2GRAY);
Mat flow;
calcOpticalFlowFarneback(prev_frame, curr_frame, flow, 0.5, 3, 15, 3, 5, 1.2, 0);
Mat flow_parts[2];
split(flow, flow_parts);
Mat magnitude, angle;
cartToPolar(flow_parts[0], flow_parts[1], magnitude, angle, true);
Mat hsv_planes[3];
hsv_planes[0] = angle;
normalize(magnitude, hsv_planes[1], 0, 255, NORM_MINMAX);
hsv_planes[2] = Mat::ones(angle.size(), CV_8UC1);
Mat hsv;
merge(hsv_planes, 3, hsv);
Mat bgr;
cvtColor(hsv, bgr, COLOR_HSV2BGR);
output_video.write(bgr);
prev_frame = curr_frame.clone();
}
cap.release();
output_video.release();
return 0;
}
```
这段代码使用了Farneback算法计算了光流,并将光流可视化为色彩图像。最后输出结果到一个视频文件中。注意需要引入OpenCV头文件,并链接OpenCV库。
阅读全文