-1

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:

enter image description here

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.

Ahmad Anis
  • 2,322
  • 4
  • 25
  • 54
  • It is not really clear what you want to do. What does the image have to do with anything? – coyote Aug 26 '21 at 07:46
  • Image shows that the point 1, 2,3,4 are not in order, and I want to classify topl, topr, botl, botr from them. – Ahmad Anis Aug 26 '21 at 09:44

1 Answers1

-3
l_d =  (pts[:,0].min(), pts[:,1].min())
r_u =  (pts[:,0].max(), pts[:,1].max())
l_u =  (pts[:,0].min(), pts[:,1].max())
r_d =  (pts[:,0].max(), pts[:,1].min())

print(f"0:{r_u} 1:{l_u} 2:{r_d},3:{l_d}")

enter image description here

I'mahdi
  • 23,382
  • 5
  • 22
  • 30
ZYY
  • 1
  • Welcome to SO and thanks for your contribution. Please don't add code as a screenshot. Search engines can't index it and it will be inaccessible to people who rely on text to speech software to browse the web – Neuron Aug 26 '21 at 08:49