The first thing you do in any computer vision program is read an image from disk. In this tutorial you will learn how to load an image with OpenCV in color or in grayscale, and when to use each.
What You Will Learn
- How
cv2.imread()works - The difference between a color and a grayscale image
- The shortcut values 0 and 1 for the read mode
- How to show an image in a window
Prerequisites
Python 3 installed and OpenCV installed with pip install opencv-python. Keep an image named machinelearning.png in the same folder as your script.
Syntax
cv2.imread(filename, flag)
| Parameter | Meaning |
|---|---|
filename |
Path of the image file, for example 'machinelearning.png' |
flag |
How to read the image: cv2.IMREAD_COLOR (1), cv2.IMREAD_GRAYSCALE (0) or cv2.IMREAD_UNCHANGED (-1) |
Reading a Grayscale Image
A grayscale image has only one channel. Each pixel is a single number from 0 (black) to 255 (white). Grayscale images are smaller and faster to process, so they are used in face detection, edge detection and OCR.
import cv2
img = cv2.imread('machinelearning.png', cv2.IMREAD_GRAYSCALE)
cv2.imshow('Machine Learning', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Explanation:
import cv2loads the OpenCV library.cv2.IMREAD_GRAYSCALEconverts the image to black and white while reading it.cv2.imshow(window_name, image)opens a window and shows the image.cv2.waitKey(0)waits until you press any key. Without it, the window closes instantly.cv2.destroyAllWindows()closes all OpenCV windows.
A window titled Machine Learning opens and shows the image in black and white.
Shortcut: Use 0 Instead of IMREAD_GRAYSCALE
import cv2
img = cv2.imread('machinelearning.png', 0)
cv2.imshow('Machine Learning', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Explanation: The flag cv2.IMREAD_GRAYSCALE has the value 0, so both lines do exactly the same thing. Using the named constant makes your code easier to read.
Reading a Color Image
A color image has three channels. OpenCV stores them in BGR order (Blue, Green, Red), not RGB.
import cv2
img = cv2.imread('machinelearning.png', cv2.IMREAD_COLOR)
cv2.imshow('Machine Learning', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Explanation: cv2.IMREAD_COLOR loads the image with 3 color channels. It is also the default, so cv2.imread('machinelearning.png') gives the same result.
The image opens in full color.
Shortcut: Use 1 Instead of IMREAD_COLOR
import cv2
img = cv2.imread('machinelearning.png', 1)
cv2.imshow('Machine Learning', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Check the Difference with shape
The quickest way to see the difference is to print the shape of both images.
import cv2
color = cv2.imread('machinelearning.png', 1)
gray = cv2.imread('machinelearning.png', 0)
print(color.shape)
print(gray.shape)
Explanation: A color image has shape (height, width, 3). A grayscale image has shape (height, width) because it has only one channel. The exact numbers depend on your image.
Common Errors
| Problem | Cause and fix |
|---|---|
img is None / error (-215:Assertion failed) !_src.empty() |
OpenCV could not find the file. cv2.imread() does not raise an error for a wrong path, it returns None. Check the file name and run the script from the folder that contains the image. |
ModuleNotFoundError: No module named 'cv2' |
Install OpenCV: pip install opencv-python |
| Window does not open in Jupyter / Google Colab | cv2.imshow() needs a desktop. In Colab use from google.colab.patches import cv2_imshow, or display with Matplotlib. |
Practice Exercises
- Read the same image in color and grayscale and compare their
shape. - Use
cv2.IMREAD_UNCHANGEDon a transparent PNG and check how many channels it has. - Save the grayscale image with
cv2.imwrite('gray.png', img).
Interview Questions
Q: In what order does OpenCV store color channels?
BGR (Blue, Green, Red). Matplotlib and Pillow use RGB, so you must convert with cv2.cvtColor(img, cv2.COLOR_BGR2RGB) before showing an OpenCV image in Matplotlib.
Q: What does cv2.imread() return if the file is missing?None. It does not raise an error, so always check if img is None.
Summary
Use cv2.imread(path, 1) for color and cv2.imread(path, 0) for grayscale. Show images with imshow, waitKey and destroyAllWindows.
Machine Learning Tutorials, Courses and Certifications