網格到圖#

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 軸的維度。

mask形狀為 (n_x, n_y, n_z) 的 ndarray,dtype=bool,預設值=None

影像的可選遮罩,僅考慮部分像素。

return_asnp.ndarray 或稀疏矩陣類別,預設值=sparse.coo_matrix

用於建構傳回的鄰接矩陣的類別。

dtypedtype,預設值=int

傳回的稀疏矩陣的資料。預設為 int。

傳回:
graphnp.ndarray 或稀疏矩陣類別

計算的鄰接矩陣。

範例

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