Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get center of set of points using Python

I would like to get the center point(x,y) of a figure created by a set of points.

How do I do this?

like image 678
Dominik Szopa Avatar asked Dec 04 '10 21:12

Dominik Szopa


People also ask

How do you find the center point of a set of points?

Mathwords: Centroid Formula. The coordinates of the centroid of a triangle are found by averaging the x- and y-coordinates of the vertices. This method will also find the centroid (center of mass) of any set of points on the x-y plane.

How do you find the center of a value in Python?

I assume that a point is a tuple like (x,y), so you can use zip to join the x's and y's. Then using the min and max of x and y's, you can determine the center point. x,y=zip(*points) center=(max(x)+min(x))/2., (max(y)+min(y))/2.

What is a centroid in Python?

The training data is split into groups by class label, then the centroid for each group of data is calculated. Each centroid is simply the mean value of each of the input variables. If there are two classes, then two centroids or points are calculated; three classes give three centroids, and so on.

How do you find the center of a bounding box?

p1-> left top corner, p2-> left bottom corner, p3-> right bottom corner, p4-> right top corner as I believe is in Your case, You can treat the center of the bounding box as the intersection of the diagonals. You need to find equations for lines that diagonals are lying on: Y1 = a1*X1+b1; Y2 = a2*X2+b2 .


2 Answers

If you mean centroid, you just get the average of all the points.

x = [p[0] for p in points] y = [p[1] for p in points] centroid = (sum(x) / len(points), sum(y) / len(points)) 
like image 156
Colin Avatar answered Sep 30 '22 18:09

Colin


If the set of points is a numpy array positions of sizes N x 2, then the centroid is simply given by:

centroid = positions.mean(axis=0) 

It will directly give you the 2 coordinates a a numpy array.

In general, numpy arrays can be used for all these measures in a vectorized way, which is compact and very quick compared to for loops.

like image 36
meduz Avatar answered Sep 30 '22 18:09

meduz