#include #include #include #include <pcl/io/ply_io.h> #include <pcl/point_types.h>

using namespace std;

// 定义点云数据类型 typedef pcl::PointXYZ PointT; typedef pcl::PointCloud PointCloud;

// Hough变换检测直线 void hough_transform(PointCloud::Ptr cloud, float threshold, float resolution) { // 设置Hough空间的分辨率 float rho_resolution = resolution; float theta_resolution = M_PI / 180.0 * resolution;

// 设置Hough空间的大小
int rho_size = ceil(sqrt(pow(cloud->width, 2) + pow(cloud->height, 2)) / rho_resolution);
int theta_size = ceil(M_PI / theta_resolution);

// 初始化Hough空间
int *hough_space = new int[rho_size * theta_size];
memset(hough_space, 0, sizeof(int) * rho_size * theta_size);

// 遍历所有点,统计Hough空间中的投票
for (int i = 0; i < cloud->points.size(); i++) {
    PointT point = cloud->points[i];
    for (float theta = 0; theta < M_PI; theta += theta_resolution) {
        float rho = point.x * cos(theta) + point.y * sin(theta);
        int rho_index = round(rho / rho_resolution) + rho_size / 2;
        int theta_index = round(theta / theta_resolution);
        if (rho_index >= 0 && rho_index < rho_size && theta_index >= 0 && theta_index < theta_size) {
            hough_space[rho_index * theta_size + theta_index]++;
        }
    }
}

// 在Hough空间中寻找直线
vector<pair<int, int>> line_candidates;
for (int rho_index = 0; rho_index < rho_size; rho_index++) {
    for (int theta_index = 0; theta_index < theta_size; theta_index++) {
        int votes = hough_space[rho_index * theta_size + theta_index];
        if (votes >= threshold) {
            line_candidates.push_back(make_pair(rho_index, theta_index));
        }
    }
}

// 输出检测到的直线
cout << "Detected " << line_candidates.size() << " lines:" << endl;
for (int i = 0; i < line_candidates.size(); i++) {
    int rho_index = line_candidates[i].first;
    int theta_index = line_candidates[i].second;
    float rho = (rho_index - rho_size / 2) * rho_resolution;
    float theta = theta_index * theta_resolution;
    cout << "Line " << i << ": rho=" << rho << ", theta=" << theta << endl;
}

// 释放Hough空间的内存
delete[] hough_space;

}

int main(int argc, char **argv) { // 读取PLY点云数据 PointCloud::Ptr cloud(new PointCloud); pcl::io::loadPLYFile("cloud.ply", *cloud);

// 调用Hough变换检测直线
float threshold = 100;
float resolution = 1;
hough_transform(cloud, threshold, resolution);

return 0;

}

C++ Hough 变换检测 PLY 点云数据中的直线

原文地址: https://www.cveoy.top/t/topic/opyI 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录