def test_build_laplacian_pyramid_rgb(): rows, cols, dim = image.shape with expected_warnings(['The default multichannel']): pyramid = pyramids.pyramid_laplacian(image, downscale=2) for layer, out in enumerate(pyramid): layer_shape = (rows / 2**layer, cols / 2**layer, dim) assert_array_equal(out.shape, layer_shape)
def test_build_laplacian_pyramid_rgb(): rows, cols, dim = image.shape pyramid = pyramids.pyramid_laplacian(image, downscale=2, multichannel=True) for layer, out in enumerate(pyramid): layer_shape = (rows / 2 ** layer, cols / 2 ** layer, dim) assert_array_equal(out.shape, layer_shape)
def test_build_laplacian_pyramid_rgb_deprecated_multichannel(): rows, cols, dim = image.shape with expected_warnings(["`multichannel` is a deprecated argument"]): pyramid = pyramids.pyramid_laplacian(image, downscale=2, multichannel=True) for layer, out in enumerate(pyramid): layer_shape = (rows / 2 ** layer, cols / 2 ** layer, dim) assert_array_equal(out.shape, layer_shape) # repeat prior test, but check for positional multichannel warning with expected_warnings(["Providing the `multichannel` argument"]): pyramid = pyramids.pyramid_laplacian(image, -1, 2, None, 1, 'reflect', 0, True) for layer, out in enumerate(pyramid): layer_shape = (rows / 2 ** layer, cols / 2 ** layer, dim) assert_array_equal(out.shape, layer_shape)
def test_build_laplacian_pyramid_rgb(): rows, cols, dim = image.shape with expected_warnings(['The default multichannel']): pyramid = pyramids.pyramid_laplacian(image, downscale=2) for layer, out in enumerate(pyramid): layer_shape = (rows / 2 ** layer, cols / 2 ** layer, dim) assert_array_equal(out.shape, layer_shape)
def test_build_laplacian_pyramid_nd(): for ndim in [1, 2, 3, 4]: img = np.random.randn(*(16, )*ndim) original_shape = np.asarray(img.shape) pyramid = pyramids.pyramid_laplacian(img, downscale=2, multichannel=False) for layer, out in enumerate(pyramid): print(out.shape) layer_shape = original_shape / 2 ** layer assert_array_equal(out.shape, layer_shape)
def test_build_laplacian_pyramid_rgb(channel_axis): image = data.astronaut() rows, cols, dim = image.shape image = np.moveaxis(image, source=-1, destination=channel_axis) pyramid = pyramids.pyramid_laplacian(image, downscale=2, channel_axis=channel_axis) for layer, out in enumerate(pyramid): layer_shape = [rows / 2**layer, cols / 2**layer] layer_shape.insert(channel_axis % image.ndim, dim) assert out.shape == tuple(layer_shape)
def test_laplacian_pyramid_max_layers(): for downscale in [2, 3, 5, 7]: img = np.random.randn(32, 8) pyramid = pyramids.pyramid_laplacian(img, downscale=downscale, multichannel=False) max_layer = int(np.ceil(math.log(np.max(img.shape), downscale))) for layer, out in enumerate(pyramid): if layer < max_layer: # should not reach all axes as size 1 prior to final level assert_(np.max(out.shape) > 1) # total number of images is max_layer + 1 assert_equal(max_layer, layer) # final layer should be size 1 on all axes assert_array_equal((out.shape), (1, 1))
def multiscale_saliency(image, method, min_image_area=10000): """ Runs any saliency method as a multiscale method. method is run for each image downsized until its area is lower than min_image_area. The final result is an image with the same size as the original. """ sals = None count = 0 for img in pyramid_laplacian(image, 1): print "calculating for shape = %s, %s" % img.shape[:2] s = method(image) s = resize(s, image.shape[:2], mode="nearest") if sals is not None: sals = sals + s else: sals = s count += 1 return sals / count
def multiscale_saliency(image, method, min_image_area=10000): ''' Runs any saliency method as a multiscale method. method is run for each image downsized until its area is lower than min_image_area. The final result is an image with the same size as the original. ''' sals = None count = 0 for img in pyramid_laplacian(image, 1): print 'calculating for shape = %s, %s' % img.shape[:2] s = method(image) s = resize(s, image.shape[:2], mode='nearest') if sals is not None: sals = sals + s else: sals = s count += 1 return sals / count
def test_laplacian_pyramid_max_layers(channel_axis): for downscale in [2, 3, 5, 7]: if channel_axis is None: shape = (32, 8) shape_without_channels = shape else: shape_without_channels = (32, 8) ndim = len(shape_without_channels) + 1 n_channels = 5 shape = list(shape_without_channels) shape.insert(channel_axis % ndim, n_channels) shape = tuple(shape) img = np.ones(shape) pyramid = pyramids.pyramid_laplacian(img, downscale=downscale, channel_axis=channel_axis) max_layer = math.ceil(math.log(max(shape_without_channels), downscale)) for layer, out in enumerate(pyramid): if channel_axis is None: out_shape_without_channels = out.shape else: assert out.shape[channel_axis] == n_channels out_shape_without_channels = list(out.shape) out_shape_without_channels.pop(channel_axis) out_shape_without_channels = tuple(out_shape_without_channels) if layer < max_layer: # should not reach all axes as size 1 prior to final level assert max(out_shape_without_channels) > 1 # total number of images is max_layer + 1 assert_equal(max_layer, layer) # final layer should be size 1 on all axes assert out_shape_without_channels == (1, 1)
def test_build_laplacian_pyramid_gray(): rows, cols = image_gray.shape pyramid = pyramids.pyramid_laplacian(image_gray, downscale=2) for layer, out in enumerate(pyramid): layer_shape = (rows / 2**layer, cols / 2**layer) assert_array_equal(out.shape, layer_shape)
def test_build_laplacian_pyramid_gray(): rows, cols = image_gray.shape pyramid = pyramids.pyramid_laplacian(image_gray, downscale=2) for layer, out in enumerate(pyramid): layer_shape = (rows / 2 ** layer, cols / 2 ** layer) assert_array_equal(out.shape, layer_shape)