繁体   English   中英

如何获取代表轮廓形状的向量的坐标,该轮廓形状存储为 2D numpy 像素数组?

[英]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?

我有一个 1000x1000 2D numpy 阵列,可以被认为是图像的像素。 没有形状的单元格为 0,形状为某个值,表示强度的值。 它可以像这样绘制:

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

numpy 数组的 Matshow 图

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

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))

Matplotlib 散点图表示 numpy 数组

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

[
  [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))

精明的

这个答案中得到一些启发,我能够用精明的方式解决这个问题,尽管方式很慢:

from skimage import feature
from shapely.geometry import Polygon

c = feature.canny(data > cluster_contour_threshold)
ans = []
for y in range(0, c.shape[0]):
    for x in range(0, c.shape[1]):
        if c[y, x] != 0:
            ans = ans + [[x, y]]
Polygon(ans)

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM