import requests
from PIL import Image
import numpy as np
import io
from sklearn.cluster import mean_shift
# get the image
url = 'https://i.stack.imgur.com/qKAk5.png'
res = requests.get(url)
# set the content as a file pointer object
fp = io.BytesIO(res.content)
# load the image to PIL
img = Image.open(fp)
# convert the image to gray-scale and load it to a numpy array
# need to transpose because the X and Y are swapped in PIL
# need [:,::-1] because pngs are indexed from the upper left
arr = np.array(img.convert('L')).T[:,::-1]
# get indices where there line is the pixel values is dark, ie <100
indices = np.argwhere(arr < 100)
这实际上是一个复杂的问题。您需要提取图像中所有黑色的点,然后找到一种方法将它们压缩为一组更有限的数据,粗略地跟踪路径。你知道吗
到目前为止,我们有索引,或(x,y)的位置,其中一个暗像素发生。但这些比你需要的多得多。为了减少点数,我们可以使用聚类技术来减少点数。
mean_shift
聚类技术在这里是合适的。它将一个内核分配给一组点,然后迭代地让附近的点慢慢聚集在一起。主要参数是内核的带宽,也就是拉的宽度。你知道吗所以对于200点的近似值,你可以使用3.0的带宽。你知道吗
您可以使用opencv进行以下操作:
相关问题 更多 >
编程相关推荐