<p>所以我继续用<a href="https://stackoverflow.com/users/3250829/rayryeng">rayryeng</a>建议的方法编写代码,并想出了这个。在</p>
<pre><code>c = [[[] for x in range(2)] for y in range (2)]
for xy in centroids:
x_i = xy[0]*2/col
y_i = xy[1]*2/row
c[y_i][x_i].append(np.array(xy))
combination = []
combination = np.array([ np.array([q1,q2,q3,q4]) for q1 in c[0][0] for q2 in c[0][1] for q3 in c[1][1] for q4 in c[1][0]])
if len(combination)>0 :
key = 0
property = [[ 0 for j in range(4)] for i in range(len(combination))]
for i in range(len(combination)):
q = combination[i]
d1,d2 = q[2]-q[0],q[1]-q[3]
d1_len,d2_len = np.sqrt(sum(d1**2)),np.sqrt(sum(d2**2))
angle = math.degrees(math.acos(sum(d1*d2)/(d1_len*d2_len)))
if d1_len > d2_len:
r,extent = int(100*d1_len/d2_len),d2_len
else:
r,extent = int(100*d2_len/d1_len),d1_len
property[i] = [r,angle,extent,i]
property.sort(key = lambda x:x[0])
key = property[0][3]
combination = combination[key]
</code></pre>
<p>这里质心是我得到的点,我把它分成四个象限,因为在我的问题中,这些点需要来自所有四个象限。我把四点结合起来。
我找到了对角线,中心的角度。
列出了其中包含对角线长度(最小值与最大值)之比、角度和最短对角线长度。
我用了矩形的对角线等长的性质。这对现在很有用。我不知道如何使用其他属性,因此它被搁置了。在最后的答案中,我得到了最好的矩形(当没有点来做正方形时,它也不关心矩形的大小)。在</p>