首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何用ORB对齐OpenCV中的2个图像?(编译失败)

如何用ORB对齐OpenCV中的2个图像?(编译失败)
EN

Stack Overflow用户
提问于 2018-10-10 00:37:14
回答 1查看 603关注 0票数 -2

首先,这是我第一次使用C++。我之所以使用C++,是因为OpenCV在C语言中非常有限。

OpenCV文档并不是我见过的最明确的文档。

我已经尝试重写这个ORB example,以便在我的项目中使用。

代码是:

img_orb.cpp

代码语言:javascript
复制
/******************************************************************************
 ******* headers **************************************************************
 ******************************************************************************/
/* Standard (C++) ------------------------------------------------------------*/
        /* std::vector */
    #include <vector>

/* Packages ------------------------------------------------------------------*/
        /* opencv */
    #include <opencv2/opencv.hpp>
    #include <opencv2/features2d/features2d.hpp>

/* Project -------------------------------------------------------------------*/
    #include "img_orb.hpp"


/******************************************************************************
 ******* macros ***************************************************************
 ******************************************************************************/
    # define    MAX_FEATURES    (500)
    # define    GOOD_MATCH_P    (0.15)


/******************************************************************************
 ******* main *****************************************************************
 ******************************************************************************/
void    img_orb_align   (struct _IplImage  *pattern,
                struct _IplImage  *imgptr)
{
    /* Transform (struct _IplImage *) to (class cv::Mat) */
    /* Make a copy so that they aren't modified */
    class cv::Mat   img_0;
    class cv::Mat   img_1;
    img_0   = cv::cvarrToMat(pattern, true);
    img_1   = cv::cvarrToMat(imgptr, true);

    /* Variables to store keypoints & descriptors */
    std::vector <class cv::KeyPoint>    keypoints_0;
    std::vector <class cv::KeyPoint>    keypoints_1;
    class cv::Mat               descriptors_0;
    class cv::Mat               descriptors_1;

    /* Detect ORB features & compute descriptors */
    class cv::Ptr <class cv::Feature2D> orb;
    orb = cv::ORB::create(MAX_FEATURES);
    orb->detectAndCompute(img_0, cv::Mat(), keypoints_0, descriptors_0);
    orb->detectAndCompute(img_1, cv::Mat(), keypoints_1, descriptors_1);

    /* Match structures */
    std::vector <struct cv::DMatch>     matches;
    cv::Ptr <class cv::DescriptorMatcher>   matcher;
    matcher = cv::DescriptorMatcher::create("BruteForce-Hamming");
    matcher->match(descriptors_1, descriptors_0, matches, cv::Mat());

    /* Sort matches by score */
    std::sort(matches.begin(), matches.end());

    /* Remove not so good matches */
    int good_matches;
    good_matches    = GOOD_MATCH_P * matches.size();
    matches.erase(matches.begin() + good_matches, matches.end());

    /* Draw top matches */
    class cv::Mat   img_matches;
    cv::drawMatches(img_1, keypoints_1, img_0, keypoints_0, matches,
                                img_matches);
    cv::imwrite("matches.jpg", img_matches);

    /* Extract location of good matches */
    std::vector <class cv::Point_ <float>>  points_0;
    std::vector <class cv::Point_ <float>>  points_1;
    int i;  
    for (i = 0; i < matches.size(); i++) {
        points_1.push_back(keypoints_1[matches[i].queryIdx].pt);
        points_0.push_back(keypoints_0[matches[i].trainIdx].pt);
    }

    /* Find homography */
    class cv::Mat   img_hg;
    img_hg  = cv::findHomography(points_1, points_0, CV_RANSAC);

    /* Use homography to warp image */
    class cv::Mat   img_align;
    cv::warpPerspective(img_1, img_align, img_hg, img_0.size());

    /* Write img_align into imgptr */
    *imgptr = img_align;
}


/******************************************************************************
 ******* end of file **********************************************************
 ******************************************************************************/

