OpenCV  3.2.0
Open Source Computer Vision
houghlines.cpp

An example using the Hough line detector

#include <cmath>
#include <iostream>
#include "opencv2/core.hpp"
using namespace std;
using namespace cv;
using namespace cv::cuda;
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 ../data/pic1.png\n" << endl;
}
int main(int argc, const char* argv[])
{
const string filename = argc >= 2 ? argv[1] : "../data/pic1.png";
Mat src = imread(filename, IMREAD_GRAYSCALE);
if (src.empty())
{
help();
cout << "can not open " << filename << endl;
return -1;
}
Mat mask;
cv::Canny(src, mask, 100, 200, 3);
Mat dst_cpu;
cv::cvtColor(mask, dst_cpu, COLOR_GRAY2BGR);
Mat dst_gpu = dst_cpu.clone();
vector<Vec4i> lines_cpu;
{
const int64 start = getTickCount();
cv::HoughLinesP(mask, lines_cpu, 1, CV_PI / 180, 50, 60, 5);
const double timeSec = (getTickCount() - start) / getTickFrequency();
cout << "CPU Time : " << timeSec * 1000 << " ms" << endl;
cout << "CPU Found : " << lines_cpu.size() << endl;
}
for (size_t i = 0; i < lines_cpu.size(); ++i)
{
Vec4i l = lines_cpu[i];
line(dst_cpu, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0, 0, 255), 3, LINE_AA);
}
GpuMat d_src(mask);
GpuMat d_lines;
{
const int64 start = getTickCount();
hough->detect(d_src, d_lines);
const double timeSec = (getTickCount() - start) / getTickFrequency();
cout << "GPU Time : " << timeSec * 1000 << " ms" << endl;
cout << "GPU Found : " << d_lines.cols << endl;
}
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];
line(dst_gpu, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0, 0, 255), 3, LINE_AA);
}
imshow("source", src);
imshow("detected lines [CPU]", dst_cpu);
imshow("detected lines [GPU]", dst_gpu);
return 0;
}
cv::cuda::GpuMat::empty
bool empty() const
returns true if GpuMat data is NULL
imgproc.hpp
cv::cvtColor
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0)
Converts an image from one color space to another.
cv::waitKey
int waitKey(int delay=0)
Waits for a pressed key.
cv::cuda::GpuMat::cols
int cols
Definition: cuda.hpp:286
cv::cuda::createHoughSegmentDetector
Ptr< HoughSegmentDetector > createHoughSegmentDetector(float rho, float theta, int minLineLength, int maxLineGap, int maxLines=4096)
Creates implementation for cuda::HoughSegmentDetector .
cv::Canny
void Canny(InputArray image, OutputArray edges, double threshold1, double threshold2, int apertureSize=3, bool L2gradient=false)
Finds edges in an image using the Canny algorithm .
highgui.hpp
cv::IMREAD_GRAYSCALE
@ IMREAD_GRAYSCALE
If set, always convert image to the single channel grayscale image.
Definition: imgcodecs.hpp:66
int64
int64_t int64
Definition: interface.h:57
cv::line
void line(InputOutputArray img, Point pt1, Point pt2, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
Draws a line segment connecting two points.
cv::imread
Mat imread(const String &filename, int flags=IMREAD_COLOR)
Loads an image from a file.
cv::cuda::GpuMat::download
void download(OutputArray dst) const
pefroms download data from device to host memory (Blocking call)
cv::Mat::empty
bool empty() const
Returns true if the array has no elements.
cv::Vec
Template class for short numerical vectors, a partial case of Matx.
Definition: matx.hpp:306
cudaimgproc.hpp
cv::Ptr
Template class for smart pointers with shared ownership.
Definition: cvstd.hpp:281
cv::getTickCount
int64 getTickCount()
Returns the number of ticks.
cv::cuda
Definition: cuda.hpp:65
cv::LINE_AA
@ LINE_AA
antialiased line
Definition: core.hpp:218
cv::cuda::GpuMat
Base storage class for GPU memory with reference counting.
Definition: cuda.hpp:97
cv::imshow
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
CV_32SC4
#define CV_32SC4
Definition: interface.h:109
cv::getTickFrequency
double getTickFrequency()
Returns the number of ticks per second.
cv::Scalar
Scalar_< double > Scalar
Definition: types.hpp:606
cv::Mat::clone
Mat clone() const
Creates a full copy of the array and the underlying data.
cv::Point
Point2i Point
Definition: types.hpp:183
cv::Mat
n-dimensional dense array class
Definition: mat.hpp:741
i
for i
Definition: modelConvert.m:63
cv::HoughLinesP
void HoughLinesP(InputArray image, OutputArray lines, double rho, double theta, int threshold, double minLineLength=0, double maxLineGap=0)
Finds line segments in a binary image using the probabilistic Hough transform.
core.hpp
cv
Definition: affine.hpp:52
CV_PI
#define CV_PI
Definition: cvdef.h:302
cv::COLOR_GRAY2BGR
@ COLOR_GRAY2BGR
Definition: imgproc.hpp:540
utility.hpp
cv::cuda::HoughSegmentDetector::detect
virtual void detect(InputArray src, OutputArray lines, Stream &stream=Stream::Null())=0
Finds line segments in a binary image using the probabilistic Hough transform.