在OpenCV中使用cv :: gpu :: FAST_GPU和cv :: gpu :: PyrLKOpticalFlow 2.4.13.6

时间:2018-03-20 18:52:28

标签: opencv computer-vision gpu assertion

我正在尝试按照此处提供的示例进行操作:PyrLKOpticalFlow Example

但是尝试使用FAST_GPU代替GoodFeaturesToTrack。但是,一旦我调用FAST_GPU检测器并将返回的GpuMat格式的点传递给PyrLKOpticalFlow,它就不会运行并且我得到一个断言错误。我已经尝试了几件事来改变GpuMat,但我不能让PyrLKOpticalFlow接受FAST_GPU检测到的点。

  

OpenCV错误:在calcOpticalFlowPyrLK中,断言失败((npoints = prevPtsMat.checkVector(2,CV_32F,true))> = 0),文件/ home / ubuntu / build-opencv / opencv / modules / video / src / lkpyramid .cpp,第845行终止在抛出'cv :: Exception'的实例后调用what():/ home /ubuntu / build-opencv / opencv / modules / video / src / slidepy.cpp:845:error:( - 215 )(npoints = prevPtsMat.checkVector(2,CV_32F,true))> =函数calcOpticalFlowPyrLK中的= 0

我在这里附上我的代码:

#include <opencv2/opencv.hpp>
#include <opencv2/gpu/gpu.hpp>
#include <chrono>
#include <iostream>
#include <fstream>
static void download(const cv::gpu::GpuMat& d_mat, std::vector<cv::Point2f>& vec)
            {
                vec.resize(d_mat.cols);
                cv::Mat mat(1, d_mat.cols, CV_32FC2, (void*)&vec[0]);
                d_mat.download(mat);
            }

            static void download(const cv::gpu::GpuMat& d_mat, std::vector<uchar>& vec)
            {
                vec.resize(d_mat.cols);
                cv::Mat mat(1, d_mat.cols, CV_8UC1, (void*)&vec[0]);
                d_mat.download(mat);
            }

            int main () {

                //Read the Images prv & cur Left
                cv::Mat mImageLprv = cv::imread("../mImageLprv.pgm", 0);
                cv::Mat mImageLcur = cv::imread("../mImageLcur.pgm", 0);

                //Exit if images are not read
                if (!mImageLprv.data || !mImageLcur.data) exit(1);

                //Convert to GPU format
                cv::gpu::GpuMat d_mImageLprv(mImageLprv);
                cv::gpu::GpuMat d_mImageLcur(mImageLcur);

                    cv::gpu::GpuMat d_prevPts, d2_prevPts;

                    //For comparison test
                    cv::gpu::GoodFeaturesToTrackDetector_GPU detector2(3128,0.01,0);
                    detector2(d_mImageLprv,d2_prevPts);

                //FAST GPU Detector
                    cv::gpu::FAST_GPU detector(25);
                detector(d_mImageLprv, cv::gpu::GpuMat(), d_prevPts);       

                    std::cout<<"Size: "         <<d_prevPts.size()<<" "
                             <<"Type: "         <<d_prevPts.type()<<" "
                             <<"isContinuous: " <<d_prevPts.isContinuous()<<"\n";

                //Call the KLT Tracker
                cv::gpu::PyrLKOpticalFlow d_pyrLK;
                d_pyrLK.winSize.width = 21;
                d_pyrLK.winSize.height = 21;
                d_pyrLK.maxLevel = 3;
                //d_pyrLK.iters = 30;

                cv::gpu::GpuMat d_nextPts;
                cv::gpu::GpuMat d_validIdx;
                cv::gpu::GpuMat d1_prevPts = cv::gpu::createContinuous(d_prevPts.size(),5);
                d_prevPts.copyTo(d1_prevPts);
                    cv::gpu::GpuMat dx_prevPts = d1_prevPts.reshape(2,1);

                    std::cout<<"Size: "         <<dx_prevPts.size()<<" "
                            <<"Type: "         <<dx_prevPts.type()<<" "
                            <<"isContinuous: " <<dx_prevPts.isContinuous()<<"\n";

                    d_pyrLK.sparse(d_mImageLprv, d_mImageLcur, d_prevPts, d_nextPts, d_validIdx);

                std::vector<cv::Point2f> prevPtsPyrLK(dx_prevPts.cols);
                download(dx_prevPts, prevPtsPyrLK);

                std::vector<cv::Point2f> nextPtsPyrLK(d_nextPts.cols);
                download(d_nextPts, nextPtsPyrLK);

                std::vector<uchar> validIdx(d_validIdx.cols);
                download(d_validIdx, validIdx);

                return 0;
            }

0 个答案:

没有答案