當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python skimage.draw.rectangle用法及代碼示例

用法:

skimage.draw.rectangle(start, end=None, extent=None, shape=None)

生成矩形內像素的坐標。

參數

start元組

矩形的原點,例如 ([plane,] row, column)

end元組

矩形的終點([plane,] row, column).對於二維矩陣,矩形定義的切片為[start:(end+1)].任何一個結尾或者程度必須指定。

extent元組

繪製矩形的範圍(大小)。例如:,([num_planes,] num_rows, num_cols).任何一個結尾或者程度必須指定。負範圍是有效的,並且會產生一個沿相反方向的矩形。如果範圍為負,則開始點不包括在內。

shape元組,可選

用於確定輸出坐標的最大邊界的圖像形狀。這對於裁剪超出圖像大小的矩形很有用。默認情況下,不進行剪輯。

返回

coordsint 形狀數組(Ndim,Npoints)

矩形中所有像素的坐標。

注意

通過將 start 和 end 或 extent 作為長度為 N 的元組傳遞,此函數可以應用於 N 維圖像。

例子

>>> import numpy as np
>>> from skimage.draw import rectangle
>>> img = np.zeros((5, 5), dtype=np.uint8)
>>> start = (1, 1)
>>> extent = (3, 3)
>>> rr, cc = rectangle(start, extent=extent, shape=img.shape)
>>> img[rr, cc] = 1
>>> img
array([[0, 0, 0, 0, 0],
       [0, 1, 1, 1, 0],
       [0, 1, 1, 1, 0],
       [0, 1, 1, 1, 0],
       [0, 0, 0, 0, 0]], dtype=uint8)
>>> img = np.zeros((5, 5), dtype=np.uint8)
>>> start = (0, 1)
>>> end = (3, 3)
>>> rr, cc = rectangle(start, end=end, shape=img.shape)
>>> img[rr, cc] = 1
>>> img
array([[0, 1, 1, 1, 0],
       [0, 1, 1, 1, 0],
       [0, 1, 1, 1, 0],
       [0, 1, 1, 1, 0],
       [0, 0, 0, 0, 0]], dtype=uint8)
>>> import numpy as np
>>> from skimage.draw import rectangle
>>> img = np.zeros((6, 6), dtype=np.uint8)
>>> start = (3, 3)
>>>
>>> rr, cc = rectangle(start, extent=(2, 2))
>>> img[rr, cc] = 1
>>> rr, cc = rectangle(start, extent=(-2, 2))
>>> img[rr, cc] = 2
>>> rr, cc = rectangle(start, extent=(-2, -2))
>>> img[rr, cc] = 3
>>> rr, cc = rectangle(start, extent=(2, -2))
>>> img[rr, cc] = 4
>>> print(img)
[[0 0 0 0 0 0]
 [0 3 3 2 2 0]
 [0 3 3 2 2 0]
 [0 4 4 1 1 0]
 [0 4 4 1 1 0]
 [0 0 0 0 0 0]]

相關用法


注:本文由純淨天空篩選整理自scikit-image.org大神的英文原創作品 skimage.draw.rectangle。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。