Finding the Corners of the an array of coordinates

2024/10/11 0:31:19

I have a 2D array of Coordinates in Numpy.

My goal is to attempt to find the corners (as if it were a square). So the :

Top left: smallest x, highest y Top right: largest x, largest y bottom left: smallest x, smallest y bottom right: largest x, smallest y

Obviously each of these pairs need to consider the other values.

I was trying to take the min and max depending on the row:

        BottomLeft = np.min(np.min(hull, axis=1), axis=0)

However, this does not keep the pair of values together. It would have to be something like the smallest possible X values, and out of those, the smallest y value. Or something along these lines. I am assuming there is efficient way to do this with numpy?

Here is an example of data:

    [[[260 156]][[248 176]][[235 197]][[233 199]][[192 199]][[174 197]][[160 171]][[150 151]][[154 149]][[156 149]][[260 151]]]

Thanks!

Answer

Per the discussion above, this assumes that one of the pairs with the smallest x value will also correspond to the smallest y value. So you can first find the minimum x-value:

# Some sample data
d = np.array([[3, 1, 4, 1, 5],[8, 0, 4, 2, 3]])
# smallest value in the first row which, I assume, is your x-values
xm = np.min(d[0, :])

Then you can get the subset of values that have that minimum x value like so:

d[:, d[0,:] == 1]

So you can get the min of them via:

np.min(d[1, d[0,:] == 1])
https://en.xdnf.cn/q/118391.html

Related Q&A

How to make a dictionary retain its sort order?

def positive(self):total = {}final = {}for word in envir:for i in self.lst:if word in i:if word in total:total[word] += 1else:total[word] = 1final = sorted(total, reverse = True)return totalThis return…

Counting line frequencies and producing output files

With a textfile like this:a;b b;a c;d d;c e;a f;g h;b b;f b;f c;g a;b d;fHow can one read it, and produce two output text files: one keeping only the lines representing the most often occurring couple …

Check if parent dict is not empty and retrieve the value of the nested dict

Lets suppose that I have a nested dictionary which looks like that:parent_dict = { parent_key: {child_key: child_value}How can I write the following code:if parent_dict.get(parent_key) is not None and …

List combinations in defined range

I am writing parallel rainbow tables generator using parallel python and multiple machines. So far, I have it working on a single machine. It creates all possible passwords, hashes them, saves to file.…

Python turtle drawing a symbol

import turtlewin=turtle.Screen()t = turtle.Turtle() t.width(5)#The vertical and horizontal lines t.left(90) t.forward(70) t.left(90) t.forward(20)t.left(90) t.forward(60) t.left(120) t.forward(35) t.b…

Display a countdown for the python sleep function in discord embed in python

hi all I am doing one discord bot I need to send one countdown its like a cooldown embed after every request I did this code but I dont know how to add this in my embedfor i in range(60,0,-1):print(f&q…

Bypass rate limit for requests.get

I want to constantly scrape a website - once every 3-5 seconds withrequests.get(http://www.example.com, headers=headers2, timeout=35).json()But the example website has a rate limit and I want to bypass…

ValueError when using if commands in function

Im creating some functions that I can call use keywords to call out specific functions,import scipy.integrate as integrate import numpy as npdef HubbleParam(a, model = "None"):if model == &qu…

Python consecutive subprocess calls with adb

I am trying to make a python script to check the contents of a database via adb. The thing is that in my code,only the first subprocess.call() is executed and the rest are ignored. Since i am fairly ne…

Django Page not found(404) error (Library not found)

This is my music\urls.py code:-#/music/ url(r^/$, views.index, name=index),#/music/712/ url(r^(?P<album_id>[0-9]+)/$, views.detail, name=detail),And this is my views.py code:-def index(request):…