二维 numpy 数组到 skimage
2D numpy Array to skimage
我想知道如何转换 [x, y](像素位置)形式的 numpy 数组:
[[ 93 58]
[ 94 58]
[ 95 58]
...,
[ 99 142]
[100 142]
[101 142]]
使用 skimage 的表格。为此,我认为我需要将数组标准化为适合数据集的 canvas 大小宽度 x 高度(比如 500 x 500)。
最终我想对这个阵列进行edge/contour检测。
http://scikit-image.org/docs/dev/auto_examples/edges/plot_contours.html
我怎样才能规范化这些数据,使其成为 skimage 需要的形式?
找到了一个不错的解决方案。使用 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
无需调用任何外部函数,这是一种将一系列 x,y 数据点 transform/convert 转换为适用于 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))
我想知道如何转换 [x, y](像素位置)形式的 numpy 数组:
[[ 93 58]
[ 94 58]
[ 95 58]
...,
[ 99 142]
[100 142]
[101 142]]
使用 skimage 的表格。为此,我认为我需要将数组标准化为适合数据集的 canvas 大小宽度 x 高度(比如 500 x 500)。
最终我想对这个阵列进行edge/contour检测。
http://scikit-image.org/docs/dev/auto_examples/edges/plot_contours.html
我怎样才能规范化这些数据,使其成为 skimage 需要的形式?
找到了一个不错的解决方案。使用 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
无需调用任何外部函数,这是一种将一系列 x,y 数据点 transform/convert 转换为适用于 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))