OpenCV CUDA模块霍夫变换------在 GPU 上执行概率霍夫变换检测图像中的线段端点类cv::cuda::HoughSegmentDetector

发布于:2025-06-04 ⋅ 阅读:(25) ⋅ 点赞:(0)
  • 操作系统:ubuntu22.04
  • OpenCV版本:OpenCV4.9
  • IDE:Visual Studio Code
  • 编程语言:C++11

算法描述

cv::cuda::HoughSegmentDetector 是 OpenCV 的 CUDA 模块中一个非常重要的类,它用于在 GPU 上执行 概率霍夫变换(Probabilistic Hough Transform),能够检测图像中的线段端点(即直线段),而不是标准霍夫变换中表示为 (rho, theta) 的无限长直线。

类定义

class cv::cuda::HoughSegmentDetector : public cv::cuda::Algorithm

继承自 cv::cuda::Algorithm,提供了 GPU 加速的线段检测功能。

主要功能

  • 在 GPU 上进行 概率霍夫变换
  • 输出为一系列 线段端点,格式为 Vec4i(x1, y1, x2, y2)
  • 支持边缘图作为输入(通常是 Canny 边缘检测后的图像)

构造函数 & 创建方式

你可以通过以下方式创建该类的对象:

cv::Ptr<cv::cuda::HoughSegmentDetector> hough = 
    cv::cuda::createHoughSegmentDetector(rho, theta, threshold, minLineLength, maxLineGap);

参数说明:

参数名 类型 含义
rho float 距离分辨率(像素)
theta float 角度分辨率(弧度)
threshold int 投票阈值,只有投票数大于等于此值的线段才被保留
minLineLength int 线段最小长度,小于该值的线段将被忽略
maxLineGap int 同一线段上点之间的最大间隙

方法列表(常用方法)

方法名 功能
detect(InputArray src, OutputArray lines, Stream& stream = Stream::Null()) 执行霍夫变换检测线段
setRho(float rho) / getRho() 设置/获取距离分辨率
setTheta(float theta) / getTheta() 设置/获取角度分辨率
setThreshold(int threshold) / getThreshold() 设置/获取投票阈值
setMinLineLength(int minLineLength) / getMinLineLength() 设置/获取线段最小长度
setMaxLineGap(int maxLineGap) / getMaxLineGap() 设置/获取线段最大间隙

代码示例

#include <opencv2/opencv.hpp>
#include <opencv2/cudaimgproc.hpp>

int main()
{
    // Step 1: 加载图像并转为灰度图
    cv::Mat h_src = cv::imread("/media/dingxin/data/study/OpenCV/sources/images/lines.png", cv::IMREAD_GRAYSCALE);
    if (h_src.empty()) {
        std::cerr << "无法加载图像!" << std::endl;
        return -1;
    }

    // Step 1.1: 图像预处理(高斯模糊降噪)
    cv::Mat h_blur;
    cv::GaussianBlur(h_src, h_blur, cv::Size(5, 5), 0);

    // Step 2: 上传到 GPU 并执行 Canny 边缘检测
    cv::cuda::GpuMat d_src, d_edges;
    d_src.upload(h_blur);

    cv::Ptr<cv::cuda::CannyEdgeDetector> canny = cv::cuda::createCannyEdgeDetector(100, 200);
    canny->detect(d_src, d_edges);

    // Step 3: 创建概率霍夫变换检测器
    float rho         = 1.0f;            // 距离分辨率
    float theta       = CV_PI / 180.0f;  // 角度分辨率(1 度)
    int threshold     = 30;              // 投票阈值(更敏感)
    int minLineLength = 50;              // 最小线段长度(更短也保留)
    int maxLineGap    = 20;              // 线段之间最大间隙(容许更大间隙)

    cv::Ptr<cv::cuda::HoughSegmentDetector> hough =
        cv::cuda::createHoughSegmentDetector(rho, theta, threshold, minLineLength, maxLineGap);

    // Step 4: 执行线段检测
    cv::cuda::GpuMat d_lines;
    hough->detect(d_edges, d_lines);

    // Step 5: 下载结果
    std::vector<cv::Vec4i> h_lines;
    d_lines.download(h_lines);

    // Step 6: 绘制检测到的线段
    cv::Mat display;
    cv::cvtColor(h_src, display, cv::COLOR_GRAY2BGR);

    for (const auto& line : h_lines)
    {
        cv::Point pt1(line[0], line[1]);
        cv::Point pt2(line[2], line[3]);
        cv::line(display, pt1, pt2, cv::Scalar(0, 255, 0), 2);
    }

    // Step 7: 显示结果
    cv::imshow("Detected Line Segments", display);
    cv::waitKey();

    return 0;
}

运行结果

在这里插入图片描述


网站公告

今日签到

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