Sub topic 4
BGR, RGB, and Grayscale
Assume you know basic Python, can install packages with pip, and understand NumPy array indexing. OpenCV represents color images as NumPy arrays, but it uses a channel order that differs from many Python libraries.
OpenCV's BGR convention
A color image usually has three channels: red, green, and blue. Each pixel stores one intensity value for each channel. With an 8-bit image, each value usually ranges from 0 to 255.
OpenCV stores these channels in BGR order:
- Blue
- Green
- Red
An image with height h and width w therefore has this shape:
(h, w, 3)The first two dimensions are array coordinates, so they are ordered as (row, column), or (y, x). The final dimension selects the color channel.
import cv2
img = cv2.imread("photo.jpg")
if img is None:
raise FileNotFoundError("Could not read photo.jpg")
print(img.shape)
print(img.dtype)
blue, green, red = img[0, 0]
print(blue, green, red)The pixel at (0, 0) is accessed with img[0, 0], not img[x, y]. The three returned values are BGR values.
BGR and RGB are different interpretations
RGB is the channel order used by many other tools, including Pillow and Matplotlib. The same three channel values mean different colors when their order changes.
Use cv2.cvtColor when moving between OpenCV and an RGB-based library:
import cv2
img_bgr = cv2.imread("photo.jpg")
if img_bgr is None:
raise FileNotFoundError("Could not read photo.jpg")
img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
# Convert an RGB array back to OpenCV's usual BGR order.
restored_bgr = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2BGR)
cv2.imwrite("converted.jpg", restored_bgr)cv2.imshow expects an OpenCV-style BGR image, so an image loaded with cv2.imread normally displays with correct colors. Matplotlib's imshow expects RGB data. Passing a BGR array directly to Matplotlib commonly makes red objects appear blue and blue objects appear red.
Grayscale images
A grayscale image has one intensity channel instead of three color channels. Black is near 0, white is near 255, and values between them represent shades of gray.
OpenCV stores a grayscale image as a two-dimensional array:
(height, width)Convert BGR to grayscale with the named conversion code:
import cv2
img = cv2.imread("photo.jpg")
if img is None:
raise FileNotFoundError("Could not read photo.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
print(img.shape) # (height, width, 3)
print(gray.shape) # (height, width)
print(gray.dtype) # usually uint8
cv2.imwrite("photo-gray.jpg", gray)Grayscale conversion is not usually a simple average of the three channels. OpenCV weights the channels because human vision responds differently to red, green, and blue. Keeping the conversion explicit also makes the code clear and avoids relying on channel-order assumptions.
| Array | Channels | Typical shape | Common use |
|---|---|---|---|
| BGR image | 3 | `(height, width, 3)` | OpenCV processing |
| RGB image | 3 | `(height, width, 3)` | Matplotlib or Pillow |
| Grayscale image | 1 | `(height, width)` | Edges, thresholds, measurements |
A practical color-checking workflow
When colors look wrong, inspect the array at each library boundary:
- Identify which library produced the array.
- Check the shape and dtype.
- Determine whether the array is BGR or RGB.
- Convert with
cv2.cvtColorbefore handing it to another library. - Save or display a small test image containing obvious red, green, and blue areas.
Exercise
An image loaded with cv2.imread looks correct in cv2.imshow but has swapped red and blue colors in Matplotlib. What should you do?
Show answer
Convert the image with cv2.cvtColor(image, cv2.COLOR_BGR2RGB) before passing it to Matplotlib.