c++ - cv::gpu::HoughLines 无法正常工作

标签 c++ opencv image-processing cuda gpu

我正在尝试在 cv::gpu::GpuMat 结构上使用霍夫变换来检测一些线。我尝试过使用 gpu::HoughLines 和 gpu::HoughLinesP ,但即使阈值极低,我也根本没有得到任何结果。在调试过程中,我看到应包含结果的容器 (houghLines) 内部只存储了零。下面给出了我编写的代码,

static cv::Mat drawHoughLinesOnMat (cv::gpu::GpuMat hough_Mat, cv::gpu::GpuMat houghLines)
{
    cv::Mat output_Mat;
    cv::cvtColor(cv::Mat(hough_Mat), output_Mat, CV_GRAY2BGR);

    std::vector<cv::Vec4i> lines_vector;
    if (!houghLines.empty())
    {
        lines_vector.resize(houghLines.cols);
        cv::Mat temp_Mat (1, houghLines.cols, CV_8UC3, &lines_vector[0]);
        houghLines.download (temp_Mat);
    }

    for (size_t i=0; i<lines_vector.size(); ++i)
    {
        cv::Vec4i l = lines_vector[i];
        cv::line(output_Mat, cv::Point(l[0], l[1]), cv::Point(l[2], l[3]), cv::Scalar(0, 0, 255), 1, 8);
    }

    return output_Mat;
}


int main()
{
    cv::Mat input = cv::imread(INPUT_DATA_1->c_str(), CV_LOAD_IMAGE_GRAYSCALE);
    std::string imageType = getImgType(input.type());

    cv::gpu::GpuMat mat_input(input), bil_out, mat_thresh, hough_lines;
    cv::gpu::HoughLinesBuf hough_buffer;

    int bilateral_thresh = 15; // 5 == 0.085s; 15 == 0.467s at run-time
    cv::gpu::bilateralFilter(mat_input, bil_out, bilateral_thresh, bilateral_thresh*2, bilateral_thresh/2);
    //cv::gpu::threshold(bil_out, mat_thresh, 10, 255, CV_THRESH_BINARY);
    cv::gpu::Canny(bil_out, mat_thresh, 10, 60, 5);

    cv::gpu::HoughLinesP(mat_thresh, hough_lines, hough_buffer, 1.0f, (float)(CV_PI/180.0f), 5, 1);
    //cv::Mat test_hough(hough_lines);
    cv::Mat hough_Mat = drawHoughLinesOnMat(mat_input, hough_lines);
    cv::gpu::HoughLines(mat_thresh, hough_lines,  1.0f, (float)(CV_PI/180.0f),   1, true);
    /*cv::Mat */hough_Mat = drawHoughLinesOnMat(mat_input, hough_lines);

    return EXIT_SUCCESS
}

我使用的图像是,

enter image description here

有人可以告诉我我做错了什么吗?提前致谢。!

Canny 过滤器的输出是,

enter image description here

编辑:

我已经在 HoughLines 的 CPU 版本上进行了测试,它似乎工作得很好。

EDIT_2:

@jet47 发布的解决方案完美运行。

最佳答案

您使用了错误的代码将结果从 GPU 下载回 CPU:

lines_vector.resize(houghLines.cols);
cv::Mat temp_Mat (1, houghLines.cols, CV_8UC3, &lines_vector[0]);
houghLines.download (temp_Mat);

您使用的 temp_Mat 类型不正确 - CV_8UC3,它必须是 CV_32SC4

正确的代码是:

lines_vector.resize(houghLines.cols);
cv::Mat temp_Mat(1, houghLines.cols, CV_32SC4, &lines_vector[0]);
houghLines.download(temp_Mat);

关于c++ - cv::gpu::HoughLines 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20121483/

相关文章:

matlab - HaarTraining - MATLAB 还是 OpenCV?

OpenCV::matchShapes() 示例?

matlab - Matlab中图像的边界去除

java - 在 Scala 中开发图像处理 DSL - 建议库

c++ - 哪种方法可以更好地找到最大数量?

c++ - Boost.Asio 链接错误

c++ - 链接列表最后添加不起作用

c++ - 如何使用 Qt 5.6 在高 dpi 上获得清晰的 UI?

c++ - Makefile 和库依赖关系不好?

opencv - 拼接一些图像使用JavaCV