【问题标题】:2D numpy Array to skimage2D numpy 数组到 skimage
【发布时间】:2017-08-15 13:08:37
【问题描述】:

我想知道如何以 [x, y](像素位置)的形式转换一个 numpy 数组:

[[ 93  58]
 [ 94  58]
 [ 95  58]
 ..., 
 [ 99 142]
 [100 142]
 [101 142]]

到一个适用于 skimage 的表单。为此,我认为我需要将数组标准化为适合数据集的某个画布大小宽度 x 高度(例如 500 x 500)。

最终我想在这个阵列上执行边缘/轮廓检测。

http://scikit-image.org/docs/dev/auto_examples/edges/plot_contours.html

如何规范化这些数据,使其符合 skimage 要求的形式?

http://scikit-image.org/docs/dev/user_guide/data_types.html

【问题讨论】:

  • .astype(one_of_those_dtypes)?
  • 公平的答案,我想我想知道如何将其标准化为相同的宽度、高度,以便我可以在 skimage 中应用函数(如 find_contours)。 .astype 转换 np 数组的类型,但不会使其成为有效的 skimage 格式。
  • 那个数据是什么意思?是那些像素坐标,例如(行,列)?如果是这样,每个坐标处的像素值是多少?也许图像是 0s 和 1s 的布尔图像,数据给出了 1s 的坐标?请解释一下。
  • 应该添加更多细节,它的像素坐标 [x,y]。其中 x 是位置 x,y 是位置 y。需要对这些数据进行规范化,以便将其视为 skimage 说 (500x500),在这种情况下,我们将在点 [93, 58], [94, 58]... 等处有像素。
  • 正在考虑使用 matplotlib canvas.tostring_rgb() 并使用它来生成图像的 numpy 数组(标准化为适合数据集的某个宽度高度),但尚未在实践中使用

标签: python numpy matplotlib scikit-image


【解决方案1】:

无需调用任何外部函数,这是一种将一系列 x,y 数据点转换/转换为适用于 skimage 的二维数组的简单方法:

def xy_to_binary2d(ts):
    '''Convert a list of (x,y) tuples to binary 2d format acceptable to skimage.'''
    if ts.dtype != 'int32': 
        print('Only integer input is supported.')

    xmax,ymax = ts.max(axis=0)
    __,ymin = ts.min(axis=0)

    if ymin < 0:
        print('Negative integers are not supported.')

    r = np.zeros((ymax+2,xmax+2))
    for each in ts:r.itemset(each[1],each[0])

    return r

让我们测试一下:

ts =np.array([[1,1],[2,1],[3,2],[4,3],[5,5],[6,8],[7,13]])
xy_to_binary2d(ts)

输出:

array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
   [ 0.,  1.,  1.,  0.,  0.,  0.,  0.,  0.,  0.],
   [ 0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.],
   [ 0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.],
   [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
   [ 0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.],
   [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
   [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
   [ 0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.],
   [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
   [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
   [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
   [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
   [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.],
   [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]])

还有,一张漂亮的照片…… plt.imshow(xy_to_binary2d(ts))

【讨论】:

  • 使用“for each in ts:r.itemset(each[1],each[0])”快 10%
【解决方案2】:

找到了一个不错的解决方案。使用 matplotlib 生成像素位置 [x,y] numpy 的 rgb 表示 > 使用 skimage color.rgb2gray 将其转换为 skimage 格式。

fig = Figure()
canvas = FigureCanvas(fig)
ax = fig.add_subplot(111)

ax.axis('off')
fig.patch.set_facecolor('white')
print(data.shape)
ax.fill(data[:, [0]], data[:, [1]],'black')

# Set canvas size
mi = min(x_min, y_min)
ma = max(x_max, y_max)
ax.set_xlim(mi, ma)
ax.set_ylim(mi, ma)
fig.canvas.draw()
buf = fig.canvas.tostring_rgb()
ncols, nrows = fig.canvas.get_width_height()
new_data = np.fromstring(buf, dtype=np.uint8).reshape(nrows, ncols, 3)
new_data.reshape(nrows, ncols, 3)

image = color.rgb2gray(new_data)

return image

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-09
    • 1970-01-01
    • 2013-11-06
    • 2012-12-31
    • 2019-09-23
    • 2020-11-05
    • 2021-10-12
    相关资源
    最近更新 更多