opencv kdtree & pcl kdtree 效率对比

由于项目中以一个环节需要使用kdtree ,对性能要求比较严苛,所以看看那个kdtree效率高一些。对比了opencv和pcl。

复制代码
#include <array>
#include <deque>
#include <fstream>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/flann.hpp>

#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/kdtree/kdtree_flann.h>

#include <iostream>
#include <iostream>
#include <cstdlib>  // 用于 rand()
#include <ctime>    // 用于 time()

int main() {
    // 初始化随机种子
    std::srand(std::time(0));

    // 生成 5000 个随机的二维点
    int numPoints = 5000;
    std::vector<cv::Point2f> points;
    pcl::PointCloud<pcl::PointXY>::Ptr cloud(new pcl::PointCloud<pcl::PointXY>);

    // 随机生成点,范围为 (-100, 100)
    for (int i = 0; i < numPoints; ++i) {
        float x = static_cast<float>(rand() % 200 - 100);  // x 坐标在 [-100, 100] 范围内
        float y = static_cast<float>(rand() % 200 - 100);  // y 坐标在 [-100, 100] 范围内
        points.push_back(cv::Point2f(x, y));
        cloud->points.push_back(pcl::PointXY(x, y));
    }

    // 2. 将点转换为 cv::Mat 格式
    cv::Mat pointMat(points.size(), 2, CV_32F);  // 每个点是 2D(x, y)
    for (size_t i = 0; i < points.size(); i++) {
        pointMat.at<float>(i, 0) = points[i].x; 
        pointMat.at<float>(i, 1) = points[i].y;
    }

    // 3. 创建 KD-Tree 索引(使用 FLANN)
    cv::flann::Index kdtree(pointMat, cv::flann::KDTreeIndexParams(4)); // 4 表示树的分支数


    // 获取程序开始时的时钟时间
    std::clock_t start = std::clock();
    for(int i = 0 ;i < 200; ++i){
        std::vector<int> indices(1);     // 最近邻的索引
        std::vector<float> dists(1);     // 最近邻的距离
        float x = static_cast<float>(rand() % 200 - 100);  // x 坐标在 [-100, 100] 范围内
        float y = static_cast<float>(rand() % 200 - 100);  // y 坐标在 [-100, 100] 范围内
        cv::Mat query = (cv::Mat_<float>(1, 2) << x, y);
        kdtree.knnSearch(query, indices, dists, 1, cv::flann::SearchParams(32));
        // std::cout << "Query point: " << query << std::endl;
        // std::cout << "Closest point: " << pointMat.row(indices[0]) << std::endl;
        // std::cout << "Distance: " << dists[0] << std::endl;
        // std::cout << "----------------"  << std::endl;
    }
    std::clock_t end = std::clock();
    // 计算并输出经过的时间,单位是秒
    double duration = double(end - start) / CLOCKS_PER_SEC;
    std::cout << "opencv kdtree 搜索时间: " << duration << " 秒" << std::endl;

    /
    pcl::KdTreeFLANN<pcl::PointXY> pcl_kdtree;
    pcl_kdtree.setInputCloud(cloud);
    start = std::clock();
    for(int i = 0 ;i < 200; ++i){
        std::vector<int> indices(1);     // 最近邻的索引
        std::vector<float> dists(1);     // 最近邻的距离
        float x = static_cast<float>(rand() % 200 - 100);  // x 坐标在 [-100, 100] 范围内
        float y = static_cast<float>(rand() % 200 - 100);  // y 坐标在 [-100, 100] 范围内
        pcl::PointXY searchPoint;
        searchPoint.x = x;
        searchPoint.y = y;
        pcl_kdtree.nearestKSearch(searchPoint,1, indices, dists);
        // std::cout << "Query point: " << query << std::endl;
        // std::cout << "Closest point: " << pointMat.row(indices[0]) << std::endl;
        // std::cout << "Distance: " << dists[0] << std::endl;
        // std::cout << "----------------"  << std::endl;
    }
    end = std::clock();
    // 计算并输出经过的时间,单位是秒
    duration = double(end - start) / CLOCKS_PER_SEC;
    std::cout << "pcl kdtree 搜索时间: " << duration << " 秒" << std::endl;




    return 0;
}

CMakeLists.txt

复制代码
# 设置 CMake 最低版本要求
cmake_minimum_required(VERSION 3.10)

# 设置项目名称
project(test_kdtree)

# 查找 OpenCV 包
find_package(OpenCV REQUIRED)
find_package(PCL REQUIRED)

# 显示 OpenCV 的版本和路径
message(STATUS "OpenCV version: ${OpenCV_VERSION}")
message(STATUS "OpenCV_INCLUDE_DIRS: ${OpenCV_INCLUDE_DIRS}")
message(STATUS "OpenCV_LIBS: ${OpenCV_LIBS}")

# 设置源文件
set(SOURCE_FILES test_kdtree.cpp)

# 添加可执行文件
add_executable(${PROJECT_NAME} ${SOURCE_FILES})

# 链接 OpenCV 库到目标程序
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS}  ${PCL_LIBRARIES})

# 包含 OpenCV 的头文件路径
target_include_directories(${PROJECT_NAME} PRIVATE ${OpenCV_INCLUDE_DIRS})

结果:

相关推荐
endcy201613 小时前
基于Spring AI的RAG和智能体应用实践
人工智能·ai·系统架构
Blossom.11813 小时前
移动端部署噩梦终结者:动态稀疏视觉Transformer的量化实战
java·人工智能·python·深度学习·算法·机器学习·transformer
FPGA小迷弟13 小时前
ChatGPT回答用AI怎么怎么赚钱
大数据·人工智能
轻微的风格艾丝凡13 小时前
卷积的直观理解
人工智能·深度学习·神经网络·算法·计算机视觉·matlab·cnn
月下倩影时14 小时前
视觉进阶篇——机器学习训练过程(手写数字识别,量大管饱需要耐心)
人工智能·学习·机器学习
PixelMind14 小时前
【超分辨率专题】HYPIR:扩散模型先验与 GAN 对抗训练相结合的新型图像复原框架
人工智能·生成对抗网络·扩散模型·图像复原
说私域14 小时前
从裂变能力竞争到技术水平竞争:开源AI智能名片链动2+1模式S2B2C商城小程序对微商企业竞争格局的重塑
人工智能·小程序·开源
xybDIY14 小时前
基于 Tuya.AI 开源的大模型构建智能聊天机器人
人工智能·机器人·开源
这张生成的图像能检测吗14 小时前
(论文速读)基于DCP-MobileViT网络的焊接缺陷识别
图像处理·深度学习·计算机视觉·可视化·缺陷识别·焊缝缺陷
智慧地球(AI·Earth)16 小时前
GPT-5.1发布!你的AI更暖更智能!
人工智能·gpt·神经网络·aigc·agi