注意:当我编写C语言时,我总是尝试遵循Linux内核的编码风格;这就是为什么我避免使用typedefnamespace,而是在编写之前编写class之类的东西;我喜欢让所有的东西都非常明确。

第一件事是我必须删除#include "opencv2/xfeatures2d.hpp",因为这个头文件在我的系统中不存在(Debian Stretch)。但是,我读到了头文件here,但它似乎并没有包含我问题的解决方案。

当我编译该文件时,我得到以下错误:

代码语言:javascript
复制
/.../img_orb.cpp: In function ‘void img_orb_align(_IplImage*, _IplImage*)’:
/.../img_orb.cpp:78:36: error: no matching function for call to ‘cv::ORB::create(int)’
  orb = cv::ORB::create(MAX_FEATURES);
                                    ^
In file included from /usr/include/opencv2/opencv.hpp:53:0,
                 from /.../img_orb.cpp:19:
/usr/include/opencv2/features2d/features2d.hpp:269:35: note: candidate: static cv::Ptr<cv::Feature2D> cv::Feature2D::create(const string&)
     CV_WRAP static Ptr<Feature2D> create( const string& name );
                                   ^~~~~~
/usr/include/opencv2/features2d/features2d.hpp:269:35: note:   no known conversion for argument 1 from ‘int’ to ‘const string& {aka const std::__cxx11::basic_string<char>&}’
/.../img_orb.cpp:79:7: error: ‘class cv::Feature2D’ has no member named ‘detectAndCompute’; did you mean ‘detectImpl’?
  orb->detectAndCompute(img_0, cv::Mat(), keypoints_0, descriptors_0);
       ^~~~~~~~~~~~~~~~
/.../img_orb.cpp:80:7: error: ‘class cv::Feature2D’ has no member named ‘detectAndCompute’; did you mean ‘detectImpl’?
  orb->detectAndCompute(img_1, cv::Mat(), keypoints_1, descriptors_1);
       ^~~~~~~~~~~~~~~~
Makefile:101: recipe for target 'img_orb.s' failed

我举的这个例子是不是很糟糕?它失败是因为我缺少一个头吗?它失败是因为我不理解一些C++的东西吗?还有别的吗?

编辑:

像这样安装OpenCV:apt-get install libopencv-dev

debian stretch中的版本是:2.4.9.1+dfsg1-2

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-19 03:19:34

更改代码的以下部分:

代码语言:javascript
复制
void    img_orb_align   (struct _IplImage  *pattern,
                struct _IplImage  **imgptr2)

..。

*imgptr2替换imgptr

..。

代码语言:javascript
复制
    /* Detect ORB features & compute descriptors */
    /* This works in openCV 2.4, but does not work in openCV 3.x */
    class cv::ORB   orb;
    orb(img_0, cv::Mat(), keypoints_0, descriptors_0);
    orb(img_1, cv::Mat(), keypoints_1, descriptors_1);
#if 0
    /* This did not work in openCV 2.4;  It does work in openCV 3.x */
    class cv::Ptr <class cv::Feature2D> orb;
    orb = cv::ORB::create(MAX_FEATURES);
    orb->detectAndCompute(img_0, cv::Mat(), keypoints_0, descriptors_0);
    orb->detectAndCompute(img_1, cv::Mat(), keypoints_1, descriptors_1);
#endif

..。

代码语言:javascript
复制
    /* Write img_align into imgptr (need a tmp img;  don't know why) */
    struct _IplImage    imgtmp;
    int         cols;
    int         rows;
    int         depth;
    int         chan;
    cols        = img_align.cols;
    rows        = img_align.rows;
    depth       = (*imgptr2)->depth;
    chan        = (*imgptr2)->nChannels;
    cvReleaseImage(imgptr2);
    *imgptr2    = cvCreateImage(cvSize(cols, rows), depth, chan);
    imgtmp      = img_align;
    cvCopy(&imgtmp, *imgptr2);
    img_align.release();
#if 0
    /* This did not work */
    *imgptr = img_align;
#endif
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52725621

复制
相关文章

相似问题

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