本文主要是介绍OpenCV 光流法总结,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
0.概述
1.原理说明
2.代码实现
#include <iostream>
#include <opencv2/opencv.hpp>int main(int argc, char** argv) {if(argc != 2) {std::cerr << "Usage: " << argv[0] << " <video_path>" << std::endl;return -1;}cv::VideoCapture cap(argv[1]);if(!cap.isOpened()) {std::cerr << "Error: Couldn't open the video file." << std::endl;return -1;}cv::Mat oldFrame, oldGray;std::vector<cv::Point2f> oldCorners;// Parameters for Shi-Tomasi corner detectionint maxCorners = 100;double qualityLevel = 0.3;double minDistance = 7;int blockSize = 7;cap >> oldFrame;cv::cvtColor(oldFrame, oldGray, cv::COLOR_BGR2GRAY);// Detect corners in the first framecv::goodFeaturesToTrack(oldGray, oldCorners, maxCorners, qualityLevel, minDistance, cv::Mat(), blockSize);// Color for optical flowcv::Scalar color(0, 255, 0); // Greenwhile(true) {cv::Mat frame, gray;cap >> frame;if(frame.empty()) {break;}cv::cvtColor(frame, gray, cv::COLOR_BGR2GRAY);std::vector<cv::Point2f> newCorners;std::vector<uchar> status;std::vector<float> err;// Calculate optical flow using Lucas-Kanade methodcv::calcOpticalFlowPyrLK(oldGray, gray, oldCorners, newCorners, status, err);// Draw the motion vectorsfor(size_t i = 0; i < oldCorners.size(); i++) {if(status[i]) {cv::line(frame, oldCorners[i], newCorners[i], color, 2);cv::circle(frame, newCorners[i], 5, color, -1);}}// Display the resultcv::imshow("Optical Flow - Lucas-Kanade", frame);if(cv::waitKey(30) == 27) { // Exit on pressing 'Esc' keybreak;}// Update the previous frame and cornersoldGray = gray.clone();oldCorners = newCorners;}cap.release();cv::destroyAllWindows();return 0;
}
这篇关于OpenCV 光流法总结的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!