2013-04-13 207 views
2

我試圖建立隨OpenCV的示例程序brief_match_test.cpp,但我一直在簡歷收到此錯誤:: findHomography()函數,當我運行程序:OpenCV的findHomography斷言失敗的錯誤

OpenCV Error: Assertion failed (mtype == type0 || (CV_MAT_CN(mtype) == CV_MAT_CN(type0) && ((1 << type0) & fixedDepthMask) != 0)) in create, file /opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_release_tarballs_ports_graphics_opencv/opencv/work/OpenCV-2.4.3/modules/core/src/matrix.cpp, line 1421 
libc++abi.dylib: terminate called throwing an exception 
findHomography ... Abort trap: 6 

我編譯它是這樣的:

g++ `pkg-config --cflags opencv` `pkg-config --libs opencv` brief_match_test.cpp -o brief_match_test 

我已經添加了一些東西的程序,以顯示關鍵點的快速算法發現,但還沒有觸及處理單應的部分。我將我的包括修改示例,以防萬一我沒有螺絲的東西了:

/* 
* matching_test.cpp 
* 
* Created on: Oct 17, 2010 
*  Author: ethan 
*/ 
#include "opencv2/core/core.hpp" 
#include "opencv2/calib3d/calib3d.hpp" 
#include "opencv2/features2d/features2d.hpp" 
#include "opencv2/imgproc/imgproc.hpp" 
#include "opencv2/highgui/highgui.hpp" 
#include <vector> 
#include <iostream> 

using namespace cv; 
using namespace std; 

//Copy (x,y) location of descriptor matches found from KeyPoint data structures into Point2f vectors 
static void matches2points(const vector<DMatch>& matches, const vector<KeyPoint>& kpts_train, 
        const vector<KeyPoint>& kpts_query, vector<Point2f>& pts_train, vector<Point2f>& pts_query) 
{ 
    pts_train.clear(); 
    pts_query.clear(); 
    pts_train.reserve(matches.size()); 
    pts_query.reserve(matches.size()); 
    for (size_t i = 0; i < matches.size(); i++) 
    { 
    const DMatch& match = matches[i]; 
    pts_query.push_back(kpts_query[match.queryIdx].pt); 
    pts_train.push_back(kpts_train[match.trainIdx].pt); 
    } 

} 

static double match(const vector<KeyPoint>& /*kpts_train*/, const vector<KeyPoint>& /*kpts_query*/, DescriptorMatcher& matcher, 
      const Mat& train, const Mat& query, vector<DMatch>& matches) 
{ 

    double t = (double)getTickCount(); 
    matcher.match(query, train, matches); //Using features2d 
    return ((double)getTickCount() - t)/getTickFrequency(); 
} 

static void help() 
{ 
     cout << "This program shows how to use BRIEF descriptor to match points in features2d" << endl << 
       "It takes in two images, finds keypoints and matches them displaying matches and final homography warped results" << endl << 
       "Usage: " << endl << 
        "image1 image2 " << endl << 
       "Example: " << endl << 
        "box.png box_in_scene.png " << endl; 
} 

const char* keys = 
{ 
    "{1| |box.png    |the first image}" 
    "{2| |box_in_scene.png|the second image}" 
}; 

