-
Notifications
You must be signed in to change notification settings - Fork 32
/
Functions.py
74 lines (49 loc) · 2.26 KB
/
Functions.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import math
import cv2
class ifChar:
# this function contains some operations used by various function in the code
def __init__(self, cntr):
self.contour = cntr
self.boundingRect = cv2.boundingRect(self.contour)
[x, y, w, h] = self.boundingRect
self.boundingRectX = x
self.boundingRectY = y
self.boundingRectWidth = w
self.boundingRectHeight = h
self.boundingRectArea = self.boundingRectWidth * self.boundingRectHeight
self.centerX = (self.boundingRectX + self.boundingRectX + self.boundingRectWidth) / 2
self.centerY = (self.boundingRectY + self.boundingRectY + self.boundingRectHeight) / 2
self.diagonalSize = math.sqrt((self.boundingRectWidth ** 2) + (self.boundingRectHeight ** 2))
self.aspectRatio = float(self.boundingRectWidth) / float(self.boundingRectHeight)
class PossiblePlate:
def __init__(self):
self.Plate = None
self.Grayscale = None
self.Thresh = None
self.rrLocationOfPlateInScene = None
self.strChars = ""
# this function is a 'first pass' that does a rough check on a contour to see if it could be a char
def checkIfChar(possibleChar):
if (possibleChar.boundingRectArea > 80 and possibleChar.boundingRectWidth > 2
and possibleChar.boundingRectHeight > 8 and 0.25 < possibleChar.aspectRatio < 1.0):
return True
else:
return False
# check the center distance between characters
def distanceBetweenChars(firstChar, secondChar):
x = abs(firstChar.centerX - secondChar.centerX)
y = abs(firstChar.centerY - secondChar.centerY)
return math.sqrt((x ** 2) + (y ** 2))
# use basic trigonometry (SOH CAH TOA) to calculate angle between chars
def angleBetweenChars(firstChar, secondChar):
adjacent = float(abs(firstChar.centerX - secondChar.centerX))
opposite = float(abs(firstChar.centerY - secondChar.centerY))
# check to make sure we do not divide by zero if the center X positions are equal
# float division by zero will cause a crash in Python
if adjacent != 0.0:
angleInRad = math.atan(opposite / adjacent)
else:
angleInRad = 1.5708
# calculate angle in degrees
angleInDeg = angleInRad * (180.0 / math.pi)
return angleInDeg