In this tutorial, we will explore how to convert an image into black and white using the OpenCV library in Python.
Converting images to black and white is a great way to simplify image processing tasks, remove color-related noise, and focus on the most important visual elements of an image.
Step 1: Import necessary libraries
First, we need to import the OpenCV library, which we will use to read and manipulate images. We also import matplotlib for displaying images.
1 2 |
import cv2 import matplotlib.pyplot as plt |
Step 2: Load the image
Next, let’s load the image using OpenCV’s imread()
function. Replace the 'path/to/your/image.jpg'
placeholder with the path to your own image.
1 2 |
# Load the image img = cv2.imread('path/to/your/image.jpg') |
Step 3: Convert the image to grayscale
Now, we’ll convert the image to grayscale to prep it for the conversion process. We use the cv2.cvtColor()
function with the cv2.COLOR_BGR2GRAY
argument.
1 2 |
# Convert the image to grayscale gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) |
Step 4: Threshold the grayscale image
We need to set a threshold value that will separate the image into black and white pixels. This will be the new, black-and-white image. Use the cv2.threshold()
function with a suitable threshold value and threshold type cv2.THRESH_BINARY
. Here, we set the threshold value to 128
.
1 2 3 4 5 |
# Set the threshold value (128) threshold_value = 128 # Convert the grayscale image to black and white _, bw_img = cv2.threshold(gray_img, threshold_value, 255, cv2.THRESH_BINARY) |
Step 5: Display the black-and-white image
Finally, display the black and white image using matplotlib.pyplot
.
1 2 3 4 5 6 7 8 9 10 |
# Display the original image and the black and white image side by side plt.subplot(1, 2, 1) plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) plt.title('Original Image') plt.subplot(1, 2, 2) plt.imshow(bw_img, cmap='gray') plt.title('Black and White Image') plt.show() |
This code will display the original image and the black-and-white image side by side.
Full Code
Here’s the complete code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import cv2 import matplotlib.pyplot as plt # Load the image img = cv2.imread('robot.jpg') # Convert the image to grayscale gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Set the threshold value (128) threshold_value = 128 # Convert the grayscale image to black and white _, bw_img = cv2.threshold(gray_img, threshold_value, 255, cv2.THRESH_BINARY) # Display the original image and the black and white image side by side plt.subplot(1, 2, 1) plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) plt.title('Original Image') plt.subplot(1, 2, 2) plt.imshow(bw_img, cmap='gray') plt.title('Black and White Image') plt.show() |
Output
Conclusion
We have successfully converted an image to black and white in Python using OpenCV. This simple yet powerful technique can be applied to various image-processing tasks in computer vision, machine learning, and deep learning. Feel free to experiment with different threshold values to tune the conversion process for the best results.