int main(int argc, const char ** argv) 
{ 
    Mat outimg; 
    help(); 
    CommandLineParser parser(argc, argv, keys); 
    string im1_name = parser.get<string>("1"); 
    string im2_name = parser.get<string>("2"); 

    Mat im1 = imread(im1_name, CV_LOAD_IMAGE_GRAYSCALE); 
    Mat im2 = imread(im2_name, CV_LOAD_IMAGE_GRAYSCALE); 

    if (im1.empty() || im2.empty()) 
    { 
     cout << "could not open one of the images..." << endl; 
     cout << "the cmd parameters have next current value: " << endl; 
     parser.printParams(); 
     return 1; 
    } 

    double t = (double)getTickCount(); 

    FastFeatureDetector detector(15); 
    BriefDescriptorExtractor extractor(32); //this is really 32 x 8 matches since they are binary matches packed into bytes 

    vector<KeyPoint> kpts_1, kpts_2; 
    detector.detect(im1, kpts_1); 
    detector.detect(im2, kpts_2); 

    t = ((double)getTickCount() - t)/getTickFrequency(); 

    cout << "found " << kpts_1.size() << " keypoints in " << im1_name << endl << "fount " << kpts_2.size() 
     << " keypoints in " << im2_name << endl << "took " << t << " seconds." << endl; 

    drawKeypoints(im1, kpts_1, outimg, 200); 
    imshow("Keypoints - Image1", outimg); 
    drawKeypoints(im2, kpts_2, outimg, 200); 
    imshow("Keypoints - Image2", outimg); 

    Mat desc_1, desc_2; 

    cout << "computing descriptors..." << endl; 

    t = (double)getTickCount(); 

    extractor.compute(im1, kpts_1, desc_1); 
    extractor.compute(im2, kpts_2, desc_2); 

    t = ((double)getTickCount() - t)/getTickFrequency(); 

    cout << "done computing descriptors... took " << t << " seconds" << endl; 

    //Do matching using features2d 
    cout << "matching with BruteForceMatcher<Hamming>" << endl; 
    BFMatcher matcher_popcount(NORM_HAMMING); 
    vector<DMatch> matches_popcount; 
    double pop_time = match(kpts_1, kpts_2, matcher_popcount, desc_1, desc_2, matches_popcount); 
    cout << "done BruteForceMatcher<Hamming> matching. took " << pop_time << " seconds" << endl; 

    vector<Point2f> mpts_1, mpts_2; 
    cout << "matches2points ... "; 
    matches2points(matches_popcount, kpts_1, kpts_2, mpts_1, mpts_2); //Extract a list of the (x,y) location of the matches 
    cout << "done" << endl; 

    vector<char> outlier_mask; 
    cout << "findHomography ... "; 
    Mat H = findHomography(mpts_2, mpts_1, RANSAC, 1, outlier_mask); 
    cout << "done" << endl; 

    cout << "drawMatches ... "; 
    drawMatches(im2, kpts_2, im1, kpts_1, matches_popcount, outimg, Scalar::all(-1), Scalar::all(-1), outlier_mask); 
    cout << "done" << endl; 
    imshow("matches - popcount - outliers removed", outimg); 

    Mat warped; 
    Mat diff; 
    warpPerspective(im2, warped, H, im1.size()); 
    imshow("warped", warped); 
    absdiff(im1,warped,diff); 
    imshow("diff", diff); 
    waitKey(); 
    return 0; 
} 

回答

1

我不肯定知道,所以我真的回答這個問題,只是因爲沒有其他人至今,它已經10自問你問題以來的幾個小時。

我的第一個想法是,你沒有足夠的點對。單應性需要至少4對,否則無法找到唯一的解決方案。如果匹配數至少爲4,則可能需要確保只調用findHomography。

或者,問題herehere與失敗的斷言(由調用不同於您的函數引起的錯誤)有關的問題大致相同。我猜OpenCV會進行某種形式的動態類型檢查或模板化,以至於在編譯時應該發生的類型不匹配錯誤最終成爲失敗斷言形式的運行時錯誤。 所有這一切說,也許你應該將mpts_1和mpts_2轉換爲cv :: Mat,然後再傳遞給findHomography。

+0

我不認爲這是因爲成對的數量。這個說法不會失敗。我還會嘗試將mpts_1和mpts_2投射到cv :: Mat:'Mat H = findHomography(Mat(mpts_2),Mat(mpts_1),outlier_mask,RANSAC,1);' – JonesV

1

這是內部OpenCV類型的問題。 findHomography()想要矢量< unsigned char>作爲最後一個參數。但drawMatches()需要矢量< char>作爲最後一個。

0

我認爲,在this page很多事情都解釋了有關brief_match_test.cpp和糾正它的方法。

相關問題