Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

OpenCV 3.0 beta 編譯遇到 ORB 抽象型別錯誤的解法

好一陣子沒碰 OpenCV,回頭一看已經 3.0 beta。今天編譯就被這行卡住:

error: cannot declare variable 'orb' to be of abstract type 'cv::ORB'

原因是 ORB 這個類別不能再像以前那樣直接建構了。到原始碼樹裡找到 opencvroot/samples/gpu/performance/tests.cpp,把內容改成:

TEST(ORB)
{
    Mat src = imread(abspath("../data/aloeL.jpg"), IMREAD_GRAYSCALE);
    if (src.empty()) throw runtime_error("can't open ../data/aloeL.jpg");

    //ORB orb(4000);
    Ptr<ORB> orb = ORB::create(4000,1.2f,8,31,0,2,ORB::HARRIS_SCORE,31,20);
    vector<KeyPoint> keypoints;
    Mat descriptors;

    orb->detectAndCompute(src, Mat(), keypoints, descriptors);

    //orb(src, Mat(), keypoints, descriptors);

    CPU_ON;
    //orb(src, Mat(), keypoints, descriptors);
    orb->detectAndCompute(src, Mat(), keypoints, descriptors);
    CPU_OFF;

    cuda::ORB_CUDA d_orb;
    cuda::GpuMat d_src(src);
    cuda::GpuMat d_keypoints;
    cuda::GpuMat d_descriptors;

    d_orb(d_src, cuda::GpuMat(), d_keypoints, d_descriptors);

    CUDA_ON;
    d_orb(d_src, cuda::GpuMat(), d_keypoints, d_descriptors);
    CUDA_OFF;
}

翻 feature2d.hpp 就能看出根因:

class CV_EXPORTS_W ORB : public Feature2D
{
public:
    enum { kBytes = 32, HARRIS_SCORE=0, FAST_SCORE=1 };

    CV_WRAP static Ptr<ORB> create(int nfeatures=500, float scaleFactor=1.2f, int nlevels=8, int edgeThreshold=31,
        int firstLevel=0, int WTA_K=2, int scoreType=ORB::HARRIS_SCORE, int patchSize=31, int fastThreshold=20);

    CV_WRAP virtual void setMaxFeatures(int maxFeatures) = 0;
    CV_WRAP virtual int getMaxFeatures() const = 0;
    ...(其餘 setter/getter 皆為純虛函式)
};

關鍵在於:類別裡沒有任何建構子,只有一個靜態的 create。此外,檢測也不能再用原來那個操作符重載版本,得改呼叫 detectAndCompute。這可以從 ORB_Impl 的宣告看出來:

class ORB_Impl : public ORB
{
public:
    explicit ORB_Impl(int _nfeatures, float _scaleFactor, int _nlevels, int _edgeThreshold,
            int _firstLevel, int _WTA_K, int _scoreType, int _patchSize, int _fastThreshold) :
            nfeatures(_nfeatures), scaleFactor(_scaleFactor), nlevels(_nlevels),
            edgeThreshold(_edgeThreshold), firstLevel(_firstLevel), wta_k(_WTA_K),
            scoreType(_scoreType), patchSize(_patchSize), fastThreshold(_fastThreshold)
    {}

    // 各項 setter / getter 實作略

    // returns the descriptor size in bytes
    int descriptorSize() const;
    // returns the descriptor type
    int descriptorType() const;
    // returns the default norm type
    int defaultNorm() const;

    // Compute the ORB_Impl features and descriptors on an image
    void detectAndCompute( InputArray image, InputArray mask, std::vector<KeyPoint>& keypoints,
                     OutputArray descriptors, bool useProvidedKeypoints=false );

protected:
    int nfeatures;
    double scaleFactor;
    int nlevels;
    int edgeThreshold;
    int firstLevel;
    int wta_k;
    int scoreType;
    int patchSize;
    int fastThreshold;
};

換言之,3.0 之後請一律用 Ptr<ORB> orb = ORB::create(...),再呼叫 orb->detectAndCompute(...)。希望同樣踩到這個坑的人能少花點時間。

另外,若使用最新的 CUDA 6.5,在 CMake GUI 裡務必把 CUDA_ARCH_BIN 設為 2.0 2.1(2.0) 3.0 3.5,不要留 1.1、1.2、1.3 這類舊運算能力,因為 CUDA 6.5 已不再支援那些老顯卡。

Copyright © Linux教程網 All Rights Reserved