Skip to content

Commit

Permalink
create rectangle object
Browse files Browse the repository at this point in the history
  • Loading branch information
ShisatoYano committed Oct 17, 2023
1 parent 1f69582 commit f6ad090
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,22 @@ def _calculate_rectangle(self, points_array):
if min_cost_angle[0] < cost: min_cost_angle = (cost, angle_rad)
most_fitting_points = self._rotate_points(points_array, min_cost_angle[1])

rectangle = Rectangle()

# create fitting rectangle object
angle_cos, angle_sin = cos(min_cost_angle[1]), sin(min_cost_angle[1])
c1, c2 = most_fitting_points[0, :], most_fitting_points[1, :]
rectangle = Rectangle(a=[angle_cos, -angle_sin, angle_cos, -angle_sin],
b=[angle_sin, angle_cos, angle_sin, angle_cos],
c=[min(c1), min(c2), max(c1), max(c2)])

return rectangle

def _search_rectangles(self, clusters_list):
rectangles_list = []

for cluster in clusters_list:
array_list = [point.get_point_array() for point in list(cluster)]
integrated_array = np.concatenate(array_list, 1)
self._calculate_rectangle(integrated_array)
rectangles_list.append(self._calculate_rectangle(integrated_array))

self.latest_rectangles_list = rectangles_list

Expand Down
41 changes: 28 additions & 13 deletions src/components/detection/l_shape_fitting/rectangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,39 @@
class Rectangle:
"""
Rectangle shape class
Consists of 4 edges, a*x + b*y = c
"""

def __init__(self):
def __init__(self, a, b, c):
"""
Constructor
a: List of coefficient a in 4 edges
b: List of coefficient b in 4 edges
c: List of coefficient c in 4 edges
"""

self.EDGE_NUM = 4
self.VERTEX_NUM = self.EDGE_NUM + 1
# edge parameters
self.a = a # a_1-4
self.b = b # b_1-4
self.c = c # c_1-4

edge_param_list = [None] * self.EDGE_NUM
self.a = edge_param_list
self.b = edge_param_list
self.c = edge_param_list
# contour points
vertex_num = len(self.a) + 1 # edge num of 4 + 1 to close rectangle
self.contour_x = [None] * vertex_num
self.contour_y = [None] * vertex_num
self._calculate_contour()
self.contour = XYArray(np.array([self.contour_x,
self.contour_y]))

vertex_list = [None] * self.VERTEX_NUM
vertex_xs = vertex_list
vertex_ys = vertex_list
contour = np.array([vertex_xs,
vertex_ys])
self.array = XYArray(contour)
@staticmethod
def _calculate_cross_point(a, b, c):
x = (b[0] * c[1] - b[1] * c[0]) / (b[0] * a[1] - b[1] * a[0])
y = (a[0] * c[1] - a[1] * c[0]) / (a[0] * b[1] - a[1] * b[0])
return x, y

def _calculate_contour(self):
self.contour_x[0], self.contour_y[0] = self._calculate_cross_point(self.a[0:2], self.b[0:2], self.c[0:2])
self.contour_x[1], self.contour_y[1] = self._calculate_cross_point(self.a[1:3], self.b[1:3], self.c[1:3])
self.contour_x[2], self.contour_y[2] = self._calculate_cross_point(self.a[2:4], self.b[2:4], self.c[2:4])
self.contour_x[3], self.contour_y[3] = self._calculate_cross_point([self.a[3], self.a[0]], [self.b[3], self.b[0]], [self.c[3], self.c[0]])
self.contour_x[4], self.contour_y[4] = self.contour_x[0], self.contour_y[0]

0 comments on commit f6ad090

Please sign in to comment.