File size: 10,145 Bytes
17e2002 | 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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 | 0
1
00:00:00,890 --> 00:00:06,370
So let's talk a bit about line detection. Line detection is a very important completive vision method.
1
2
00:00:06,680 --> 00:00:12,770
And you can imagine it being used in applications such as lane detection for self-driving cars or perhaps
2
3
00:00:12,770 --> 00:00:16,680
for something that actually detects lines on a chessboard like this example here.
3
4
00:00:17,330 --> 00:00:24,770
So the two algorithms that are available in OpenCV are Hough lines and probabilistic Hough lines.
4
5
00:00:24,770 --> 00:00:28,280
Now there are some differences between them which illustrate when we run the code.
5
6
00:00:28,430 --> 00:00:32,660
But for now let's talk about how Hough lines represents lines quickly.
6
7
00:00:32,750 --> 00:00:37,310
I won't go into too much detail with the Math but you may have remembered this from high school mathematics
7
8
00:00:37,700 --> 00:00:43,550
that you can read up represent a straight line by this equation y = mx+c, M being a gradient
8
9
00:00:43,550 --> 00:00:46,430
or slope of two lane and C being the y intercept.
9
10
00:00:46,850 --> 00:00:50,510
However in Hough lines we represent lines a little bit differently.
10
11
00:00:50,600 --> 00:00:53,660
So remember OpenCV uses here as this reference point.
11
12
00:00:53,660 --> 00:00:54,850
Top left corner.
12
13
00:00:55,220 --> 00:01:01,310
So what we do here in this representation we have P which is the perpendicular distance of the lane from
13
14
00:01:01,310 --> 00:01:01,990
the origin.
14
15
00:01:02,120 --> 00:01:09,110
In this case it's pixel's and theta being the angle of this line here, the normal of the lane meeting
15
16
00:01:09,110 --> 00:01:14,900
this origin point here and theta is measured in radians in OpenCV
16
17
00:01:14,910 --> 00:01:22,070
So if you actually do open this theta value in openCV or access it you'll see it's in radians.
17
18
00:01:22,100 --> 00:01:26,900
So these are some details here that I won't go into until we reach into the code but this is basically
18
19
00:01:26,910 --> 00:01:32,300
the cv2 Hough Lines function and cv2 to Probabilistc Hough Lines function
19
20
00:01:32,300 --> 00:01:35,410
So let's actually use these methods now in our code.
20
21
00:01:37,430 --> 00:01:37,810
OK.
21
22
00:01:37,870 --> 00:01:43,920
So let's open our line detection code here and select 4.6 OK.
22
23
00:01:43,930 --> 00:01:44,890
So here we go.
23
24
00:01:45,200 --> 00:01:48,580
So firstly let's run this could give you an idea what's going on.
24
25
00:01:49,040 --> 00:01:52,620
So this was a blank Soduko board here where we had lines here.
25
26
00:01:52,670 --> 00:01:56,000
So as we can see we've actually identified all the major lines here.
26
27
00:01:56,000 --> 00:02:00,920
However we've actually left with one line here surprisingly and we've identified multiple lanes in a
27
28
00:02:00,920 --> 00:02:01,750
few locations.
28
29
00:02:01,760 --> 00:02:04,610
You can see there's maybe two lines here two lines here.
29
30
00:02:04,610 --> 00:02:06,320
This one looks like three cause there's.
30
31
00:02:06,430 --> 00:02:07,450
A thicker point (line) here.
31
32
00:02:08,060 --> 00:02:10,420
So you can see it's not perfect.
32
33
00:02:10,420 --> 00:02:13,440
And what researchers do, let's bring it up again.
33
34
00:02:13,570 --> 00:02:15,430
They sometimes merge these lines together.
34
35
00:02:15,520 --> 00:02:19,130
You find the average of the tree closest points and merge them together.
35
36
00:02:19,700 --> 00:02:23,110
So let's see it take a look and see what's going on here.
36
37
00:02:23,420 --> 00:02:29,240
So similarly in the findcontours function we've grey scaled our image we found canny edges because canny edges
37
38
00:02:29,240 --> 00:02:35,800
helps quite a bit especially in off lane transforms not transform sorry as HOV lanes.
38
39
00:02:36,020 --> 00:02:37,580
And these are the parameters here.
39
40
00:02:37,700 --> 00:02:43,730
So these parameters here in this Hough lines function takes it, it takes the input image here and then we
40
41
00:02:43,730 --> 00:02:45,450
set some accuracy parameters here.
41
42
00:02:45,590 --> 00:02:48,910
So the first parameter we set rho or P value here.
42
43
00:02:48,980 --> 00:02:53,180
We typically said this that one pixel but you can experiment with larger values if you wish.
43
44
00:02:53,180 --> 00:02:59,650
And then we said the theta accuracy a pie over 180 using numpy dot pie or np.pie to generate our pie value
44
45
00:02:59,660 --> 00:03:02,110
and then this Value here
45
46
00:03:02,180 --> 00:03:03,460
This here is a key parameter.
46
47
00:03:03,470 --> 00:03:06,130
This is a threshold for determining if something is a line.
47
48
00:03:06,410 --> 00:03:12,590
So if we set this at a lower value let's say at 150 we should get a lot more lines, way too much lines
48
49
00:03:12,590 --> 00:03:13,160
here!
49
50
00:03:13,610 --> 00:03:16,930
And if we said this at a higher value let's say 300
50
51
00:03:17,570 --> 00:03:19,950
We get only some few lines.
51
52
00:03:19,970 --> 00:03:23,870
So 240 was a good parameter to use for this.
52
53
00:03:23,870 --> 00:03:29,510
We only miss, actually we missed two lines here miscounted this one here and that's the first part of
53
54
00:03:29,510 --> 00:03:31,630
the function there.
54
55
00:03:31,650 --> 00:03:34,080
So the next part of the code looks a bit confusing.
55
56
00:03:34,320 --> 00:03:40,440
But what's important to remember here is that cv2.HoughLines generates this object here, lines and lines
56
57
00:03:40,440 --> 00:03:44,040
has rho and theta values inside of it.
57
58
00:03:44,040 --> 00:03:48,750
So what's going on here is that we're actually looping through all the values inside of lines here and
58
59
00:03:48,750 --> 00:03:52,450
converting it into a format where we can actually get the endpoints of lines.
59
60
00:03:52,610 --> 00:03:58,260
That's what we use to do that line which we went through in section 2 I believe and we just plot all
60
61
00:03:58,260 --> 00:04:03,130
the lines that this cv2.HoughLines function generates.
61
62
00:04:03,140 --> 00:04:07,090
So now let's take a look at probabilistic Hough Lines which is a little bit different.
62
63
00:04:07,100 --> 00:04:10,910
And actually the code looks a lot shorter and that's mainly because we don't need to convert it into
63
64
00:04:11,030 --> 00:04:16,060
a format the line format into into something that cv2.line can use.
64
65
00:04:16,190 --> 00:04:22,190
It actually generates a start and end points of the lines automatically here and also has similar parameters
65
66
00:04:22,190 --> 00:04:27,770
here where we have pixel accuracy theta accuracy but it also has some new thresholds here.
66
67
00:04:27,770 --> 00:04:33,200
There's a minimum line length and a maximum line gap as well as the threshold value.
67
68
00:04:33,590 --> 00:04:37,250
And similarly we do have grayscale and use Canny Edges again
68
69
00:04:37,340 --> 00:04:42,190
So now let's run the probabilistic hough lines function which exists right here.
69
70
00:04:44,080 --> 00:04:53,600
And it looks a lot different you actually see what looks like small lines like worms almost on image
70
71
00:04:54,290 --> 00:04:58,070
and we can actually play with parameters to actually get straight lines if you want.
71
72
00:04:58,280 --> 00:05:04,360
But atually let's bring that up one more time what's key to note here is that because of these new parameters
72
73
00:05:04,400 --> 00:05:08,700
we have maximum length gaps maximum lane gaps actually eliminate the need.
73
74
00:05:08,810 --> 00:05:13,460
Well the problem where we have multiple lanes drawn over each other in the same lane here.
74
75
00:05:13,670 --> 00:05:18,770
If we set a maximum lane gap and see five or 10 there's no there's not going to be any lanes running
75
76
00:05:18,770 --> 00:05:26,720
over the existing line that are similar as well as we have a minimum length of five for lanes five pixels is
76
77
00:05:26,720 --> 00:05:27,620
quite small.
77
78
00:05:27,620 --> 00:05:31,330
But generally we can start with that if you want to get many lines here.
78
79
00:05:31,590 --> 00:05:33,860
Lines and a character perhaps.
79
80
00:05:33,920 --> 00:05:39,820
And let's actually play with the threshold value and see what happens now.
80
81
00:05:39,850 --> 00:05:42,560
So let's set this 50.
81
82
00:05:42,680 --> 00:05:43,130
There we go.
82
83
00:05:43,170 --> 00:05:44,880
I'm seeing slightly more lines here.
83
84
00:05:44,900 --> 00:05:46,190
It looks generally similar.
84
85
00:05:46,290 --> 00:05:51,920
So let's actually set this 10 actually less line sorry.
85
86
00:05:51,930 --> 00:05:57,640
So maybe we need to set this at a higher value.
86
87
00:05:58,070 --> 00:06:01,100
So that gives you a feel of these parameters here.
87
88
00:06:01,100 --> 00:06:05,220
Hopefully you would you would probably find whichever one more useful.
88
89
00:06:05,830 --> 00:06:07,880
Generally I tend to use Houghlines more.
89
90
00:06:08,110 --> 00:06:10,860
But they are definitely use cases for probabilistic.
90
91
00:06:11,170 --> 00:06:15,310
And if you'd like to learn more about probabilistic hough lines I've actually put a link at the bottom
91
92
00:06:15,310 --> 00:06:15,800
here.
92
93
00:06:16,060 --> 00:06:20,980
This is a link to the research paper where probabilistic Hough Lines with first proposed.
93
94
00:06:20,980 --> 00:06:23,600
So hope you enjoyed this lesson on line detection.
94
95
00:06:23,710 --> 00:06:24,190
Thanks.
|