In some scenarios, in computer vision, we need to process an image; the background can hinder the process. For this very purpose, we have to see how we can remove the background from any image. In this Answer, we will use the OpenCV library to implement this functionality.
Computer vision is a field of artificial intelligence that focuses on enabling machines to interpret and understand visual information from images or videos. It involves developing algorithms and techniques to process, analyze, and extract meaningful information from visual data. such as object detection, image segmentation, face recognition, and motion tracking. Computer vision finds applications in various industries, such as:
Robotics
Autonomous vehicles
Healthcare
Surveillance
Learn more in detail.
In this implementation, we are going to separate the background from the foreground. This is easier said than done. The basic overview of this program goes similar to this:
Read the image and resize it according to the desired size
Convert the image to gray scale and apply Gaussian blur
Create a threshold image of the image
Invert the image, resulting in a mask image, and implement a logical AND operation.
In this Answer, we will use the OpenCV library to implement this functionality. With the help of OpenCV functions, we can pull off the desired outcome. Let's see how we can install the OpenCV library in Python.
pip install opencv-python
The project below shows a complete implementation of the background removal process:
import cv2 image_path = "images.png" image = cv2.imread(image_path) #resizing the image desired_width = 400 aspect_ratio = image.shape[1] / image.shape[0] desired_height = int(desired_width / aspect_ratio) resized_image = cv2.resize(image, (desired_width, desired_height)) def onTrackbarChange(value): global blk_thresh blk_thresh = value print("Variable value:", blk_thresh) def valueScaling(value): min_value = 0 max_value = 100 new_min = 0 new_max = 255 scaled_value = (value - min_value) * (new_max - new_min) / (max_value - min_value) + new_min return int(scaled_value) blk_thresh = 50 scaled_thresh = valueScaling(blk_thresh) window_name = 'Background Removed' cv2.namedWindow(window_name) cv2.createTrackbar('Variable', window_name, scaled_thresh, 100, onTrackbarChange) while True: gray = cv2.cvtColor(resized_image, cv2.COLOR_BGR2GRAY) blur = cv2.GaussianBlur(gray, (5, 5), 0) _, threshold_img = cv2.threshold(blur, blk_thresh, 255, cv2.THRESH_BINARY) mask = 255 - threshold_img result = cv2.bitwise_and(resized_image, resized_image, mask=mask) cv2.imshow(window_name, result) if cv2.waitKey(1) & 0xFF == ord('q'): break cv2.destroyAllWindows()
Line 1: Import the OpenCV library as cv2
.
Line 3: Define the variable image_path
and set it to the path of the image file.
Line 4: Use the cv2.imread()
function to read the image from the specified path, storing it in the variable image
.
Line 7 – 10: In this section of code, we calculate the aspect ratio of the desired image and then store it in the variable aspect_ratio
. Then using the cv2.resize()
function, we resize the image to the desired aspect ratio and store it in the variable resized_image
.
Line 12 – 15: In this code section, we define the function onTrackbarChange
which is called whenever the trackbar position changes and the value of blk_thresh
is set to the input value
.
Line 17 – 23: In this code section, we define the function valueScaling
which takes value
as an input and performs value scaling to convert it from the range [0,255] to the range [0, 100].
Line 30: Create a named window with the specified window_name
.
Line 33: Create a trackbar named 'Variable'
within the created window, with the initial value set to scaled_thresh
. When the trackbar position changes, the onTrackbarChange
function will be called.
Line 35: Start an infinite loop to process the frames continuously of the resized_image
.
Line 36: Convert the resized_image
to grayscale using cv2.cvtColor()
and store the result in the variable gray
.
Line 38: Apply Gaussian blur to the grayscale image using cv2.GaussianBlur()
with a kernel size of (5, 5) and store the result in the variable blur
.
Line 40: Apply binary thresholding to the blurred image using the cv2.threshold()
function, using the blk_thresh
as the threshold value. Pixels with intensity greater than blk_thresh
will be set to 255 (white), and others to 0 (black). Store the thresholded image in the variable threshold_img
.
Line 42: Invert the thresholded image by subtracting it from 255 to create a binary mask, where white pixels (255) in threshold_img
become black (0), and vice versa. Store the resulting mask in the variable mask
.
Line 44: Perform a bitwise AND operation between the resized_image
and itself, but use mask
to mask out the background storing the resulting image in the variable result
.
Line 46 – 51: This section of code displays the resulting output.
Free Resources