How to Change T-Shirt Color and Remove Background Using Python & Mediapipe

In this article, we’ll explore how to use computer vision techniques to change the color of a T-shirt that a man is wearing and remove the background from the image. This is a common task in applications like e-commerce, virtual try-on systems, and visual effects.

We’ll use Python along with libraries such as OpenCV for image manipulation and segmentation, NumPy for handling pixel data, and some optional deep learning methods for more complex tasks like background removal.

Prerequisites

Before we start, make sure you have the following libraries installed:

pip install opencv-python numpy matplotlib

We will also use deep learning techniques for background removal, you will also need:

pip install mediapipe

Step-by-Step Approach

We will break down the task into two main parts:

  1. Background removal: We’ll use segmentation techniques to isolate the person and remove the background.
  2. T-shirt color change: We’ll use color masking to detect the T-shirt and apply color transformations.

1. Background Removal

Approach:

We will use OpenCV to detect the person and mask out the background. An optional deep learning-based approach using MediaPipe can provide more accuracy.

Using OpenCV for Background Removal:

We’ll use a simple method with OpenCV’s GrabCut algorithm, which is an iterative method for foreground-background segmentation.

import cv2
import numpy as np
import matplotlib.pyplot as plt

# Load the image
image = cv2.imread('man.jpg')

# Create a mask
mask = np.zeros(image.shape[:2], np.uint8)

# Define background and foreground models (just placeholders)
bg_model = np.zeros((1, 65), np.float64)
fg_model = np.zeros((1, 65), np.float64)

# Define a rectangle around the subject (man with T-shirt)
rect = (50, 50, image.shape[1] - 50, image.shape[0] - 50)

# Apply the GrabCut algorithm
cv2.grabCut(image, mask, rect, bg_model, fg_model, 5, cv2.GC_INIT_WITH_RECT)

# Modify the mask
mask2 = np.where((mask == 2) | (mask == 0), 0, 1).astype('uint8')

# Apply the mask to the image
image_no_bg = image * mask2[:, :, np.newaxis]

# Display the result
plt.imshow(cv2.cvtColor(image_no_bg, cv2.COLOR_BGR2RGB))
plt.title('Background Removed')
plt.show()

Output:

Explanation:

  • GrabCut Algorithm: This method requires an initial bounding box around the subject and iteratively improves the segmentation.
  • Masking: We use a binary mask where the foreground is separated from the background. Non-foreground pixels are set to black.

Using MediaPipe for Background Removal

Now if you see in the above output the background is not completely removed it is changed to black color instead, in order to remove it completely we will use MediaPipe.

MediaPipe is a library that provides more advanced body segmentation. It can handle complex scenarios where the background and subject are not easily separated using traditional methods.

import cv2
import mediapipe as mp
import numpy as np
import matplotlib.pyplot as plt

# Initialize MediaPipe
mp_selfie_segmentation = mp.solutions.selfie_segmentation
segment = mp_selfie_segmentation.SelfieSegmentation(model_selection=1)

# Load image
image = cv2.imread('man.jpg')
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Process the image
results = segment.process(image_rgb)

# Create mask
mask = results.segmentation_mask
condition = mask > 0.5  # Consider pixels with segmentation probability > 50%

# Replace the background with white
background = np.ones_like(image) * 255
image_no_bg = np.where(condition[..., None], image, background)

# Display the result
plt.imshow(cv2.cvtColor(image_no_bg, cv2.COLOR_BGR2RGB))
plt.title('Background Removed with MediaPipe')
plt.show()

This method is more sophisticated and often produces better results when the background is complex.

Output:

2. T-Shirt Color Change

Once we have removed the background, we can focus on detecting and changing the color of the T-shirt.

Approach:

We will use color thresholding to detect the T-shirt based on its color. Then, we will change the color by manipulating the pixel values in the detected region.

Code for T-Shirt Color Change:

# Convert image to HSV (Hue, Saturation, Value) color space
hsv_image = cv2.cvtColor(image_no_bg, cv2.COLOR_BGR2HSV)

# Define the color range for the T-shirt (assuming the T-shirt is blue)
lower_blue = np.array([90, 50, 50])
upper_blue = np.array([130, 255, 255])

# Create a mask for the T-shirt based on the color range
tshirt_mask = cv2.inRange(hsv_image, lower_blue, upper_blue)

