Scipy: Centroid of convex hull

2024/11/15 16:51:28

how can I calculate the centroid of a convex hull using python and scipy? All I found are methods for computing Area and Volume.

regards,frank.

Answer

Assuming you have constructed the convex hull using scipy.spatial.ConvexHull, the returned object should then have the positions of the points, so the centroid may be as simple as,

import numpy as np
from scipy.spatial import ConvexHullpoints = np.random.rand(30, 2)   # 30 random points in 2-D
hull = ConvexHull(points)#Get centoid
cx = np.mean(hull.points[hull.vertices,0])
cy = np.mean(hull.points[hull.vertices,1])

Which you can plot as follows,

import matplotlib.pyplot as plt
#Plot convex hull
for simplex in hull.simplices:plt.plot(points[simplex, 0], points[simplex, 1], 'k-')#Plot centroid
plt.plot(cx, cy,'x',ms=20)
plt.show()

The scipy convex hull is based on Qhull which should have method centrum, from the Qhull docs,

A centrum is a point on a facet's hyperplane. A centrum is the average of a facet's vertices. Neighboring facets are convex if each centrum is below the neighbor facet's hyperplane.

where the centrum is the same as a centroid for simple facets,

For simplicial facets with d vertices, the centrum is equivalent to the centroid or center of gravity.

As scipy doesn't seem to provide this, you could define your own in a child class to hull,

class CHull(ConvexHull):def __init__(self, points):ConvexHull.__init__(self, points)def centrum(self):c = []for i in range(self.points.shape[1]):c.append(np.mean(self.points[self.vertices,i]))return chull = CHull(points)c = hull.centrum()
https://en.xdnf.cn/q/71866.html

Related Q&A

Creating a montage of pictures in python

I have no experience with python, but the owner of this script is not responding.When I drag my photos over this script, to create a montage, it ends up cutting off half of the last photo on the right …

stop python program when ssh pipe is broken

Im writing a python script with an infinite while loop that I am running over ssh. I would like the script to terminate when someone kills ssh. For example:The script (script.py):while True:# do someth…

How do I export a TensorFlow model as a .tflite file?

Background information:I have written a TensorFlow model very similar to the premade iris classification model provided by TensorFlow. The differences are relatively minor: I am classifying football ex…

Using plotly in Jupyter to create animated chart in off-line mode

Ive been trying to get the "Filled-Area Animation in Python" example to work using plotly in offline mode in a Jupyter notebook. The example can be found here: https://plot.ly/python/filled-a…

Django: How to unit test Update Views/Forms

Im trying to unit test my update forms and views. Im using Django Crispy Forms for both my Create and Update Forms. UpdateForm inherits CreateForm and makes a small change to the submit button text. Th…

Why is Python faster than C++ in this case?

A program in both Python and C++ is given below, which performs the following task: read white-space delimited words from stdin, print the unique words sorted by string length along with a count of eac…

Python - write headers to csv

Currently i am writing query in python which export data from oracle dbo to .csv file. I am not sure how to write headers within file. try:connection = cx_Oracle.connect(user,pass,tns_name)cursor = con…

Opening/Attempting to Read a file [duplicate]

This question already has answers here:PyCharm shows unresolved references error for valid code(31 answers)Closed 5 years ago.I tried to simply read and store the contents of a text file into an array,…

How to pass custom settings through CrawlerProcess in scrapy?

I have two CrawlerProcesses, each is calling different spider. I want to pass custom settings to one of these processes to save the output of the spider to csv, I thought I could do this:storage_setti…

numpy how to slice index an array using arrays?

Perhaps this has been raised and addressed somewhere else but I havent found it. Suppose we have a numpy array: a = np.arange(100).reshape(10,10) b = np.zeros(a.shape) start = np.array([1,4,7]) # ca…