2016-02-13 3 views
0

Я использую код последующего запустить базовый код SIFT в opencv3.1: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; 

} 

Но код drawKeypoints и drawMatches выдаст ошибку: гавань

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

I Еще не играл с 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; 
}