def get_background_color(img: np.ndarray, mask: np.ndarray) -> np.ndarray:
    """
    Get the background color of the image within the masked region.

    Args:
        img (np.ndarray): The input image as a NumPy array.
        mask (np.ndarray): The binary mask that defines the region of interest.

    Returns:
        np.ndarray: The most frequent color within the masked region.
    """

    def compute_color_similarity(color1, color2):
        r1, g1, b1 = color1
        r2, g2, b2 = color2
        distance = ((r1 - r2) ** 2 + (g1 - g2) ** 2 + (b1 - b2) ** 2) ** 0.5
        return distance

    def get_no_white(colors, threshold=10):
        for color in colors:
            dis = compute_color_similarity(color, [255, 255, 255])
            if dis > threshold:
                return color
        return colors[0]

    # Get the coordinates of the mask area
    mask_area = np.where(mask > 0)

    # Calculate the pixel value distribution within the masked region
    img_mask_area = img[mask_area]
    unique_values, counts = np.unique(img_mask_area, axis=0, return_counts=True)

    sorted_indices = np.argsort(-counts)
    sorted_colors = unique_values[sorted_indices]

    TopK = min(len(sorted_colors), 20)
    sorted_colors = sorted_colors[:TopK]

    most_frequent_value = get_no_white(sorted_colors)

    return most_frequent_value

    Get the background color of the image within the masked region.

    Args:
        img (np.ndarray): The input image as a NumPy array.
        mask (np.ndarray): The binary mask that defines the region of interest.

    Returns:
        np.ndarray: The most frequent color within the masked region.
    
scripts/easyphoto_utils/tryon_utils.py