c++ 调用opencv或pcl流程

发布于:2025-05-28 ⋅ 阅读:(20) ⋅ 点赞:(0)

以下是一个基于C++和OpenCV调用PCL(Point Cloud Library)的教程,主要介绍如何在Ubuntu环境下配置和使用PCL与OpenCV:

环境准备

  1. 安装依赖库
    在Ubuntu系统中,运行以下命令安装必要的依赖库:

    sudo apt-get install build-essential
    sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
    sudo apt-get install python-dev python-numpy python3-dev python3-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev
    sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
    sudo apt update
    sudo apt install libjasper1 libjasper-dev
    
  2. 安装OpenCV

    • 下载OpenCV和OpenCV_contrib源码:
      wget https://github.com/opencv/opencv/archive/4.5.3.zip -O opencv-4.5.3.zip
      wget https://github.com/opencv/opencv_contrib/archive/4.5.3.zip -O opencv_contrib-4.5.3.zip
      unzip opencv-4.5.3.zip
      unzip opencv_contrib-4.5.3.zip
      mv opencv_contrib-4.5.3 opencv-4.5.3/opencv_contrib
      
    • 编译OpenCV:
      cd opencv-4.5.3
      mkdir build && cd build
      sudo cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local/opencv4 \
        -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules/ -D OPENCV_GENERATE_PKGCONFIG=YES -D OPENCV_ENABLE_NONFREE=True ..
      sudo make -j$(nproc)
      sudo make install
      
  3. 安装PCL

    • 使用系统包管理器安装PCL:
      sudo apt-get install libpcl-dev
      

配置项目

  1. 创建项目目录结构
    创建一个C++项目目录,例如:

    cpp_project
    ├── .vscode
    │   ├── c_cpp_properties.json
    │   ├── launch.json
    │   └── tasks.json
    ├── build
    ├── CMakeLists.txt
    ├── include
    │   ├── func_common.h
    │   ├── func_opencv.h
    │   └── func_pcl.h
    └── src
        ├── func_common.cpp
        ├── func_opencv.cpp
        ├── func_pcl.cpp
        └── main.cpp
    
  2. 配置CMakeLists.txt
    编写CMakeLists.txt文件以配置项目:

    cmake_minimum_required(VERSION 2.8)
    project(MyProject)
    
    # 添加PCL库
    find_package(PCL 1.8 REQUIRED)
    include_directories(${PCL_INCLUDE_DIRS})
    link_directories(${PCL_LIBRARY_DIRS})
    add_definitions(${PCL_DEFINITIONS})
    
    # 添加OpenCV库
    set(OpenCV_DIR "/usr/local/opencv4")
    find_package(OpenCV REQUIRED)
    include_directories(${OpenCV_INCLUDE_DIRS})
    link_directories(${OpenCV_LIB_DIR})
    
    # 添加源文件
    aux_source_directory(src DIR_SRCS)
    add_executable(MyProject ${DIR_SRCS})
    
    # 链接库
    target_link_libraries(MyProject ${PCL_LIBRARIES} ${OpenCV_LIBS})
    
  3. 编写代码
    main.cpp中编写代码,结合OpenCV和PCL的功能。例如:

    #include <opencv2/opencv.hpp>
    #include <pcl/point_cloud.h>
    #include <pcl/point_types.h>
    #include <pcl/io/pcd_io.h>
    
    int main() {
        // 使用OpenCV读取图像
        cv::Mat image = cv::imread("test.jpg");
        cv::imshow("Image", image);
        cv::waitKey(0);
    
        // 创建PCL点云
        pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
        cloud->width = 5;
        cloud->height = 1;
        cloud->is_dense = false;
        cloud->points.resize(cloud->width * cloud->height);
    
        for (size_t i = 0; i < cloud->points.size(); ++i) {
            cloud->points[i].x = 1024.0f * rand() / (RAND_MAX + 1.0f);
            cloud->points[i].y = 1024.0f * rand() / (RAND_MAX + 1.0f);
            cloud->points[i].z = 1024.0f * rand() / (RAND_MAX + 1.0f);
        }
    
        // 保存点云
        pcl::io::savePCDFileASCII("test.pcd", *cloud);
        std::cout << "Saved point cloud to test.pcd" << std::endl;
    
        return 0;
    }
    
  4. 编译和运行
    在项目根目录下运行以下命令:

    cd build
    cmake ..
    make
    ./MyProject
    

注意事项

  • 确保OpenCV和PCL的路径正确配置在CMakeLists.txt中。
  • 如果使用VSCode开发,需要在.vscode/c_cpp_properties.json中添加OpenCV和PCL的头文件路径。

通过以上步骤,你可以在C++中结合使用OpenCV和PCL,实现图像处理和点云处理的功能。


网站公告

今日签到

点亮在社区的每一天
去签到