[英]Is there a term for finding a minimal set of N points that approximate a curve?
[英]Finding approximate rectangle in from a set of points
我必须从图像中提取区域(无法公开该图像)。 借助此Abid K Rahman的答案,我获得了该图像,作为结果图像。我获得了该图像的要点,它们可能会形成一个矩形,但找不到最近似的矩形。
[[625, 389], [10, 385], [116, 184], [5, 35], [626, 26]]
我们可以假设矩形的所有四个点都将位于图像的四个不同角中。 因此,我根据位置将其分为四个不同的组。
[[[[41, 63]], [[613, 66]]], [[[227, 428], [25, 426], [39, 392]], [[612, 394]]]]
但是我无法继续前进。 我想提取大约构成矩形的点。 如果答案在python中,那就更好了。
因此,我继续使用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]
这里的质心是我得到的点,我将其划分为多个象限,因为在我的问题中,这些点必须来自所有四个象限。 我已经将所有四点结合在一起。 我找到了对角线,即中心的角度。 制成一个列表,其中包含对角线的长度比(最小到最大),角度和最短对角线的长度。 我使用了矩形的对角线长度相等的属性。 目前,这非常有用。 我不知道如何使用其他属性,因此保持不变。 在最后的答案中,我得到了最好的矩形(当没有要绘制正方形的点时,它也不在乎矩形的大小)。
也许Python Shapely库可以提供帮助。
例:
from shapely.geometry import MultiPoint
points = MultiPoint([(0.0, 0.0), (1.0, 1.0), ... ])
points.bounds
# A (minx, miny, maxx, maxy) tuple.
最好的事情是imcrop(I,rect);
rect定义为
rect = [xmin ymin width height]
因此,例如第一对[41,63],[613,66]
xmin = 41
ymin = 63
宽度= 613-41
高度= 66-63
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.