英文:
Can I convert "sparse" contour to "dense" contour in Python?
问题
OpenCV的findContours()
函数提供轮廓压缩功能。
- 如果传递
cv2.CHAIN_APPROX_NONE
选项,轮廓不会被近似,而是返回轮廓上的每个点(即“密集”轮廓)。 - 如果传递
cv2.CHAIN_APPROX_SIMPLE
选项,形成直线段的点会被压缩,以便中间的点被删除(即“稀疏”轮廓)。
我想将一个稀疏轮廓转换为密集轮廓。我考虑过对稀疏点进行插值,但我想要“精确”的结果,不带任何参数。换句话说,我想要从稀疏点重新绘制轮廓并使用cv2.CHAIN_APPROX_NONE
选项重新应用findContours()
时得到的结果。有没有办法实现这个目标?
英文:
OpenCV's findContours()
function provides contour compression.
- If
cv2.CHAIN_APPROX_NONE
option is passed, contour is not approximated and every point on the contour is returned (i.e., "dense" contour). - 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。因此,可以通过简单的插值来增加轮廓的密度:
- 如果线段的角度为n*pi/2,用距离1进行插值。如果角度为(2n + 1)*pi/4,则用距离sqrt(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:
- If line has angle n*pi/2, interpolate with distnace 1. If the angle is (2n + 1)*pi/4, interpolate with distance sqrt(2).
- Round the coordinates of interpolated points.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论