【PCL】二维圆拟合

1 CMake

cmake_minimum_required(VERSION 3.20 FATAL_ERROR)
project(RansacModel)
find_package(PCL 1.11 REQUIRED)

include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})

add_executable(FitCirle2D FitCirlce2D.cpp)
target_link_libraries(FitCirle2D ${PCL_LIBRARIES})

2 CPluePlus

#include <iostream>
#include <thread>

#include <pcl/filters/extract_indices.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/sample_consensus/ransac.h>
#include <pcl/sample_consensus/sac_model_circle.h>

int
main(int argc, char** argv)
{
    float pointsX[] = {618.389f, 647.896f, 658.925f, 647.519f, 617.921f, 580.924f, 550.169f, 537.156f, 547.043f, 576.112f};
    float pointsY[] = {464.645f, 487.112f, 522.397f, 557.498f, 579.570f, 580.432f, 559.622f, 524.686f, 488.641f, 465.161f};

    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_c(new pcl::PointCloud<pcl::PointXYZ>);
    for(int i = 0; i < 10; i++)
    {
        cloud_c->push_back(pcl::PointXYZ(pointsX[i], pointsY[i], 0.0f));
    }
    
    pcl::SampleConsensusModelCircle2D <pcl::PointXYZ>::Ptr model_c(new pcl::SampleConsensusModelCircle2D<pcl::PointXYZ>(cloud_c));
    pcl::RandomSampleConsensus<pcl::PointXYZ> ransac(model_c);
    ransac.setDistanceThreshold(.01);
    ransac.computeModel();
    std::vector<int> inliers;
    ransac.getInliers(inliers);
    Eigen::VectorXf model_coefficients;
    ransac.getModelCoefficients(model_coefficients);

    std::cout << model_coefficients << std::endl;

    return 0;
}

3. 结果

598.014
522.001
60.9168

上一篇:DHCP动态主机配置协议


下一篇:pclpy 点云可视化汇总