Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
132 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can use comprehension if you like (still a loop + ugly):

a = [[[2,5],[3,4],[5,8],[5,6],[5,9]], [[11,14],[12,15],[16,17]]]

[[[j[0], j[1]], [j[0], j[3]], [j[2], j[3]], [j[2], j[1]]] for j in [np.array(i).min(0).tolist() + np.array(i).max(0).tolist() for i in a]]

#[[[2, 4], [2, 9], [5, 9], [5, 4]],
# [[11, 14], [11, 17], [16, 17], [16, 14]]]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...