I have a list of list where each list corresponds to the boundary point of a figure. I have to extract a list of four points, each defining the enclosing rectangle of the figures. For example, I have this list
[[[2,5],[3,4],[5,8],[5,6],[5,9]],
[[11,14],[12,15],[16,17]],
...
]
Here each list defines a figure boundary. what I have to get is list of four points as
[[min_x, min_y], [min_x, max_y], [max_x, max_y], [max_x, min_y]]
i.e.
[[[2,4],[2,9],[5,9],[5,4]],
[[11,14], [11, 17], [16,17], [16,14]]
...
]
I have done this using python loop which is perfectly working. Here is the code.
cleaned_contours = list()
for cur_cont in contours:
min_x, min_y = cur_cont.min(axis=0).flatten()
max_x, max_y = cur_cont.max(axis=0).flatten()
cleaned_contours.append(np.array([[min_x, min_y], [min_x, max_y], [max_x, max_y], [max_x, min_y]]))
Is there a way I can do this without using loops or list comprehension. I am using python3.
See Question&Answers more detail:os