grid_to_graph#

sklearn.feature_extraction.image.grid_to_graph(n_x, n_y, n_z=1, *, mask=None, return_as=<class 'scipy.sparse._coo.coo_matrix'>, dtype=<class 'int'>)[源代码]#

像素到像素连接的图表。

如果2个体素连接,则存在边。

参数:
n_xint

x轴上的尺寸。

n_yint

y轴上的尺寸。

n_zint,默认=1

z轴上的尺寸。

masknd形状数组(n_x,n_y,n_z),dype =bool,默认=无

图像的可选面膜,仅考虑部分像素。

return_asNP. ndray或稀疏矩阵类, 默认=sparse.coo_matrix

用于构建返回的邻近矩阵的类。

dtypedype,默认=int

返回的稀疏矩阵的数据。默认情况下,它是int。

返回:
graphNP. ndray或稀疏矩阵类

计算出的邻近矩阵。

示例

>>> import numpy as np
>>> from sklearn.feature_extraction.image import grid_to_graph
>>> shape_img = (4, 4, 1)
>>> mask = np.zeros(shape=shape_img, dtype=bool)
>>> mask[[1, 2], [1, 2], :] = True
>>> graph = grid_to_graph(*shape_img, mask=mask)
>>> print(graph)
<COOrdinate sparse matrix of dtype 'int64'
  with 2 stored elements and shape (2, 2)>
  Coords    Values
  (0, 0)    1
  (1, 1)    1