def expand_binary_mask_for_window(mask, height, width): '''Returns a new mask including window around each pixel in source mask. Arguments: `mask` (2D ndarray): An ndarray whose non-zero elements define a mask. `height` (int): Height of the window. `width` (int): Width of the window Returns a new mask of ones and zeros with same shape as `mask`. For each non-zero element in mask, the returned mask will contain a value of one for all pixels in the `height`x`width` window about the pixel and zeros elsewhere. ''' from spectral.algorithms.algorithms import iterator_ij m = np.zeros_like(mask) (mask_height, mask_width) = mask.shape for (i, j) in iterator_ij(mask): (r0, r1, c0, c1) = get_window_bounds_clipped(mask_height, mask_width, height, width, i, j) m[r0:r1, c0:c1] = 1 return m
def test_iterator_ij_nonzero(self): '''Iteration over all non-background pixels.''' data = self.image.load() classes = self.gt.ravel() pixels = data.reshape((-1, data.shape[-1])) sum = np.sum(pixels[classes > 0], 0) itsum = np.sum(np.array([data[ij] for ij in iterator_ij(self.gt)]), 0) assert_allclose(sum, itsum)
def test_iterator_ij_index(self): '''Iteration over single ground truth index''' cls = 5 data = self.image.load() classes = self.gt.ravel() pixels = data.reshape((-1, data.shape[-1])) sum = np.sum(pixels[classes == cls], 0) itsum = np.sum( np.array([data[ij] for ij in iterator_ij(self.gt, cls)]), 0) assert_allclose(sum, itsum)