将开放曲线转换为有序像素列表:使用 numpy 的 Python 测试代码
我有一个 numpy 数组中的开放曲线的图像,我需要构建一个根据曲线上的位置排序的点坐标列表。 我使用 numpy 编写了一个 草稿脚本和马霍塔斯。它可能不是最佳的。
我知道 OpenCV 可以对闭合曲线做到这一点。 OpenCV 可以用开放曲线做同样的事情(更快)吗?
例如,如果原始曲线是:
[[0 0 0 0 0 0 0]
[0 1 0 0 1 0 0]
[0 0 1 0 0 1 0]
[0 0 0 1 1 0 0]
[0 0 0 0 0 0 0]]
使用np.where(myarray==1)
,我可以获得像素的索引:
(array([1, 1, 2, 2, 3, 3]), array([1, 4, 2, 5, 3, 4]))
但这不是我需要的。我的脚本生成的索引考虑了曲线上像素的顺序:
i= 0 ( 1 , 1 )
i= 1 ( 2 , 2 )
i= 2 ( 3 , 3 )
i= 3 ( 3 , 4 )
i= 4 ( 2 , 5 )
i= 5 ( 1 , 4 )
我想优化我的脚本。有什么想法吗?
I have the image of an open curve in a numpy array and I need to build a list of points coordinates ordered according to their position on the curve.
I wrote a draft script using numpy and mahotas. It may not be optimal.
I know that OpenCV can do this for a closed curve. Can OpenCV do the same (faster) with an open curve?
For example, if the original curve is:
[[0 0 0 0 0 0 0]
[0 1 0 0 1 0 0]
[0 0 1 0 0 1 0]
[0 0 0 1 1 0 0]
[0 0 0 0 0 0 0]]
Using np.where(myarray==1)
, I can get the indices of the pixels:
(array([1, 1, 2, 2, 3, 3]), array([1, 4, 2, 5, 3, 4]))
But this not what I need. My script yields the indices taking into account the order of the pixels on the curve:
i= 0 ( 1 , 1 )
i= 1 ( 2 , 2 )
i= 2 ( 3 , 3 )
i= 3 ( 3 , 4 )
i= 4 ( 2 , 5 )
i= 5 ( 1 , 4 )
I would like to optimize my script. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设矩阵/图像中仅存在一条曲线,并且曲线上的每个点都有 1 到 2 个邻居,以下函数将提供您需要的结果。
它的工作原理是获取最接近左上角的点,并通过迭代查找尚未访问过的最近点直到没有剩余的点来形成点链。对于闭合曲线,链上第一个/最后一个点之间的欧氏距离平方将小于 2。 对于
开放曲线:
对于闭合曲线:
并且一条曲线的起点(距原点最近的点)将曲线一分为二。
Assuming that only one curve is present in the matrix/image and that each point on the curve has between 1 and 2 neighbours, the following function will provide the results you need.
It works by taking the point closest to the top left hand corner, and forming a chain of points by iteratively finding the closest point that has not already been visited until no further points remain. For a closed curve, the squared Euclidean distance between the first/final points on the chain will be less than than 2.
For an open curve:
And for a closed curve:
And a curve where the starting point (closest point to the origin) splits the curve in two.