平滑二维图形 [英] Smoothing a 2-D figure

查看:144
本文介绍了平滑二维图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些模糊的矩形2D图形需要进行平滑处理.简化示例:

fig, ax1 = plt.subplots(1,1, figsize=(3,3))
xs1 = [-0.25,  -0.625, -0.125, -1.25, -1.125, -1.25, 0.875, 1.0, 1.0, 0.5, 1.0, 0.625, -0.25]
ys1 = [1.25, 1.375, 1.5, 1.625, 1.75, 1.875, 1.875, 1.75, 1.625, 1.5, 1.375, 1.25, 1.25]

ax1.plot(xs1, ys1)
ax1.set_ylim(0.5,2.5)
ax1.set_xlim(-2,2) ;

我尝试了scipy.interpolate.RectBivariateSpline,但显然要在所有点上都需要数据(例如用于热图),而scipy.interpolate.interp1d却想要生成一个1d平滑版本.

有什么合适的方法可以解决这个问题?

编辑以更好地修改/说明我的目标.我不需要这些线就可以遍历所有要点.实际上,我更希望它们不经过所有要点,因为存在明显的离群点,应该"与邻居或某些类似方法进行平均.我已经在上面包括了我心目中的粗略手工草图.

解决方案

Chaikin的切角算法可能是您的理想方法.对于给定的顶点为P0,P1,... P(N-1)的多边形,边角切割算法将为P(i)和P(i + 1)定义的每个线段生成2个新顶点,

Q(i)=(3/4)P(i)+(1/4)P(i + 1)
R(i)=(1/4)P(i)+(3/4)P(i + 1)

因此,您的新多边形将具有2N个顶点.然后,可以重复并反复在新多边形上应用拐角切割,直到达到所需的分辨率为止.结果将是具有许多顶点的多边形,但显示时它们将看起来很平滑.可以证明,通过这种切角方法生成的结果曲线将收敛为二次B样条曲线.这种方法的优点是所产生的曲线永远不会过冲.以下图片将为您提供有关此算法的更好的主意(图片取自链接.算法.

I have a number of vaguely rectangular 2D figures that need to be smoothed. A simplified example:

fig, ax1 = plt.subplots(1,1, figsize=(3,3))
xs1 = [-0.25,  -0.625, -0.125, -1.25, -1.125, -1.25, 0.875, 1.0, 1.0, 0.5, 1.0, 0.625, -0.25]
ys1 = [1.25, 1.375, 1.5, 1.625, 1.75, 1.875, 1.875, 1.75, 1.625, 1.5, 1.375, 1.25, 1.25]

ax1.plot(xs1, ys1)
ax1.set_ylim(0.5,2.5)
ax1.set_xlim(-2,2) ;

I have tried scipy.interpolate.RectBivariateSpline but that apparently wants data at all the points (e.g. for a heat map), and scipy.interpolate.interp1d but that, reasonably enough, wants to generate a 1d smoothed version.

What is an appropriate method to smooth this?

Edit to revise/explain my goal a little better. I don't need the lines to go through all the points; in fact I'd prefer that they not go through all the points, because there are clear outlier points that "should" be averaged with neighbors, or some similar approach. I've included a crude manual sketch of the start of what I have in mind above.

解决方案

Chaikin's corner cutting algorithm might be the ideal approach for you. For a given polygon with vertices as P0, P1, ...P(N-1), the corner cutting algorithm will generate 2 new vertices for each line segment defined by P(i) and P(i+1) as

Q(i) = (3/4)P(i) + (1/4)P(i+1)
R(i) = (1/4)P(i) + (3/4)P(i+1)

So, your new polygon will have 2N vertices. You can then apply the corner cutting on the new polygon again and repeatedly until the desired resolution is reached. The result will be a polygon with many vertices but they will look smooth when displayed. It can be proved that the resulting curve produced from this corner cutting approach will converge into a quadratic B-spline curve. The advantage of this approach is that the resulting curve will never over-shoot. The following pictures will give you a better idea about this algorithm (pictures taken from this link)

Original Polygon

Apply corner cutting once

Apply corner cutting one more time

See this link for more details for Chaikin's corner cutting algorithm.

这篇关于平滑二维图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