garmentiq.segmentation.change_background_color

Replacing the background of an image using a segmentation mask.

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