沿路径的样本数组 [英] Sample array along path

查看:79
本文介绍了沿路径的样本数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个二进制图像:

为0和1值的numpy数组.

as numpy array of 0 and 1 values.

我想在路径的每10个像素上对其进行采样,例如:

I want to sample it on every 10th pixel along the path, like:

我知道如何通过对数组进行切片来对正交对象进行采样,但是我不知道如何对不规则形状进行处理,并获得均匀分布的点"集.

I know how to sample orthogonal objects, by slicing the array, but I don't know what to do on irregular shape, and get evenly distributed set of "points".

推荐答案

您可以使用OpenCV通过findContours查找路径.这是演示代码x& y是路径上像素的坐标.

You can use OpenCV to find the path by findContours. Here is the demo code, x & y are the coordinates of the pixels on the path.

import numpy as np
import cv2
import pylab as pl
img = cv2.imread("img.png")
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret,img = cv2.threshold(img,127,255,0)
r = cv2.findContours(255-img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
c = r[0][0]
x, y = cc[::10, 0, 0], cc[::10, 0, 1]
pl.figure(figsize=(10, 10))
pl.imshow(img, cmap="gray", interpolation="nearest")
pl.plot(cc[::10, 0, 0], cc[::10, 0, 1], "o")

以下是输出:

我只是从路径中每10个点选择一个点,所以两个相邻点之间的距离并不相同.但是您可以使用一些插值方法将路径转换为平滑曲线,然后找到等距点.

I just select one point every 10 points from the path, so the distance between two nearby points are not the same. But you can use some interpolation method to convert the path to a smooth curve, and then find equidistance points.

这篇关于沿路径的样本数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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