learnergy/visual/image.py
def _rasterize(x, img_shape, tile_shape, tile_spacing=(0, 0), scale=True, output=True):
    """Rasterizes and prepares an image to be outputted as a mosaic.

    Args:
        x (array): An input array to be rasterized.
        img_shape (tuple): A tuple for the image shape.
        tile_shape (tuple): A tuple holding the shape of each tile.
        tile_spacing (tuple): A tuple containing the spacing between tiles.
        scale (bool): If output array should be scaled between 0 and 1.
        output (bool): If output values should be returned as pixels or not.

    Returns:
        An output array containing the rasterized version of input array.

    """

    # Asserts if tuple lengths are equal to 2
    assert len(img_shape) == 2
    assert len(tile_shape) == 2
    assert len(tile_spacing) == 2

    # Creates an output shape
    out_shape = [(ishp + tsp) * tshp - tsp for ishp, tshp,
                 tsp in zip(img_shape, tile_shape, tile_spacing)]

    # Asserts if input array is a tuple
    if isinstance(x, tuple):
        # Checks if its length is equal to 4
        assert len(x) == 4

        # If output boolean is true
        if output:
            # Output values as pixels
            out_array = np.zeros(
                (out_shape[0], out_shape[1], 4), dtype='uint8')

            # Apply the default channels
            channel_defaults = [0, 0, 0, 255]

        # If output boolean is false
        else:
            # Output values as its input type
            out_array = np.zeros(
                (out_shape[0], out_shape[1], 4), dtype=x[0].dtype)

            # Apply the default channels
            channel_defaults = [0., 0., 0., 1.]

        # For every possible item in tuple
        for i in range(4):
            # If there is no channel
            if x[i] is None:
                # Fill it with zeros of the correct dtype
                out_array[:, :, i] = np.zeros(
                    out_shape, dtype=out_array.dtype) + channel_defaults[i]

            # If there is a channel
            else:
                # Use a recurrent call to compute the channel and store it
                out_array[:, :, i] = _rasterize(
                    x[i], img_shape, tile_shape, tile_spacing, scale, output)

        return out_array

    # Gathers the image shape and its tile spacing
    H, W = img_shape
    Hs, Ws = tile_spacing

    # Checks the current input dtype
    dt = x.dtype

    # If output boolean is true
    if output:
        # Output type should be an unsigned integer
        dt = 'uint8'

    # Creates a zeros array based on output shape
    out_array = np.zeros(out_shape, dtype=dt)

    # For every row of tiles
    for tile_row in range(tile_shape[0]):
        # For every column of tiles
        for tile_col in range(tile_shape[1]):
            # Checks if belongs to an specific row
            if tile_row * tile_shape[1] + tile_col < x.shape[0]:
                # Replace its value
                x1 = x[tile_row * tile_shape[1] + tile_col]

                # If scale boolean is true
                if scale:
                    # We should scale values to be between 0 and 1
                    img = scl.unitary_scale(x1.reshape(img_shape))

                # If not
                else:
                    # We just reshape the image to the input shape
                    img = x1.reshape(img_shape)

                # Add the slice to the corresponding position in the output array
                c = 1

                # If output boolean is true
                if output:
                    # The slice should be a maximum pixel value
                    c = 255

                # Creates the output array
                out_array[
                    tile_row * (H + Hs): tile_row * (H + Hs) + H,
                    tile_col * (W + Ws): tile_col * (W + Ws) + W
                ] = img * c

    return out_array
Rasterizes and prepares an image to be outputted as a mosaic.

    Args:
        x (array): An input array to be rasterized.
        img_shape (tuple): A tuple for the image shape.
        tile_shape (tuple): A tuple holding the shape of each tile.
        tile_spacing (tuple): A tuple containing the spacing between tiles.
        scale (bool): If output array should be scaled between 0 and 1.
        output (bool): If output values should be returned as pixels or not.

    Returns:
        An output array containing the rasterized version of input array.

    
Rasterizes and prepares an image to be outputted as a mosaic.
Rasterizes and prepares an image to be outputted as a mosaic .
def _rasterize(x, img_shape, tile_shape, tile_spacing=(0, 0), scale=True, output=True):
    assert len(img_shape) == 2
    assert len(tile_shape) == 2
    assert len(tile_spacing) == 2
    out_shape = [(ishp + tsp) * tshp - tsp for ishp, tshp,
                 tsp in zip(img_shape, tile_shape, tile_spacing)]
    if isinstance(x, tuple):
        assert len(x) == 4
        if output:
            out_array = np.zeros(
                (out_shape[0], out_shape[1], 4), dtype='uint8')
            channel_defaults = [0, 0, 0, 255]
        else:
            out_array = np.zeros(
                (out_shape[0], out_shape[1], 4), dtype=x[0].dtype)
            channel_defaults = [0., 0., 0., 1.]
        for i in range(4):
            if x[i] is None:
                out_array[:, :, i] = np.zeros(
                    out_shape, dtype=out_array.dtype) + channel_defaults[i]
            else:
                out_array[:, :, i] = _rasterize(
                    x[i], img_shape, tile_shape, tile_spacing, scale, output)
        return out_array
    H, W = img_shape
    Hs, Ws = tile_spacing
    dt = x.dtype
    if output:
        dt = 'uint8'
    out_array = np.zeros(out_shape, dtype=dt)
    for tile_row in range(tile_shape[0]):
        for tile_col in range(tile_shape[1]):
            if tile_row * tile_shape[1] + tile_col < x.shape[0]:
                x1 = x[tile_row * tile_shape[1] + tile_col]
                if scale:
                    img = scl.unitary_scale(x1.reshape(img_shape))
                else:
                    img = x1.reshape(img_shape)
                c = 1
                if output:
                    c = 255
                out_array[
                    tile_row * (H + Hs): tile_row * (H + Hs) + H,
                    tile_col * (W + Ws): tile_col * (W + Ws) + W
                ] = img * c
    return out_array
