opencv 3.1 drawKeypoints 抛出错误

opencv 3.1 drawKeypoints throws errors

我使用以下代码来 运行 opencv3.1 中的基本 SIFT 代码:

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/xfeatures2d/nonfree.hpp>
#include <opencv2/xfeatures2d.hpp>

#include <vector>

using namespace std;
using namespace cv;

int main(int argc, char *argv[])
{        
    //cv::initModule_nonfree();
    //initModule_features2d();
    Mat img_1 = imread("11.bmp", 1);
    Mat img_2 = imread("22.bmp", 1);

    cv::Ptr<Feature2D> f2d = xfeatures2d::SIFT::create();

    //-- Step 1: Detect the keypoints:
    std::vector<KeyPoint> keypoints_1, keypoints_2;    
    f2d->detect( img_1, keypoints_1 );
    f2d->detect( img_2, keypoints_2 );

    //-- Step 2: Calculate descriptors (feature vectors)    
    Mat descriptors_1, descriptors_2;    
    f2d->compute( img_1, keypoints_1, descriptors_1 );
    f2d->compute( img_2, keypoints_2, descriptors_2 );

    Mat out0;
    drawKeypoints(img_1, keypoints_1, out0);
    imshow("KeyPoint0.jpg", out0);
    imwrite("KeyPoint0", out0);

    //-- Step 3: Matching descriptor vectors using BFMatcher :
    BFMatcher matcher;
    std::vector< DMatch > matches;
    matcher.match( descriptors_1, descriptors_2, matches );

    /*
    Mat img_matches;
    drawMatches(img_1,keypoints_1,img_2,keypoints_2,matches,img_matches);
    imshow("matches",img_matches);
    imwrite("matches.jpg",img_matches);
    */
    char c = ' ';
    while ((c = waitKey(0)) != 'q');  // Keep window there until user presses 'q' to quit.

    return 0;

}

但是代码drawKeypointsdrawMatches会抛出错误:

OpenCV Error: Assertion failed (!outImage.empty()) in drawKeypoints, file /Users/vinllen/opt/opencv-3.1.0/modules/features2d/src/draw.cpp, line 113
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /Users/vinllen/opt/opencv-3.1.0/modules/features2d/src/draw.cpp:113: error: (-215) !outImage.empty() in function drawKeypoints

Abort trap: 6

我还没有玩过 SIFT,但您似乎没有在本节中实例化 img_matches:

Mat img_matches;
drawMatches(img_1,keypoints_1,img_2,keypoints_2,matches,img_matches); 

错误消息提示它不需要 empty() 图像。 您可以先尝试用黑色像素初始化图像:

img_matches = Mat::zeros( img_1.size(), CV_8UC3 );

注意我使用的是第一张图片 dimensions/size,假设两张图片的大小相同。请务必仔细检查您的设置并使用合适的尺寸。 此外,您可以将块放在 try...catch 块中以查看详细信息:

try { 
//your drawKeyPoints code here
}catch (cv::Exception &e) {
cerr << e.msg << endl; 
}