Skip to content

Commit 5add07c

Browse files
committed
Added expand_binary_mask_for_window.
1 parent d33bebc commit 5add07c

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

spectral/algorithms/spatial.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,3 +783,33 @@ def map_classes(class_image, class_id_map, allow_unmapped=False):
783783
mapped[class_image == i] = j
784784
return mapped
785785

786+
def expand_binary_mask_for_window(mask, height, width):
787+
'''Returns a new mask including window around each pixel in source mask.
788+
789+
Arguments:
790+
791+
`mask` (2D ndarray):
792+
793+
An ndarray whose non-zero elements define a mask.
794+
795+
`height` (int):
796+
797+
Height of the window.
798+
799+
`width` (int):
800+
801+
Width of the window
802+
803+
Returns a new mask of ones and zeros with same shape as `mask`. For each
804+
non-zero element in mask, the returned mask will contain a value of one
805+
for all pixels in the `height`x`width` window about the pixel and zeros
806+
elsewhere.
807+
'''
808+
from spectral.algorithms.algorithms import iterator_ij
809+
m = np.zeros_like(mask)
810+
(mask_height, mask_width) = mask.shape
811+
for (i, j) in iterator_ij(mask):
812+
(r0, r1, c0, c1) = get_window_bounds_clipped(mask_height, mask_width,
813+
height, width, i, j)
814+
m[r0:r1, c0:c1] = 1
815+
return m

0 commit comments

Comments
 (0)