简体   繁体   English

OpenCV Flann-断言失败

[英]OpenCV Flann - Assertion Fails

I've been trying to setup a basic 3D point flann knnsearch, but so far haven't been able to successfully get it to work. 我一直在尝试设置基本的3D点flann knnsearch,但是到目前为止,还没有成功地使它起作用。 I've tried many different things of which all come back with errors. 我已经尝试了许多不同的方法,但所有方法都会出错。 For a while I had issues with cv::flann::Index kdtree(cv::Mat(cvPointCloud).reshape(1), indexParams); 有一段时间我遇到了cv::flann::Index kdtree(cv::Mat(cvPointCloud).reshape(1), indexParams); complaining about the type not matching. 抱怨类型不匹配。 But it seems that I no longer see that error, but this one has replaced it and I have no idea what to do. 但是似乎我不再看到该错误,但是此错误已替代它,我不知道该怎么办。

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

My Code: 我的代码:

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

If someone could point me into the right direction I would greatly appreciate it! 如果有人能指出我正确的方向,我将不胜感激!

According to Opencv documentation , your variable 'dists' needs to be a 根据Opencv文档 ,您的变量'dists'必须为

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

not a 不是

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

as declared in your code. 如您的代码中所声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM