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'>)[source]#

像素到像素连接的图。

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

用户指南中了解更多信息。

参数:
n_xint

x 轴上的维度。

n_yint

y 轴上的维度。

n_zint, default=1

z 轴上的维度。

maskndarray of shape (n_x, n_y, n_z), dtype=bool, default=None

图像的可选掩码,仅考虑部分像素。

return_asnp.ndarray or a sparse matrix class, default=sparse.coo_matrix

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

dtypedtype, default=int

返回的稀疏矩阵的数据类型。默认值为 int。

返回:
graphnp.ndarray or a sparse matrix class

计算出的邻接矩阵。

示例

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