代码中的 OpenCV 错误

OpenCV error in codes

我正在做一个关于图像匹配的 opencv 项目。 行

    std::vector<cv::Keypoint> keypoints1;
    std::vector<cv::Keypoint> keypoints2;

出现错误:命名空间 "cv" 没有成员 "Keypoint" 我该如何解决?

代码中还有一个错误

    //Define feature detector
    cv::FastFeatureDetector fastDet(80);
    //Keypoint detection
    fastDet.detect(image1, keypoints1);
    fastDet.detect(image2, keypoints2);

错误说: 不允许抽象 class 类型 "cv::FastFeatureDetector" 的对象:

函数 :cv::FastFeatureDetector::setThreshold" 是一个纯虚函数

函数 :cv::FastFeatureDetector::getThreshold" 是一个纯虚函数

函数 :cv::FastFeatureDetector::setNonmaxSuppression" 是一个纯虚函数

函数 :cv::FastFeatureDetector::getNonmaxSuppression" 是一个纯虚函数

function :cv::FastFeatureDetector::setType" 是一个纯虚函数

function :cv::FastFeatureDetector::getType" 是一个纯虚函数

有人可以帮忙吗?

完整代码如下:

    #include "opencv2/highgui/highgui.hpp"
    #include "opencv2/imgproc/imgproc.hpp"
    #include "opencv2\features2d\features2d.hpp"
    #include"opencv2\core.hpp"

    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <vector>

    using namespace cv;
    using namespace std;

    void main(int argc, const char** argv)
    {
        Mat image1 = imread("image1.jpg", CV_LOAD_IMAGE_UNCHANGED);
        Mat image2 = imread("image2.jpg", CV_LOAD_IMAGE_UNCHANGED);

        //Define keypoints vector
        std::vector<cv::Keypoint> keypoints1;
        std::vector<cv::Keypoint> keypoints2;

        //Define feature detector
        cv::FastFeatureDetector fastDet(80);
        //Keypoint detection
        fastDet.detect(image1, keypoints1);
        fastDet.detect(image2, keypoints2);

        //Define a square neighbourhood
        const int nsize(11);                            //size of the neighbourhood
        cv::Rect neighbourhood(0, 0, nsize, nsize);     //11x11
        cv::Mat patch1;
        cv::Mat patch2;

        //For all points in first image
        //find the best match in second image
        cv::Mat result;
        std::vector<cv::DMatch> matches;

        //for all keypoints in image 1
        for (int i = 0; i < keypoints1.size(); i++)
        {
            //define image patch
            neighbourhood.x = keypoints1[i].pt.x - nsize / 2;
            neighbourhood.y = keypoints1[i].pt.y - nsize / 2;

            //if neighbourhood of points outside image,
            //then continue with next point
            if (neighbourhood.x < 0 || neighbourhood.y < 0 || neighbourhood.x + nsize >= image1.cols || neighbourhood.y + nsize >= image1.rows)
                continue;

            //patch in image 1
            patch1 = image1(neighbourhood);

            //reset best correlation value;
            cv::DMatch bestMatch;

            //for all keypoints in image 2
            for (int j = 0; j < keypoints2.size(); j++)
            {
                //define image patch
                neighbourhood.x = keypoints2[j].pt.x - nsize / 2;
                neighbourhood.y = keypoints2[j].pt.y - nsize / 2;


                //if neighbourhood of points outside image,
                //then continue with next point
                if (neighbourhood.x < 0 || neighbourhood.y < 0 || neighbourhood.x + nsize >= image2.cols || neighbourhood.y + nsize >= image2.rows)
                    continue;

                //patch in image 2
                patch2 = image2(neighbourhood);

                //match the 2 patches
                cv::matchTemplate(patch1, patch2, result, CV_TM_SQDIFF_NORMED);

                //check if it is best match
                if (result.at<float>(0, 0) < bestMatch.distance)
                {
                    bestMatch.distance = result.at<float>(0, 0);
                    bestMatch.queryIdx = i;
                    bestMatch.trainIdx = j;
                }
            }
            //add the best match
            matches.push_back(bestMatch);
        }
        //extract the 25 best matches
        std::nth_element(matches.begin(), matches.begin() + 25, matches.end());
        matches.erase(matches.begin() + 25, matches.end());

        //Draw matching results
        cv::Mat matchImage;
        cv::DrawMatchesFlags();
    }
  • 您的代码中有一些错误。

    1. 替换下面的行

      std::vector<cv::Keypoint> keypoints1;
      std::vector<cv::Keypoint> keypoints2;
      

      有了这个

      std::vector<cv::KeyPoint> keypoints1;
      std::vector<cv::KeyPoint> keypoints2;
      
    2. 对于 cv::FastFeatureDetector fastDet(80);,您可能需要包含库 opencv_features2d

  • 此更改后您的代码将 运行 成功。