opencv4.2.0 视频去抖动算法代码
生活随笔
收集整理的這篇文章主要介紹了
opencv4.2.0 视频去抖动算法代码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
// videostabDemo.cpp : 此文件包含 "main" 函數。程序執行將在此處開始并結束。
//#include <iostream>#include <opencv2/opencv.hpp>
#include <opencv2/videostab.hpp>
#include <string>
#include <iostream>#pragma comment(lib, "opencv_core420.lib")
#pragma comment(lib, "opencv_videoio420.lib")
#pragma comment(lib, "opencv_highgui420.lib")
#pragma comment(lib, "opencv_features2d420.lib")
#pragma comment(lib, "opencv_videostab420.lib")using namespace std;
using namespace cv;
using namespace cv::videostab;string inputPath = "video_seq_2.avi";
string outputPath = "outputstab001.avi";//001 must// 視頻穩定輸出
void videoOutput(Ptr<IFrameSource> stabFrames, string outputPath)
{VideoWriter writer;cv::Mat stabFrame;int nframes = 0;// 設置輸出幀率double outputFps = 10;// 遍歷搜索視頻幀while (!(stabFrame = stabFrames->nextFrame()).empty()){nframes++;// 輸出視頻穩定幀if (!outputPath.empty()){if (!writer.isOpened())writer.open(outputPath, VideoWriter::fourcc('M', 'J', 'P', 'G'), outputFps, stabFrame.size());writer << stabFrame;}imshow("stabFrame", stabFrame);// esc鍵退出char key = static_cast<char>(waitKey(100));if (key == 27){cout << endl;break;}}writer.release();std::cout << "nFrames: " << nframes << endl;std::cout << "finished " << endl;
}void cacStabVideo(Ptr<IFrameSource> stabFrames, string srcVideoFile)
{try{Ptr<VideoFileSource> srcVideo = makePtr<VideoFileSource>(inputPath);cout << "frame count: " << srcVideo->count() << endl;// 運動估計double estPara = 0.1;Ptr<MotionEstimatorRansacL2> est =makePtr<MotionEstimatorRansacL2>(MM_AFFINE);// Ransac參數設置RansacParams ransac = est->ransacParams();ransac.size = 3;ransac.thresh = 5;ransac.eps = 0.5;// Ransac計算est->setRansacParams(ransac);est->setMinInlierRatio(estPara);// Fast特征檢測Ptr<FastFeatureDetector> feature_detector =FastFeatureDetector::create();// 運動估計關鍵點匹配Ptr<KeypointBasedMotionEstimator> motionEstBuilder =makePtr<KeypointBasedMotionEstimator>(est);// 設置特征檢測器motionEstBuilder->setDetector(feature_detector);Ptr<IOutlierRejector> outlierRejector = makePtr<NullOutlierRejector>();motionEstBuilder->setOutlierRejector(outlierRejector);// 3-Prepare the stabilizerStabilizerBase *stabilizer = 0;// first, prepare the one or two pass stabilizerbool isTwoPass = 1;int radius_pass = 15;if (isTwoPass){// with a two pass stabilizerbool est_trim = true;TwoPassStabilizer *twoPassStabilizer = new TwoPassStabilizer();twoPassStabilizer->setEstimateTrimRatio(est_trim);twoPassStabilizer->setMotionStabilizer(makePtr<GaussianMotionFilter>(radius_pass));stabilizer = twoPassStabilizer;}else{// with an one pass stabilizerOnePassStabilizer *onePassStabilizer = new OnePassStabilizer();onePassStabilizer->setMotionFilter(makePtr<GaussianMotionFilter>(radius_pass));stabilizer = onePassStabilizer;}// second, set up the parametersint radius = 15;double trim_ratio = 0.1;bool incl_constr = false;stabilizer->setFrameSource(srcVideo);stabilizer->setMotionEstimator(motionEstBuilder);stabilizer->setRadius(radius);stabilizer->setTrimRatio(trim_ratio);stabilizer->setCorrectionForInclusion(incl_constr);stabilizer->setBorderMode(BORDER_REPLICATE);// cast stabilizer to simple frame source interface to read stabilized framesstabFrames.reset(dynamic_cast<IFrameSource*>(stabilizer));// 4-videoOutput the stabilized frames. The results are showed and saved.videoOutput(stabFrames, outputPath);}catch (const exception &e){cout << "error: " << e.what() << endl;stabFrames.release();}
}//對于實時視頻流,使用緩存50幀來處理
int main(int argc, char* argv[])
{Ptr<IFrameSource> stabFrames;// 輸入輸出視頻準備cacStabVideo(stabFrames, inputPath);stabFrames.release();getchar();return 0;
}
?
總結
以上是生活随笔為你收集整理的opencv4.2.0 视频去抖动算法代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OpenCV中VideoWriter输出
- 下一篇: python 图形界面库对比