Python 如何连接matplotlab、scipy等中的不连续曲线

Python 如何连接matplotlab、scipy等中的不连续曲线,python,numpy,matplotlib,scipy,Python,Numpy,Matplotlib,Scipy,我有一个小问题,当我拍摄一幅图像的轮廓时,我得到了这个图: 正如你所见,我可以提取轮廓,但一旦我提取路径,它将留下这些奇怪的割线,它们将穿过图像,因为曲线上有两个不连续的区域。我想知道是否有办法断开不连续的行,还是我的路径提取代码错了 import matplotlib.pyplot as plt import numpy as np def contourPath(img, width, height): x = np.arange(0,width) y

我有一个小问题,当我拍摄一幅图像的轮廓时,我得到了这个图:

正如你所见,我可以提取轮廓,但一旦我提取路径,它将留下这些奇怪的割线,它们将穿过图像,因为曲线上有两个不连续的区域。我想知道是否有办法断开不连续的行,还是我的路径提取代码错了

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]
谢谢你抽出时间

对于用户545424 我想在这里。Matplotlib轮廓函数工作正常,因为图像上有两个不连续的点导致了这一小事件的发生

我了解到这些割线是由scypi引起的,但它提出了另一个问题,即库如何与等高线点交互

哦,好吧,我相信通过找到路径并对其插值来掩盖问题是可能的。但是,我喜欢避免重复路径,因为旅行推销员的问题在我的电脑上并不好

你有什么建议吗


我想我是在回答我自己的问题。这些奇怪的割线是由于将列表的末端连接在一起造成的。默认的行为,我相信是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

从您的编辑中,不清楚您是否仍然存在问题。顺便问一下,这是视网膜吗?我知道,但是重新填充路径会破坏路径对象的用途。哦,好吧,我想我可能已经发布了。转换为polar,用户argsort()获取索引和插值图像更简单。-heltonbiker它是视网膜的背面