def _rasterize ( x , img_shape , tile_shape , tile_spacing = ( 0 , 0 ) , scale = True , output = True ) : assert len ( img_shape ) == 2 assert len ( tile_shape ) == 2 assert len ( tile_spacing ) == 2 out_shape = [ ( ishp + tsp ) * tshp - tsp for ishp , tshp , tsp in zip ( img_shape , tile_shape , tile_spacing ) ] if isinstance ( x , tuple ) : assert len ( x ) == 4 if output : out_array = np . zeros ( ( out_shape [ 0 ] , out_shape [ 1 ] , 4 ) , dtype = 'uint8' ) channel_defaults = [ 0 , 0 , 0 , 255 ] else : out_array = np . zeros ( ( out_shape [ 0 ] , out_shape [ 1 ] , 4 ) , dtype = x [ 0 ] . dtype ) channel_defaults = [ 0. , 0. , 0. , 1. ] for i in range ( 4 ) : if x [ i ] is None : out_array [ : , : , i ] = np . zeros ( out_shape , dtype = out_array . dtype ) + channel_defaults [ i ] else : out_array [ : , : , i ] = _rasterize ( x [ i ] , img_shape , tile_shape , tile_spacing , scale , output ) return out_array H , W = img_shape Hs , Ws = tile_spacing dt = x . dtype if output : dt = 'uint8' out_array = np . zeros ( out_shape , dtype = dt ) for tile_row in range ( tile_shape [ 0 ] ) : for tile_col in range ( tile_shape [ 1 ] ) : if tile_row * tile_shape [ 1 ] + tile_col < x . shape [ 0 ] : x1 = x [ tile_row * tile_shape [ 1 ] + tile_col ] if scale : img = scl . unitary_scale ( x1 . reshape ( img_shape ) ) else : img = x1 . reshape ( img_shape ) c = 1 if output : c = 255 out_array [ tile_row * ( H + Hs ) : tile_row * ( H + Hs ) + H , tile_col * ( W + Ws ) : tile_col * ( W + Ws ) + W ] = img * c return out_array
Rasterizes and prepares an image to be outputted as a mosaic.
Rasterizes and prepares an image to be outputted as a mosaic .
"""Rasterizes and prepares an image to be outputted as a mosaic.

    Args:
        x (array): An input array to be rasterized.
        img_shape (tuple): A tuple for the image shape.
        tile_shape (tuple): A tuple holding the shape of each tile.
        tile_spacing (tuple): A tuple containing the spacing between tiles.
        scale (bool): If output array should be scaled between 0 and 1.
        output (bool): If output values should be returned as pixels or not.

    Returns:
        An output array containing the rasterized version of input array.

    """ # Asserts if tuple lengths are equal to 2 # Creates an output shape # Asserts if input array is a tuple # Checks if its length is equal to 4 # If output boolean is true # Output values as pixels # Apply the default channels # If output boolean is false # Output values as its input type # Apply the default channels # For every possible item in tuple # If there is no channel # Fill it with zeros of the correct dtype # If there is a channel # Use a recurrent call to compute the channel and store it # Gathers the image shape and its tile spacing # Checks the current input dtype # If output boolean is true # Output type should be an unsigned integer # Creates a zeros array based on output shape # For every row of tiles # For every column of tiles # Checks if belongs to an specific row # Replace its value # If scale boolean is true # We should scale values to be between 0 and 1 # If not # We just reshape the image to the input shape # Add the slice to the corresponding position in the output array # If output boolean is true # The slice should be a maximum pixel value # Creates the output array
An output array containing the rasterized version of input array.
An output array containing the rasterized version of input array .
x
An input array to be rasterized.
An input array to be rasterized .
img_shape
A tuple for the image shape.
A tuple for the image shape .
tile_shape
A tuple holding the shape of each tile.
A tuple holding the shape of each tile .
tile_spacing
A tuple containing the spacing between tiles.
A tuple containing the spacing between tiles .
scale
If output array should be scaled between 0 and 1.
If output array should be scaled between 0 and 1 .
output
If output values should be returned as pixels or not.
If output values should be returned as pixels or not .