我可以将“稀疏”轮廓转换为Python中的“密集”轮廓吗?

huangapple go评论62阅读模式
英文:

Can I convert "sparse" contour to "dense" contour in Python?

问题

OpenCV的findContours()函数提供轮廓压缩功能。

  1. 如果传递cv2.CHAIN_APPROX_NONE选项,轮廓不会被近似,而是返回轮廓上的每个点(即“密集”轮廓)。
  2. 如果传递cv2.CHAIN_APPROX_SIMPLE选项,形成直线段的点会被压缩,以便中间的点被删除(即“稀疏”轮廓)。

我想将一个稀疏轮廓转换为密集轮廓。我考虑过对稀疏点进行插值,但我想要“精确”的结果,不带任何参数。换句话说,我想要从稀疏点重新绘制轮廓并使用cv2.CHAIN_APPROX_NONE选项重新应用findContours()时得到的结果。有没有办法实现这个目标?

英文:

OpenCV's findContours() function provides contour compression.

  1. If cv2.CHAIN_APPROX_NONE option is passed, contour is not approximated and every point on the contour is returned (i.e., "dense" contour).
  2. If cv2.CHAIN_APPROX_SIMPLE option is passed, the points forming a straight line segment are compressed so that the points in between are gone (i.e., "sparse" contour).

I want to transform a sparse contour to a dense contour. I considered interpolating the sparse points, but I want the "exact" result without any parameter. In other words, I want what I would get if I re-drew the contour from sparse points and re-applied findContours() with cv2.CHAIN_APPROX_NONE option. Is there any way I can achieve this?

答案1

得分: 2

看起来cv2.CHAIN_APPROX_SIMPLE执行点的无损压缩,即减少点的数量但不“逼近”原始曲线。

这意味着从cv2.CHAIN_APPROX_SIMPLE获得的线段之间的角度始终为n*pi/4。因此,可以通过简单的插值来增加轮廓的密度:

  1. 如果线段的角度为n*pi/2,用距离1进行插值。如果角度为(2n + 1)*pi/4,则用距离sqrt(2)进行插值。
  2. 四舍五入插值点的坐标。
英文:

It seems that cv2.CHAIN_APPROX_SIMPLE performs lossless compression of the points, i.e., it reduces the number of points but does not "approximate" the original curve.

This means that the angle between the line segments from cv2.CHAIN_APPROX_SIMPLE is always n*pi/4. Thus densifying the contour can be done by simple interpolation:

  1. If line has angle n*pi/2, interpolate with distnace 1. If the angle is (2n + 1)*pi/4, interpolate with distance sqrt(2).
  2. Round the coordinates of interpolated points.

huangapple
  • 本文由 发表于 2023年7月13日 20:23:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76679340.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定