【问题标题】:calling contour without plotting it, python, pylab inline调用轮廓而不绘制它,python,pylab inline
【发布时间】:2019-03-29 23:21:11
【问题描述】:

对于我使用轮廓的算法,但我只对它的路径集合感兴趣。因为我打过电话

pylab inline

从一开始,现在在没有内联的情况下重写代码太痛苦了(许多函数必须更仔细地声明,比如 np.something() 而不是 something() 等...),我是想知道是否有一种方法可以在不绘制等高线图的情况下调用等高线? 像

contour(image_matrix, 'No Show')? 

问候

【问题讨论】:

    标签: python matplotlib contour


    【解决方案1】:

    以下是修改后的代码,用于在声明的网格中获取单位圆上的点。它比 plt.contour 更快地给出轮廓点,并且不绘制点。

    matplotlib._cntr 是 plt.contour 调用的核心函数,它试图获取轮廓点。

    import matplotlib._cntr as cntr
    import numpy as np
    
    # Test data.
    x = np.linspace(-1, 1, 20)
    y = np.linspace(-1, 1, 20)
    
    x, y = np.meshgrid(x, y)
    z = x**2 + y**2 - 1            # Function to get points from
    # the above function can be replaced with any curve equation
    # conics like ellipse or hyperbola: ((x**2)/a)+((y**2)/b)-1,etc. 
    
    
    level = 0
    c = cntr.Cntr(x, y, z)
    nlist = c.trace(level, level, 0)
    segs = nlist[:len(nlist)//2]
    print segs[0][0]    # x,y coords of contour points.
    

    对不起,我的解释很糟糕,我对 python 的经验不够。详细解释可以参考下面的链接。

    讨论链接: http://matplotlib.1069221.n5.nabble.com/pyplot-Extract-contourset-without-plotting-td15868.html

    在讨论结束时,Ian Thomas 先生附上了一个代码“contour_test.py”,它可能对您有所帮助。

    示例代码链接: http://matplotlib.1069221.n5.nabble.com/attachment/15872/0/contour_test.py

    【讨论】:

    【解决方案2】:

    没有特定的选项来抑制contour 的绘图(据我所知)。以下question 似乎提供了您使用matplotlib._cntr 所需的内容。

    对于您的情况,通过切换回不同的 gui 来实现 pylab inline 中图形的抑制可能更简单,例如使用%pylab qt,然后调用cs = contour(image_matrix)。如果没有显式调用plt.show(),这可能不会显示任何内容,您可以使用cs 获取所需的轮廓信息。

    您也可以使用matplotlib.interactive(False) 之类的东西来隐藏数字。

    【讨论】:

      【解决方案3】:

      由于不再支持matplotlib._cntr,您可以使用skimage 中的find_contour() 函数。这是从documentation 的分析函数中提取轮廓级别 0.8 的简单代码。

      import numpy as np
      from skimage import measure
      
      # Construct some test data
      x, y = np.ogrid[-np.pi:np.pi:100j, -np.pi:np.pi:100j]
      r = np.sin(np.exp((np.sin(x)**3 + np.cos(y)**2)))
      
      # Find contours at a constant value of 0.8
      contours = measure.find_contours(r, 0.8)
      

      这将为您提供沿轮廓的(行、列)坐标函数中的轮廓,而不是x 和y 的值。要转换为 x 和 y 值,您可以使用来自 scipy 的 interp1d 进行插值:

      from scipy.interpolate import interp1d
      fx = interp1d(np.arange(0,x.shape[0]), x.flatten())
      fy = interp1d(np.arange(0,y.shape[1]), y.flatten())
      for contour in contours:
          contour[:,0] = fx(contour[:,0])
          contour[:,1] = fy(contour[:,1])
      

      查看结果和验证的简单代码:

      import matplotlib.pyplot as plt
      fig = plt.figure()
      ax = fig.add_subplot(111)
      for contour in contours:
          ax.plot(contour[:,0], contour[:,1])
      fig.show()
      

      Figure of the extracted contours

      【讨论】:

      • 如果您尝试在不规则(不可分割的)网格中执行此操作,这将变得异常困难且计算量很大。有什么方法可以应用find_contour 而无需通过插值处理转换?此外,find_contour 和 matplotlib contour 的结果似乎略有不同,在我的情况下,matplotlib contour 似乎非常强大。 matplotlib 停止使用 cntr 功能真是令人沮丧……
      • 你有例子吗?
      • 你也可以看看legacy contour repo。
      • 另一种选择可能是定义一个“一次性”图形,在其上使用contour,然后定义另一个图形,在该图形上定义实际的显示图。
      猜你喜欢
      • 2023-03-20
      • 2018-07-09
      • 2019-01-25
      • 1970-01-01
      • 1970-01-01
      • 2021-09-07
      • 1970-01-01
      • 2021-04-14
      • 1970-01-01
      相关资源
      最近更新 更多