【问题标题】:OpenCV Flann - Assertion FailsOpenCV Flann - 断言失败
【发布时间】:2015-04-25 23:20:53
【问题描述】:

我一直在尝试设置一个基本的 3D 点 flann knnsearch,但到目前为止还没有成功地让它工作。我尝试了许多不同的方法,但都返回错误。有一段时间我遇到了cv::flann::Index kdtree(cv::Mat(cvPointCloud).reshape(1), indexParams); 抱怨类型不匹配的问题。但似乎我不再看到那个错误,但是这个错误已经取代了它,我不知道该怎么办。

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;
}

如果有人能指出我正确的方向,我将不胜感激!

【问题讨论】:

    标签: c++ opencv visual-c++ flann opencv3.0


    【解决方案1】:

    根据Opencv documentation,你的变量'dists'需要是一个

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

    不是

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

    在您的代码中声明。

    【讨论】:

    • 可以用 Point3i 代替 Point3f 吗?我所有的数据都是整数,不想在它们之间混合。切换时收到以下错误:OpenCV Error : Unsupported format or combination of formats(type = 4) in cv::flann::buildIndex_, file E : \opencv\source\modules\flann\src\miniflann.cpp, line 315 其中type = 4CV_32S
    • 我认为没有。它只接受 Point3f 的向量作为输入。将您的数据向量转换为 Point3f 的向量,只需 O(n)。
    猜你喜欢
    • 2014-02-10
    • 2012-11-26
    • 2014-11-24
    • 2014-05-28
    • 2020-12-31
    • 2015-09-12
    • 1970-01-01
    • 1970-01-01
    • 2012-06-21
    相关资源
    最近更新 更多