-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathquadrilateral.py
51 lines (41 loc) · 919 Bytes
/
quadrilateral.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = "heider"
__doc__ = r"""
Created on 5/5/22
"""
import cv2
import numpy
from warg import Number
__all__ = ["dot_points", "is_quadrilateral"]
def dot_points(a, b, c):
"""
:param a:
:type a:
:param b:
:type b:
:param c:
:type c:
:return:
:rtype:
"""
ab = a - b
cb = c - b
return ab / numpy.linalg.norm(ab) @ cb / numpy.linalg.norm(cb)
def is_quadrilateral(contour, threshold: Number = 0.2) -> bool:
"""
:param contour:
:type contour:
:param threshold:
:type threshold:
:return:
:rtype:
"""
if cv2.isContourConvex(contour):
for i in range(4):
if threshold < abs(
dot_points(contour[i][0], contour[i - 1][0], contour[i - 2][0])
):
return False
return True
return False