I have a numpy array with 4 coordinates of rect/quadrilateral:
pts = np.array([(690, 110), (345, 130), (690, 300), (445, 298)])
Note that these are not in order.
Now I want to find the top_left, top right, bottom left, bottom right of a rectangle/quadrilateral.
Points are randomly arranged in array like this:
I have designed some manual logic, but it is not correct. Need some help. My Logic:
import numpy as np
pts = np.array([(690, 110), (345, 130), (690, 300), (445, 298)])
min_X_Cord = pts[np.where(pts == pts[:, 0].min())[0]]
sec_min = pts[np.where(pts == np.unique(pts[:, 0])[1])[0]]
top2 = np.array([min_X_Cord, sec_min]).reshape(-1, 2)
top_left, bot_left = (
top2[np.where(top2 == top2[:, 1].max())[0]],
top2[np.where(top2 == top2[:, 1].min())[0]],
)
print(top_left, bot_left)
max_X_Cord = pts[np.where(pts == pts[:, 0].max())[0]]
sec_min = pts[np.where(pts == np.unique(pts[:, 0])[-2])[0]]
Edit
My question might not be clear earlier. If you have 4 points on a coordinate system, you can make a rectangle, or quadrilateral out of them. Now the problem is that to do that and perform some further operations on it, I do need to know that which point is top left, which is top right, which is bottom left, which is bottom right.
For example, in this array:
[[300,120], [213,306], [538, 171], [550, 374]]
(300, 110) is top left, (538, 171) is top right, (213, 306) is bottom left and (550, 374) is bottom right. There is no specific order in it, like sometimes my cordinates can be like
[[213,306],[300,120], [550, 374], [538, 171]]
Same coordinates with different positions.
Now I want to extract the top left, top right, bottom left, bottom right.