use*_*442 6 python numpy matplotlib scipy
我有一个小问题,当我拍摄图像轮廓时,我得到这个数字:
正如你所看到的,我可以提取轮廓,但是一旦我提取了路径,它就会留下这些奇怪的割线,它会穿过图像,因为曲线上有2个不连续的区域.我想知道是否有一种方法可以断开不连续的线路,或者是我的路径提取代码是错误的
import matplotlib.pyplot as plt
import numpy as np
def contourPath(img, width, height):
x = np.arange(0,width)
y = np.arange(height,0,-1)
X, Y = np.meshgrid(x,y)
plot = plt.contour(X,Y,img, [0])
pathList = plot.collections[0].get_paths()
x, y = [], []
for i in range(0, len(pathList)):
iterPath = pathList[i].iter_segments()
for point in iterPath:
pt = np.rint(point[0])
x.append(pt[0])
y.append(pt[1])
X = np.hstack(x)
Y = np.hstack(y)
return np.dstack((X,Y))[0]
Run Code Online (Sandbox Code Playgroud)
感谢您的时间
for user545424我猜这里.Matplotlib轮廓功能正常工作,因为图像上有两个不连续的点,导致这个小事件发生.
我知道那些割线是由scypi引起的,但它引发了关于库如何与轮廓点相互作用的另一个问题
哦,我相信可以通过找到路径并插入它来掩盖问题.但是,我喜欢避免重新路径,因为旅行Salemans问题在我的电脑上并不好.
你有什么建议吗?
我想我正在回答我自己的问题。这些奇怪的割线是由 scipy 连接列表末尾引起的。我相信默认行为是 scipy 将连接 2 个连续点,无论位置如何。顶点工作正常,因为它严格遵循了该图像的轮廓并且包围了曲线。正割线之所以连接到上半圆的中间。那是最后一条路径结束的位置。
我想转换为极坐标并重新寻找路径很简单,但并不完全是最佳的,但是哦,好吧。
我想知道其他人是否有更好的解决方案
def cart2polar(x,y, origin=None, size=np.array([200,200])):
ny, nx= size[0], size[1]
print size.shape
if origin is None:
origin_x, origin_y = nx//2, ny//2
else:
origin_x, origin_y = origin[0], origin[1]
x -= origin_x
y -= origin_y
r = np.sqrt(x**2 + y**2)
theta = np.arctan2(y, x)
return r, theta
def main():
index = np.argsort(theta)
r, theta = r[index[:]], theta[index[:]]
f = interpolate.interp1d(theta,r)
theta = np.linspace(round(theta.min()+.00005,),theta.max(), 200)
r = f(theta)
x,y = polar2cart(r, theta)
def polar2cart(r, theta, origin=None, size=np.array([200,200]) ):
ny, nx= size[0], size[1]
if origin is None:
origin_x, origin_y = nx//2, ny//2
else:
origin_x, origin_y = origin[0], origin[1]
x += origin_x
y += origin_y
return x, y
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
433 次 |
最近记录: |