代码中的OpenCV错误

时间:2016-01-21 08:58:11

标签: c++ opencv namespaces computer-vision matching

我正在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);

错误说明: 不允许使用抽象类类型“cv :: FastFeatureDetector”的对象:

function:cv :: FastFeatureDetector :: setThreshold“是纯虚函数

function:cv :: FastFeatureDetector :: getThreshold“是纯虚函数

function:cv :: FastFeatureDetector :: setNonmaxSuppression“是纯虚函数

function: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 个答案:

答案 0 :(得分:0)

  • 您的代码中存在一些错误。

    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

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