garmentiq.segmentation.change_background_color

 1import numpy as np
 2
 3
 4def change_background_color(
 5    image_np: np.ndarray, mask_np: np.ndarray, background_color: tuple[int, int, int]
 6):
 7    """
 8    Changes the background color of an image based on a given mask.
 9
10    This function modifies the background of an image by replacing the pixels
11    that correspond to the background (as identified by the mask) with a specified color.
12
13    Args:
14        image_np (numpy.ndarray): The original image as a numpy array (RGB).
15        mask_np (numpy.ndarray): A binary mask where background pixels are labeled as 0 and foreground as 1.
16        background_color (tuple[int, int, int]): A tuple representing the RGB color to replace the background with,
17                                                 e.g., (255, 255, 255) for white.
18
19    Returns:
20        numpy.ndarray: A new image with the background color changed.
21    """
22    # Create a mask where the background (black) is 1, and the foreground is 0
23    background_mask = mask_np == 0  # Mask values of 0 represent background
24
25    # Create a copy of the original image to modify
26    modified_image = image_np.copy()
27
28    # Change the background areas to the specified background color
29    modified_image[background_mask] = background_color
30
31    return modified_image
def change_background_color( image_np: numpy.ndarray, mask_np: numpy.ndarray, background_color: tuple[int, int, int]):
 5def change_background_color(
 6    image_np: np.ndarray, mask_np: np.ndarray, background_color: tuple[int, int, int]
 7):
 8    """
 9    Changes the background color of an image based on a given mask.
10
11    This function modifies the background of an image by replacing the pixels
12    that correspond to the background (as identified by the mask) with a specified color.
13
14    Args:
15        image_np (numpy.ndarray): The original image as a numpy array (RGB).
16        mask_np (numpy.ndarray): A binary mask where background pixels are labeled as 0 and foreground as 1.
17        background_color (tuple[int, int, int]): A tuple representing the RGB color to replace the background with,
18                                                 e.g., (255, 255, 255) for white.
19
20    Returns:
21        numpy.ndarray: A new image with the background color changed.
22    """
23    # Create a mask where the background (black) is 1, and the foreground is 0
24    background_mask = mask_np == 0  # Mask values of 0 represent background
25
26    # Create a copy of the original image to modify
27    modified_image = image_np.copy()
28
29    # Change the background areas to the specified background color
30    modified_image[background_mask] = background_color
31
32    return modified_image

Changes the background color of an image based on a given mask.

This function modifies the background of an image by replacing the pixels that correspond to the background (as identified by the mask) with a specified color.

Arguments:
  • image_np (numpy.ndarray): The original image as a numpy array (RGB).
  • mask_np (numpy.ndarray): A binary mask where background pixels are labeled as 0 and foreground as 1.
  • background_color (tuple[int, int, int]): A tuple representing the RGB color to replace the background with, e.g., (255, 255, 255) for white.
Returns:

numpy.ndarray: A new image with the background color changed.