2015-02-24 141 views
1

我一直在嘗試設置一個基本的3D點flann knnsearch,但到目前爲止還沒有能夠成功地將其工作。我已經嘗試了許多不同的事情,所有這些事情都會返回錯誤。有一段時間我有問題與cv::flann::Index kdtree(cv::Mat(cvPointCloud).reshape(1), indexParams);抱怨類型不匹配。但似乎我不再看到那個錯誤,但是這個錯誤已經取代了它,我不知道該怎麼做。OpenCV Flann - 斷言失敗

OpenCV Error: Assertion failed (mtype == type0 || (CV_MAT_CN(mtype) == CV_MAT_CN (type0) && ((1 << type0) & fixedDepthMask) != 0)) in cv::_OutputArray::create, f ile E:\opencv\source\modules\core\src\matrix.cpp, line 2280

我的代碼:

#include <opencv2/core/core.hpp> 
#include <opencv2/flann/flann.hpp> 
#include <iostream> 
#include <stdio.h> 
#include <random> 


#define POINTS_IN_CLOUD 15 
#define NUM_NEAREST_NEIGHBORS 2 
#define NUM_SEARCHES 64 



int main() { 
    std::vector<cv::Point3f> cvPointCloud; 

    // Build the cvPointCloud 
    for (int i = 0; i < POINTS_IN_CLOUD; i++) { 
     int x = rand() % 100; 
     int y = rand() % 100; 
     int z = rand() % 100; 
     cvPointCloud.push_back(cv::Point3f(x, y, z)); 
    } 

    // Loop through and print out the points in the cloud 
    for (std::vector<cv::Point3f>::iterator it = cvPointCloud.begin(); it != cvPointCloud.end(); ++it) { 
     std::cout << *it << "\t"; 
    } 




    // KdTree with 4 random trees 
    cv::flann::KDTreeIndexParams indexParams(4); 
    cv::flann::Index kdtree(cv::Mat(cvPointCloud).reshape(1), indexParams); 


    // Point to find nearest neighbors 
    cv::Point3f pt = cv::Point3f(5, 5, 5); 

    // Generate the search query 
    std::vector<float> query; 
    query.push_back(pt.x); 
    query.push_back(pt.y); 
    query.push_back(pt.z); 


    std::vector<int> indices(NUM_NEAREST_NEIGHBORS); 
    std::vector<int> dists(NUM_NEAREST_NEIGHBORS); 
    kdtree.knnSearch(query, indices, dists, NUM_NEAREST_NEIGHBORS, cv::flann::SearchParams(NUM_SEARCHES)); 

    std::cout << indices[0]; 
    std::cout << dists[0]; 

    return 0; 
} 

如果有人可以點我到正確的方向,我將不勝感激!

回答

2

Opencv documentation,你的變量 'dists中' 需要一個

std::vector<float> dists(NUM_NEAREST_NEIGHBORS); 

不是

std::vector<int> dists(NUM_NEAREST_NEIGHBORS); 

在你的代碼中聲明。

+0

是否可以使用Point3i而不是Point3f?我所有的數據都是int,不想在它們之間混合。切換時收到以下錯誤:cv :: flann :: buildIndex_,文件E:\ opencv \ source \ modules \ flann \ src \ miniflann.cpp中的OpenCV錯誤:不支持的格式或格式組合(類型= 4)其中'type = 4'的行315'是'CV_32S' – Steven10172 2015-02-25 04:58:56

+0

我認爲不行。它只接受Point3f的矢量作爲輸入。將您的數據向量轉換爲Point3f的向量,它只需要花費O(n)。 – 2015-02-25 06:26:01