Here is a quick (and not optimal, but I won’t do more) adaptation for reading a RTSP stream instead:
#include <cmath>
#include <iostream>
#include "opencv2/core.hpp"
#include <opencv2/core/utility.hpp>
#include "opencv2/videoio.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/cudaimgproc.hpp"
using namespace std;
using namespace cv;
using namespace cv::cuda;
int main()
{
const char* gst_cap = "rtspsrc location=rtsp://127.0.0.1:8554/test ! decodebin ! videoconvert ! video/x-raw,format=GRAY8 ! appsink";
VideoCapture cap(gst_cap);
if( !cap.isOpened() )
{
cout << "Error: Cv::VideoCapture.open() failed" << endl;
return 0;
}
Mat src;
Mat mask;
Mat dst_cpu;
Mat dst_gpu;
GpuMat d_lines;
Ptr<cuda::HoughSegmentDetector> hough = cuda::createHoughSegmentDetector(1.0f, (float) (CV_PI / 180.0f), 50, 5);
namedWindow("source", WINDOW_AUTOSIZE);
namedWindow("detected lines [CPU]", WINDOW_AUTOSIZE);
namedWindow("detected lines [GPU]", WINDOW_AUTOSIZE);
for(;;) {
if (!cap.read(src)) {
cout<<"Capture read error"<<endl;
break;
}
cv::Canny(src, mask, 100, 200, 3);
cv::cvtColor(mask, dst_cpu, COLOR_GRAY2BGR);
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);
{
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);
waitKey(1);
}
return 0;
}