houghlines.cpp 2.42 KB
Newer Older
Vladislav Vinogradov's avatar
Vladislav Vinogradov committed
1 2 3
#include <cmath>
#include <iostream>

4 5 6 7
#include "opencv2/core.hpp"
#include <opencv2/core/utility.hpp>
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
8
#include "opencv2/cudaimgproc.hpp"
Vladislav Vinogradov's avatar
Vladislav Vinogradov committed
9 10 11

using namespace std;
using namespace cv;
12
using namespace cv::cuda;
Vladislav Vinogradov's avatar
Vladislav Vinogradov committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

static void help()
{
    cout << "This program demonstrates line finding with the Hough transform." << endl;
    cout << "Usage:" << endl;
    cout << "./gpu-example-houghlines <image_name>, Default is pic1.png\n" << endl;
}

int main(int argc, const char* argv[])
{
    const string filename = argc >= 2 ? argv[1] : "pic1.png";

    Mat src = imread(filename, IMREAD_GRAYSCALE);
    if (src.empty())
    {
        help();
        cout << "can not open " << filename << endl;
        return -1;
    }

    Mat mask;
34
    cv::Canny(src, mask, 100, 200, 3);
Vladislav Vinogradov's avatar
Vladislav Vinogradov committed
35 36

    Mat dst_cpu;
37
    cv::cvtColor(mask, dst_cpu, COLOR_GRAY2BGR);
Vladislav Vinogradov's avatar
Vladislav Vinogradov committed
38 39 40
    Mat dst_gpu = dst_cpu.clone();

    vector<Vec4i> lines_cpu;
41
    {
42
        const int64 start = getTickCount();
43

44
        cv::HoughLinesP(mask, lines_cpu, 1, CV_PI / 180, 50, 60, 5);
45 46 47 48 49

        const double timeSec = (getTickCount() - start) / getTickFrequency();
        cout << "CPU Time : " << timeSec * 1000 << " ms" << endl;
        cout << "CPU Found : " << lines_cpu.size() << endl;
    }
Vladislav Vinogradov's avatar
Vladislav Vinogradov committed
50 51 52 53

    for (size_t i = 0; i < lines_cpu.size(); ++i)
    {
        Vec4i l = lines_cpu[i];
54
        line(dst_cpu, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0, 0, 255), 3, LINE_AA);
Vladislav Vinogradov's avatar
Vladislav Vinogradov committed
55 56
    }

57
    GpuMat d_src(mask);
Vladislav Vinogradov's avatar
Vladislav Vinogradov committed
58
    GpuMat d_lines;
59
    {
60
        const int64 start = getTickCount();
61

62
        Ptr<cuda::HoughSegmentDetector> hough = cuda::createHoughSegmentDetector(1.0f, (float) (CV_PI / 180.0f), 50, 5);
63 64

        hough->detect(d_src, d_lines);
65 66 67 68 69

        const double timeSec = (getTickCount() - start) / getTickFrequency();
        cout << "GPU Time : " << timeSec * 1000 << " ms" << endl;
        cout << "GPU Found : " << d_lines.cols << endl;
    }
Vladislav Vinogradov's avatar
Vladislav Vinogradov committed
70 71 72 73 74 75 76 77 78 79 80
    vector<Vec4i> lines_gpu;
    if (!d_lines.empty())
    {
        lines_gpu.resize(d_lines.cols);
        Mat h_lines(1, d_lines.cols, CV_32SC4, &lines_gpu[0]);
        d_lines.download(h_lines);
    }

    for (size_t i = 0; i < lines_gpu.size(); ++i)
    {
        Vec4i l = lines_gpu[i];
81
        line(dst_gpu, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0, 0, 255), 3, LINE_AA);
Vladislav Vinogradov's avatar
Vladislav Vinogradov committed
82 83 84 85 86 87 88 89 90
    }

    imshow("source", src);
    imshow("detected lines [CPU]", dst_cpu);
    imshow("detected lines [GPU]", dst_gpu);
    waitKey();

    return 0;
}