【问题标题】:Is there a way to get contour properties in OpenCV/skimage for floating point coordinates?有没有办法在 OpenCV/skimage 中获取浮点坐标的轮廓属性?
【发布时间】:2021-07-21 11:44:07
【问题描述】:

我在 Matplotlib 中创建了等高线图,我需要进一步分析它们是否是闭合曲线,然后查看细胞结构的面积、凸度、坚固度等。在 Matplotlib 中,它们的类型为 LineCollectionPath

在 OpenCV 中,我无法将 float 数组传递给 cv2.contourArea 或类似函数。另一方面,转换为uint8 坐标会丢失嵌套结构等重要数据。在这种情况下,我需要到达内部嵌套的凸轮廓。

是否有任何选项可以在 Python 中查找区域、凸包、边界矩形等信息?

我可以放大图片,但我担心它可能会意外歪斜图片。

例如:带有浮点和整数坐标的附加图像。

【问题讨论】:

    标签: python image opencv scikit-image


    【解决方案1】:

    我假设,您可以完全控制 Matplotlib 部分。因此,让我们尝试从那里获取图像,您可以轻松地将其用于 OpenCV 的进一步图像处理。

    我们从一些常见的contour 情节开始,如您的问题所示:

    您可以设置levels 参数以获取单个轮廓级别。这对于单独在多个级别上工作很有帮助。在下文中,我将重点关注levels=[1.75](最内层的绿色椭圆)。之后,您可以简单地遍历所有需要的级别,然后执行您的分析。

    对于我们的自定义等值线图,我们将使用xlimylim 设置一个固定的x, y 域,例如[-3, 3] x [-2, 2]。因此,我们已经知道实际画布的尺寸。我们使用axis('off') 去除坐标轴,使用tight_layout(pad=0) 去除画布周围的边距。剩下的是全尺寸的普通画布(图形大小调整为(10, 5),颜色自动调整为级别数):

    现在,我们将画布保存到某个 NumPy 数组中,参见。 this Q&A。从那里,我们可以执行任何 OpenCV 操作。为了找到这个级别轮廓的组合区域,我们可能会对灰度图像进行阈值化,找到所有轮廓,并使用cv2.contourArea 计算它们的区域。我们对这些区域求和,并以像素为单位获得整个区域。从已知的画布尺寸,我们知道以“单位”为单位的整个画布区域,从图像尺寸,我们知道以像素为单位的整个画布区域。因此,我们只需要将整个轮廓区域(以像素为单位)除以整个画布区域(以像素为单位),然后乘以整个画布区域(以“单位”为单位)。

    这就是整个代码:

    import cv2
    import matplotlib.pyplot as plt
    import numpy as np
    
    # Generate some data for some contour plot
    delta = 0.025
    x = np.arange(-3.0, 3.0, delta)
    y = np.arange(-2.0, 2.0, delta)
    X, Y = np.meshgrid(x, y)
    Z1 = np.exp(-(X + 1.5)**2 - Y**2)
    Z2 = np.exp(-(X - 1.5)**2 - Y**2)
    Z = (Z1 + Z2) * 2
    
    # Custom contour plot
    x_min, x_max = -3, 3
    y_min, y_max = -2, 2
    fig = plt.figure(2, figsize=(10, 5))    # Set large figure size
    plt.contour(X, Y, Z, levels=[1.75])     # Set single levels if needed
    plt.xlim([x_min, x_max])                # Explicitly set x limits
    plt.ylim([y_min, y_max])                # Explicitly set y limits
    plt.axis('off')                         # No axes shown at all
    plt.tight_layout(pad=0)                 # No margins at all
    
    # Get figure's canvas as NumPy array, cf. https://stackoverflow.com/a/7821917/11089932
    fig.canvas.draw()
    img = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
    img = img.reshape(fig.canvas.get_width_height()[::-1] + (3,))
    
    # Grayscale, and threshold image
    mask = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    mask = cv2.threshold(mask, 0, 255, cv2.THRESH_OTSU + cv2.THRESH_BINARY_INV)[1]
    
    # Find contours, calculate areas (pixels), sum to get whole area (pixels) for certain level
    cnts = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
    cnts = cnts[0] if len(cnts) == 2 else cnts[1]
    area = np.sum(np.array([cv2.contourArea(cnt) for cnt in cnts]))
    
    # Whole area (coordinates) from canvas area (pixels), and x_min, x_max, etc.
    area = area / np.prod(mask.shape[:2]) * (x_max - x_min) * (y_max - y_min)
    print('Area:', area)
    

    输出区域似乎合理:

    Area: 0.861408
    

    现在,您可以使用 OpenCV 进行任何您喜欢的图像处理。永远记得将任何以像素为单位的结果转换为以“单位”为单位的某些结果。

    ----------------------------------------
    System information
    ----------------------------------------
    Platform:      Windows-10-10.0.16299-SP0
    Python:        3.9.1
    PyCharm:       2021.1.1
    Matplotlib:    3.4.1
    NumPy:         1.20.2
    OpenCV:        4.5.1
    

    【讨论】:

    • 很好,有帮助!设置 figsize 可以让 matplotlib 完成这项工作。
    猜你喜欢
    • 2015-07-24
    • 1970-01-01
    • 1970-01-01
    • 2021-06-16
    • 2023-03-14
    • 2018-10-10
    • 1970-01-01
    • 2017-03-11
    • 2020-04-10
    相关资源
    最近更新 更多