2016-02-13 98 views
0

我使用的后续代码在运行opencv3.1基本SIFT代码:OpenCV的3.1 drawKeypoints抛出错误

#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 

回答

0

我的天堂现在还没有玩过SIFT,但看起来你并没有在本节中实例化img_matches:

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

错误消息提示它不期待空的()图像。 你可以尝试用黑色像素首先初始化图像:

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

通知我使用的第一个图像尺寸/尺寸,假设两个图像具有相同的尺寸。请确保仔细检查您的设置并使用适当的尺寸。 此外,你可以把块在try...catch块看到的细节:

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