【问题标题】:How do I get the coordinates of the vectors that represent a contour shape which I have stores as a 2D numpy array of pixels?如何获取代表轮廓形状的向量的坐标,该轮廓形状存储为二维 numpy 像素数组?
【发布时间】:2022-01-20 05:27:53
【问题描述】:

我有一个 1000x1000 的 2D numpy 数组,可以将其视为图像的像素。没有形状的单元格为 0,形状为某个值,表示强度的值。可以这样绘制:

plt.matshow(data, origin='lower')

当只考虑超过某个阈值的数据时,可以将数据视为形状,如下所示:

fig, ax = plt.subplots()

cluster_contour_threshold = 60
y,x = np.argwhere(data > cluster_contour_threshold).T

ax.scatter(x, y)
ax.set_xlim((0, 1000))
ax.set_ylim((0, 1000))

我想要的是获取代表此形状轮廓的坐标列表。像这样的:

[
  [x0,y0],
  [x1,y1],
  [x2,y2]
]

到目前为止,我最好的尝试是使用 canny,但这并不完全正确:

from skimage import feature
import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

c = feature.canny(data)
y,x = np.argwhere(c).T


ax.scatter(x, y)
ax.set_xlim((0, 1000))
ax.set_ylim((0, 1000))

【问题讨论】:

  • 我想只是points = np.argwhere(c)?
  • 这会给你所有有数据的点,而不是轮廓。

标签: python arrays numpy


【解决方案1】:

我用 skimage 解决了这个问题。

from skimage import measure

contours = measure.find_contours(data, 0.8)

# Display the image and plot all contours found
fig, ax = plt.subplots()
ax.imshow(data, cmap=plt.cm.gray)

for contour in contours:
    ax.plot(contour[:, 1], contour[:, 0], linewidth=2)

ax.axis('image')
ax.set_xticks([])
ax.set_yticks([])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-09
    • 2021-03-17
    • 1970-01-01
    • 1970-01-01
    • 2014-11-22
    • 2020-02-08
    相关资源
    最近更新 更多