pulse2percept.utils.images

center_image, scale_image, shift_image, trim_image

Functions

center_image(img[, loc])

Center the image foreground

scale_image(img, scaling_factor)

Scale the image foreground

shift_image(img, shift_cols, shift_rows)

Shift the image foreground

trim_image(img[, tol, return_coords])

Remove any black border around the image

pulse2percept.utils.images.shift_image(img, shift_cols, shift_rows)[source]

Shift the image foreground

This function shifts the center of mass (CoM) of the image by the specified number of rows and columns. The background of the image is assumed to be black (0 grayscale).

Added in version 0.7.

Parameters:
  • img (ndarray) – A 2D NumPy array representing a (height, width) grayscale image, or a 3D NumPy array representing a (height, width, channels) RGB image

  • shift_cols (float) – Number of columns by which to shift the CoM. Positive: to the right, negative: to the left

  • shift_rows (float) – Number of rows by which to shift the CoM. Positive: downward, negative: upward

Returns:

img – A copy of the shifted image

Return type:

ndarray

pulse2percept.utils.images.center_image(img, loc=None)[source]

Center the image foreground

This function shifts the center of mass (CoM) to the image center. The background of the image is assumed to be black (0 grayscale).

Added in version 0.7.

Parameters:
  • img (ndarray) – A 2D NumPy array representing a (height, width) grayscale image, or a 3D NumPy array representing a (height, width, channels) RGB image

  • loc ((col, row), optional) – The pixel location at which to center the CoM. By default, shifts the CoM to the image center.

Returns:

img – A copy of the image centered at loc

Return type:

ndarray

pulse2percept.utils.images.scale_image(img, scaling_factor)[source]

Scale the image foreground

This function scales the image foreground by a factor. The background of the image is assumed to be black (0 grayscale).

Added in version 0.7.

Parameters:
  • img (ndarray) – A 2D NumPy array representing a (height, width) grayscale image, or a 3D NumPy array representing a (height, width, channels) RGB image

  • scaling_factor (float) – Factory by which to scale the image

Returns:

img – A copy of the scaled image

Return type:

ndarray

pulse2percept.utils.images.trim_image(img, tol=0, return_coords=False)[source]

Remove any black border around the image

Added in version 0.7.

Parameters:
  • img (ndarray) – A 2D NumPy array representing a (height, width) grayscale image, or a 3D NumPy array representing a (height, width, channels) RGB image. If an alpha channel is present, the image will first be blended with black.

  • tol (float, optional) – Any pixels with gray levels > tol will be trimmed.

  • return_coords (bool, optional) – If True, will also return the row and column coordinates of the retained image

Returns:

  • img (ndarray) – A copy of the image with trimmed borders.

  • (row_start, row_end) (tuple, optional) – The range of row indices in the trimmed image (returned only if return_coords is True)

  • (col_start, col_end) (tuple, optional) – The range of column indices in the trimmed image (returned only if return_coords is True)