-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
130 lines (113 loc) · 5.96 KB
/
utils.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
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
'''
@author Skully (https://github.com/ImSkully)
@website https://skully.tech
@email [email protected]
@file utils.py
@updated 13/12/21
'''
import cv2 as cv
import numpy as np
import math
'''
getDistanceBetweenPoints2D(x1, y1, x2, y2)
Gets the distance between the two coordinates provided on a 2D plane.
@param (int) x1 The x1 coordinate to for position 1.
@param (int) y1 The y1 coordinate to for position 1.
@param (int) x2 The x2 coordinate to for position 2.
@param (int) y2 The y2 coordinate to for position 2.
@returns (int) distance The total distance between the two points.
'''
def getDistanceBetweenPoints2D(x1, y1, x2, y2):
distanceX = x1 - x2; distanceY = y1 - y2
return math.sqrt((distanceX * distanceX) + (distanceY * distanceY))
'''
isPositionWithinCircle(x, y, radius, centerPoint)
Takes the given x, y coordinate and checks to see if it is within the boundary of the radius provided.
@param (int) x The x position.
@param (int) y The y position.
@param (int) radius The radius of the circle.
@param (int) centerPoint The center point of this circle.
@returns (Bool) result True if the position given is within the circle, false otherwise.
'''
def isPositionWithinCircle(x, y, radius, centerPoint):
if (((x - centerPoint[0]) ** 2) + ((y - centerPoint[1]) ** 2)) < (radius ** 2): return True
return False
'''
getRingCenter(image)
Gets the center point of the ring within the image provided.
@param image The image containing the o-ring.
@returns (int) [x, y] The x, y position of the ring's center point.
'''
def getRingCenter(image):
x, y, sumX, sumY, labelID = 0, 0, 0, 0, 0 # Initialize coordinate variables.
for x in range(0, image.shape[0]):
for y in range(0, image.shape[1]):
if(image[x, y] == 1): # If the label at the current index is 1.
sumX += x
sumY += y
labelID += 1 # Increase label ID.
y += 1 # Move to next row.
x += 1 # Move to next column.
# Center point is located at the sum of total x values divided by label ID, for both x, y.
return [round(sumX / labelID), round(sumY / labelID)]
'''
getBoundaryRatio(image, centerPoint)
Gets the total ratio of pixels within the boundary of the o-ring vs. the total number of pixels outside of the ring.
@param image The image containing the ring.
@param centerPoint The center point of the ring within the image.
@returns (int) ratio The ratio of total pixels within divided by total pixels outside the boundary.
'''
def getBoundaryRatio(image, centerPoint):
radius = getRingRadius(image, centerPoint) # Get the radius of our ring.
outOfBoundary, withinBoundary = 0, 0
for i in range(0, image.shape[0]):
for j in range(0, image.shape[1]):
withinOuterCircle = isPositionWithinCircle(i, j, radius[0], centerPoint)
withinInnerCircle = isPositionWithinCircle(i, j, radius[1], centerPoint)
# If this position is within the outer radius and not inside the inner radius.
if not isPositionWithinCircle(i, j, radius[1], centerPoint) and withinOuterCircle:
if image[i, j] == 1: withinBoundary += 1 # If this is labelled as 1, increase within boundary.
elif image[i, j] == 0: outOfBoundary += 1 # Otherwise increase outside boundary.
elif withinInnerCircle or (not withinOuterCircle and not withinInnerCircle): # Otherwise if we aren't within the inner or outer circle, or not within either circle.
if image[i, j] == 1: outOfBoundary += 1 # Increase the out of boundary counter.
return outOfBoundary / withinBoundary # Return our ratio of total within/outside the boundaries of the ring.
'''
getRingCircularity(image, centerPoint)
Gets the circularity of the ring in the image provided.
@param image The image containing the ring.
@param centerPoint The position of the center point of the ring in the image.
@returns (int) circularity The circularity of the ring that was found.
'''
def getRingCircularity(image, centerPoint):
distanceToRing = []
for x in range(0, image.shape[0]):
for y in range(0, image.shape[1]):
if (image[x,y] == 1): # If the label at this index is 1.
# Get the distance between this point and our center point and add it to our distances table.
distanceToRing.append(getDistanceBetweenPoints2D(x, y, centerPoint[0], centerPoint[1]))
# Circularity of the image is the mean value of all distances to the center point divided by the standard deviation of all distances.
return np.mean(distanceToRing) / np.std(distanceToRing)
'''
getRingRadius(image, centerPoint)
Gets the radius of the ring within the image provided.
@param image The image containing the ring.
@param centerPoint The center point within the image, used to obtain the distance to the ring.
@return table
{
(int) outerRadius The radius of the outer ring.
(int) innerRadius The radius of the inner ring.
(int) thickness The thickness of the ring.
}
'''
def getRingRadius(image, centerPoint):
innerRingRadius, outerRingRadius, currentPosition = 0, 0, 0
x, y = centerPoint[0], centerPoint[1] # Starting x, y positions are from the center of the ring.
while currentPosition != 2: # While we aren't outside of the ring.
if (currentPosition == 0) and (image[x, y] == 1): # If the current position is within the ring and this is labelled as the ring.
currentPosition = 1 # This position is on the ring.
innerRingRadius = getDistanceBetweenPoints2D(x, y, centerPoint[0], centerPoint[1])
elif (currentPosition == 1) and (image[x, y] == 0): # If the current position is on the ring and its labelled as outside the ring.
currentPosition = 2 # We are at the edge of the ring.
outerRingRadius = getDistanceBetweenPoints2D(x, y, centerPoint[0], centerPoint[1])
x += 1 # Increase x coordinate and continue moving right.
return (outerRingRadius, innerRingRadius, (outerRingRadius - innerRingRadius)) # Thickness of the ring is distance between the outer ring and the inner ring.