6

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

如您所见,我可以提取轮廓,但是一旦我提取路径,它就会留下这些奇怪的割线,这些割线会穿过图像,因为曲线上有 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]

感谢您的时间

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

我了解到这些割线是由 scypi 引起的,但它引发了另一个关于库如何与轮廓点交互的问题

哦,好吧,我相信可以通过找到路径并对其进行插值来掩盖问题。但是,我喜欢避免重新寻找路径,因为旅行推销员问题在我的计算机上并不好。

你有什么建议吗?

在此处输入图像描述

4

1 回答 1

3

我想我正在回答我自己的问题。那些奇怪的割线是由连接列表末端的 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
于 2012-06-23T20:29:42.440 回答