forked from challinan/opencv-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linedetection.cpp
168 lines (138 loc) · 5.03 KB
/
linedetection.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*------------------------------------------------------------------------------------------*\
This program is free software; permission is hereby granted to use, copy, modify,
and distribute this source code, or portions thereof, for any purpose, without fee,
subject to the restriction that the copyright notice may not be removed
or altered from any source or altered source distribution.
The software is released on an as-is basis and without any warranties of any kind.
In particular, the software is not guaranteed to be fault-tolerant or free from failure.
The author disclaims all warranties with regard to this software, any use,
and any consequent failure, is purely the responsibility of the user.
\*------------------------------------------------------------------------------------------*/
//------------------------------------------------------------------------------------------*/
// Hough Transforms
//------------------------------------------------------------------------------------------*/
//------------------------------------------------------------------------------------------*/
//
// Framework by Eric Gregori - BDTi
//
//------------------------------------------------------------------------------------------*/
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video/background_segm.hpp>
#include <stdio.h>
using namespace std;
using namespace cv;
cv::Mat gray, edges;
// threshold – The accumulator threshold parameter. Only those lines are returned that get enough votes ( )
// minLineLength – The minimum line length. Line segments shorter than that will be rejected
// maxLineGap – The maximum allowed gap between points on the same line to link them.
int my_Algorithm(cv:: Mat &frame, int Threshold, int MinLineLength, int MaxLineGap )
{
Mat output;
double t = (double)cvGetTickCount();
// convert to gray-level image
cv::cvtColor(frame, gray, CV_BGR2GRAY);
output = frame.clone();
// Edge detection using Canny
cv::Canny( gray, edges, 50, 200, 3 );
std::vector<cv::Vec4i> lines;
cv::HoughLinesP( edges, lines, 1, CV_PI/180, Threshold, MinLineLength, MaxLineGap );
t = (double)cvGetTickCount() - t;
t = t/((double)cvGetTickFrequency()*1000.);
printf( "detection time = %g ms\n", t );
for( size_t i = 0; i < lines.size(); i++ )
{
cv::Vec4i l = lines[i];
cv::line( output, cv::Point(l[0], l[1]), cv::Point(l[2], l[3]), cv::Scalar(0,0,255), 3, CV_AA);
}
imshow("Edges",edges);
imshow("Hough Lines", output );
return( t );
}
int main( int argc, char *argv[] )
{
cv::VideoCapture cap;
int MinLineLength, MaxLineGap;
int Threshold;
bool Camera;
int framenum;
int frametime, algtime, framedelay;
// Open the video file
Camera = true;
frametime = 33;
if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
cap.open(argc == 2 ? argv[1][0] - '0' : 0);
else if( argc == 2 )
{
cap.open(argv[1]);
Camera = false;
frametime = 1000/cap.get(CV_CAP_PROP_FPS);
}
// check if video successfully opened
if (!cap.isOpened())
{
if( Camera == false )
{
printf( "\n\n!Error! - Failed to open file - %s", argv[1] );
cv::waitKey();
}
else
{
printf( "\n\n!Error! - Faild to connect to web camera" );
cv::waitKey();
}
return 0;
}
// current video frame
cv::Mat frame;
cv::namedWindow("Parameters");
Threshold = 50;
MinLineLength = 50;
MaxLineGap = 10;
// Threshold, MinLineLength, MaxLineGap
cv::createTrackbar( "Threshold", "Parameters", &Threshold, 255, 0 );
cv::createTrackbar( "MinLengh", "Parameters", &MinLineLength, 255, 0 );
cv::createTrackbar( "MaxGap", "Parameters", &MaxLineGap, 255, 0 );
cv::createTrackbar( "delay", "Parameters", &frametime, 100, 0 );
bool stop(false);
framenum = 0;
algtime = 0;
// for all frames in video
while (!stop) {
// read next frame if any
if (!cap.read(frame))
{
if( !Camera )
{
cap.release();
cap.open(argv[1]);
cap.read(frame);
}
else
break;
}
//-------------------------------------------------------------------------
// Insert Algorithm here
//-------------------------------------------------------------------------
cv::Mat &frame_ref = frame;
if( framenum++ > 5 )
algtime = my_Algorithm( frame_ref, Threshold?Threshold:1,MinLineLength, MaxLineGap );
else
continue;
//-------------------------------------------------------------------------
// Algorithm Done
//-------------------------------------------------------------------------
// introduce a delay
// or press key to stop
printf( " frametime = %d", frametime, algtime );
if( frametime > algtime )
framedelay = frametime - algtime;
else
framedelay = 10;
printf( " framedelay = %d", framedelay );
if (cv::waitKey(framedelay)>=0)
stop= true;
}
}