# Change the T-shirt color (to red in this case)
# We define the new color in BGR format
new_color = [0, 0, 255]  # Red color in BGR

# Apply the color change to the T-shirt area
image_no_bg[tshirt_mask != 0] = new_color

# Display the result
plt.imshow(cv2.cvtColor(image_no_bg, cv2.COLOR_BGR2RGB))
plt.title('T-Shirt Color Changed to Red')
plt.show()

Explanation:

  • HSV Color Space: The HSV color space is used because it separates color (hue) from intensity, making it easier to detect specific colors like the T-shirt’s color.
  • Color Thresholding: We define a color range to create a mask that identifies the T-shirt. In this case, we assumed the T-shirt is blue.
  • Color Change: We replace the pixel values in the T-shirt’s region with a new color.

Full Source Code:

import cv2
import mediapipe as mp
import numpy as np
import matplotlib.pyplot as plt

# Initialize MediaPipe
mp_selfie_segmentation = mp.solutions.selfie_segmentation
segment = mp_selfie_segmentation.SelfieSegmentation(model_selection=1)

# Load image
image = cv2.imread('man.jpg')
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Process the image
results = segment.process(image_rgb)

# Create mask
mask = results.segmentation_mask
condition = mask > 0.5  # Consider pixels with segmentation probability > 50%

# Replace the background with white
background = np.ones_like(image) * 255
image_no_bg = np.where(condition[..., None], image, background)

# Convert image to HSV (Hue, Saturation, Value) color space
hsv_image = cv2.cvtColor(image_no_bg, cv2.COLOR_BGR2HSV)

# Define the color range for the T-shirt (assuming the T-shirt is blue)
lower_blue = np.array([90, 50, 50])
upper_blue = np.array([130, 255, 255])

# Create a mask for the T-shirt based on the color range
tshirt_mask = cv2.inRange(hsv_image, lower_blue, upper_blue)

# Change the T-shirt color (to red in this case)
# We define the new color in BGR format
new_color = [0, 0, 255]  # Red color in BGR

# Apply the color change to the T-shirt area
image_no_bg[tshirt_mask != 0] = new_color

# Display the result
plt.imshow(cv2.cvtColor(image_no_bg, cv2.COLOR_BGR2RGB))
plt.title('T-Shirt Color Changed to Red')
plt.show()


# Display the result
plt.imshow(cv2.cvtColor(image_no_bg, cv2.COLOR_BGR2RGB))
plt.title('Background Removed with MediaPipe')
plt.show()

In this tutorial, we explored how to leverage Python’s computer vision capabilities, particularly using OpenCV and Mediapipe, to perform two key tasks: removing the background of an image and changing the color of a T-shirt. These techniques are highly relevant for industries such as fashion, e-commerce, and entertainment, where personalized image editing and visual effects play a crucial role.

We started with the basic principles of background removal using OpenCV’s GrabCut algorithm. This semi-automatic segmentation method effectively isolates the foreground (in this case, the person wearing the T-shirt) from the background. While this technique works well in simple scenarios, we introduced Mediapipe as a more advanced alternative, offering precise background removal using deep learning models. Mediapipe’s selfie segmentation model provided an efficient and accurate solution for more complex images.

Once the background was removed, we shifted focus to the T-shirt color change. By converting the image into the HSV color space, we were able to isolate the T-shirt based on its color range and apply a new color to the identified region. This process not only showcases the flexibility of Python in manipulating pixel-level data but also highlights how intuitive color transformations can be performed in real-world applications.

Throughout the tutorial, we emphasized a step-by-step approach, combining image segmentation, color space manipulation, and pixel masking. These methods can be expanded further for various use cases, such as virtual clothing try-on, product customization, or even augmented reality experiences.

In conclusion, Python, with the help of libraries like OpenCV and Mediapipe, provides powerful tools for image manipulation tasks. Whether you’re looking to remove backgrounds for product photos or offer users interactive color customization options, the techniques discussed here offer a solid foundation for building sophisticated image-processing applications. With further refinement, such as enhancing color detection or improving segmentation accuracy, these methods can be integrated into production-level systems across a wide range of industries.

Author

Sona Avatar

Written by

Leave a Reply

Trending

CodeMagnet

Your Magnetic Resource, For Coding Brilliance

Programming Languages

Web Development

Data Science and Visualization

Career Section

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-4205364944170772"
     crossorigin="anonymous"></script>