首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将C/++ OpenCV程序从视频稳定程序改为CUDA程序

将C/++ OpenCV程序从视频稳定程序改为CUDA程序
EN

Stack Overflow用户
提问于 2014-11-25 03:52:10
回答 1查看 2.8K关注 0票数 2

我正在做一个C++视频稳定/防抖动程序:-获取参考系上的兴趣点(使用FAST,SURF,Shi-Matoshi或SIFT,可能会尝试更多)-使用calcOpticalFlowPyrLK计算卢卡斯-卡纳德光流-获取单应矩阵-使用warPerspective校正抖动图像(参见下面的代码)

代码语言:javascript
复制
//Calculate the Lucas Kanade optical flow
calcOpticalFlowPyrLK(original, distorted, refFeatures, currFeatures, featuresFound, err);   

//Find the homography between the current frame's features and the reference ones's
if(homographyRansac){
    homography = findHomography(currFeatures, refFeatures, CV_RANSAC); /*CV_RANSAC: Random sample consensus (RANSAC) is an iterative method to
    estimate parameters of a mathematical model from a set of observed data which contains outliers */
}else{
    homography = findHomography(currFeatures, refFeatures, 0);
}


//We use warpPerspective once on the distorted image to get the resulting fixed image
if(multiChannel){
    //Spliting into channels        
    vector <Mat> rgbChannels(channels), fixedChannels;
    split(distortedCopy, rgbChannels);
    recovered = Mat(reSized, CV_8UC3);
    //We apply the transformation to each channel
    for(int i = 0; i < channels; i ++){
        Mat tmp;
        warpPerspective(rgbChannels[i], tmp, homography, reSized);
        fixedChannels.push_back(tmp);
    }
    //Merge the result to obtain a 3 channel corrected image
    merge(fixedChannels, recovered);
}else{
    warpPerspective(distorted, recovered, homography, reSized);
}

如果您有任何替代我的稳定解决方案,请随时这样说,但这不是本线程的主题。

由于所有这一切都需要很多时间(在我的i5电脑上大约每帧300ms,所以对于一个30分钟的视频来说,这是一个很长的时间),我正在考虑使用CUDA来加快速度。我已经安装好了,可以正常工作了,但是我不确定下一步该怎么做。我做了一些测试,我知道最耗时的操作是分别使用calcOpticalFlowPyrLK和warpPerspective获取光流和帧校正。因此,理想情况下,至少在一开始,我将只使用这两个函数的CUDA版本,其余的保持不变。

这个是可能的吗?或者我需要重写所有内容?

谢谢

EN

回答 1

Stack Overflow用户

发布于 2014-11-26 01:53:17

从CUDA3.0开始,可以使用OpenCV实现视频稳定。建议使用已有的实现,而不是编写自己的实现,除非您确定您的版本会更好或更快。

以下是演示如何使用OpenCV视频稳定模块来稳定视频的最小代码。

代码语言:javascript
复制
#include <opencv2/highgui.hpp>
#include <opencv2/videostab.hpp>

using namespace cv::videostab;

int main()
{
    std::string videoFile = "shaky_video.mp4";

    MotionModel model = cv::videostab::MM_TRANSLATION; //Type of motion to compensate
    bool use_gpu = true; //Select CUDA version or "regular" version

    cv::Ptr<VideoFileSource> video = cv::makePtr<VideoFileSource>(videoFile,true);
    cv::Ptr<OnePassStabilizer> stabilizer = cv::makePtr<OnePassStabilizer>();

    cv::Ptr<MotionEstimatorBase> MotionEstimator = cv::makePtr<MotionEstimatorRansacL2>(model);

    cv::Ptr<ImageMotionEstimatorBase> ImageMotionEstimator;

    if (use_gpu)
        ImageMotionEstimator = cv::makePtr<KeypointBasedMotionEstimatorGpu>(MotionEstimator);
    else
        ImageMotionEstimator = cv::makePtr<KeypointBasedMotionEstimator>(MotionEstimator);

    stabilizer->setFrameSource(video);
    stabilizer->setMotionEstimator(ImageMotionEstimator);
    stabilizer->setLog(cv::makePtr<cv::videostab::NullLog>()); //Disable internal prints

    std::string windowTitle = "Stabilized Video";

    cv::namedWindow(windowTitle, cv::WINDOW_AUTOSIZE);

    while(true)
    {
        cv::Mat frame = stabilizer->nextFrame();

        if(frame.empty())   break;

        cv::imshow(windowTitle,frame);
        cv::waitKey(10);
    }

    return 0;
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27113018

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档