简体   繁体   English

从一组点中找到近似矩形

[英]Finding approximate rectangle in from a set of points

I have to extract an area from a image (can't disclose that image). 我必须从图像中提取区域(无法公开该图像)。 With the help of this Abid K Rahman's answer I've obtained the image as Result Image I've obtained the points of that may make a rectangle but couldn't find a rectangle which is the best approximate. 借助此Abid K Rahman的答案,我获得了该图像,作为结果图像。我获得了该图像的要点,它们可能会形成一个矩形,但找不到最近似的矩形。

    [[625, 389], [10, 385], [116, 184], [5, 35], [626, 26]]

We can assume that all four points of the rectangle will be in four different corner of image. 我们可以假设矩形的所有四个点都将位于图像的四个不同角中。 So I have divided the set into four different group based on their location. 因此,我根据位置将其分为四个不同的组。

    [[[[41, 63]], [[613, 66]]], [[[227, 428], [25, 426], [39, 392]], [[612, 394]]]]

But I'm not able to move further. 但是我无法继续前进。 I want to extract points which approximately makes a rectangle. 我想提取大约构成矩形的点。 Better if the answer is in python. 如果答案在python中,那就更好了。

So I went on to write my the code using something rayryeng suggested and come up with this. 因此,我继续使用rayryeng建议的方式编写代码,并提出了建议。

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]

Here centroids is the points I have got, which I divided in to quadrants, since in my problem the points need to be from all four qaudrants. 这里的质心是我得到的点,我将其划分为多个象限,因为在我的问题中,这些点必须来自所有四个象限。 I've made the all possible combination of four points. 我已经将所有四点结合在一起。 I found the diagonals, the angle the make at the centre. 我找到了对角线,即中心的角度。 Made a list of which contain the ratio of length of diagonal(smallest to largest), the angle and the length of shortest diagonal. 制成一个列表,其中包含对角线的长度比(最小到最大),角度和最短对角线的长度。 I used the property that the diagonal of rectangle are of equal length. 我使用了矩形的对角线长度相等的属性。 Which is quite useful for now. 目前,这非常有用。 I don't know how to use the other properties hence its kept on hold. 我不知道如何使用其他属性,因此保持不变。 In final answer I get the best rectangle (when there are no points to make a sqaure, also, it doesn't care about the size of rectangle). 在最后的答案中,我得到了最好的矩形(当没有要绘制正方形的点时,它也不在乎矩形的大小)。

Maybe Python Shapely library can help. 也许Python Shapely库可以提供帮助。

Example: 例:

from shapely.geometry import MultiPoint
points = MultiPoint([(0.0, 0.0), (1.0, 1.0), ... ])

points.bounds
# A (minx, miny, maxx, maxy) tuple.

best thing is imcrop(I,rect); 最好的事情是imcrop(I,rect); where rect is define as rect定义为

rect=[xmin ymin width height] rect = [xmin ymin width height]

so at first pair for example [41,63],[613,66] 因此,例如第一对[41,63],[613,66]

xmin=41 xmin = 41

ymin=63 ymin = 63

width=613-41 宽度= 613-41

height=66-63 高度= 66-63

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM