Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/python/test/test_houghlines.py
16337 views
1
#!/usr/bin/python
2
3
'''
4
This example illustrates how to use Hough Transform to find lines
5
'''
6
7
# Python 2/3 compatibility
8
from __future__ import print_function
9
10
import cv2 as cv
11
import numpy as np
12
import sys
13
import math
14
15
from tests_common import NewOpenCVTests
16
17
def linesDiff(line1, line2):
18
19
norm1 = cv.norm(line1 - line2, cv.NORM_L2)
20
line3 = line1[2:4] + line1[0:2]
21
norm2 = cv.norm(line3 - line2, cv.NORM_L2)
22
23
return min(norm1, norm2)
24
25
class houghlines_test(NewOpenCVTests):
26
27
def test_houghlines(self):
28
29
fn = "/samples/data/pic1.png"
30
31
src = self.get_sample(fn)
32
dst = cv.Canny(src, 50, 200)
33
34
lines = cv.HoughLinesP(dst, 1, math.pi/180.0, 40, np.array([]), 50, 10)[:,0,:]
35
36
eps = 5
37
testLines = [
38
#rect1
39
[ 232, 25, 43, 25],
40
[ 43, 129, 232, 129],
41
[ 43, 129, 43, 25],
42
[232, 129, 232, 25],
43
#rect2
44
[251, 86, 314, 183],
45
[252, 86, 323, 40],
46
[315, 183, 386, 137],
47
[324, 40, 386, 136],
48
#triangle
49
[245, 205, 377, 205],
50
[244, 206, 305, 278],
51
[306, 279, 377, 205],
52
#rect3
53
[153, 177, 196, 177],
54
[153, 277, 153, 179],
55
[153, 277, 196, 277],
56
[196, 177, 196, 277]]
57
58
matches_counter = 0
59
60
for i in range(len(testLines)):
61
for j in range(len(lines)):
62
if linesDiff(testLines[i], lines[j]) < eps:
63
matches_counter += 1
64
65
self.assertGreater(float(matches_counter) / len(testLines), .7)
66
67
68
if __name__ == '__main__':
69
NewOpenCVTests.bootstrap()
70
71