Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Accessing Pixels
Accessing individual pixels lets you inspect and verify what an image looks like at the numerical level. It is a fundamental debugging skill in Computer Vision.
Pixel Access Syntax
OpenCV images are NumPy arrays, so you access pixels using standard NumPy indexing.
For a colour image (BGR):
image[row, col] → returns array [B, G, R]
image[row, col, 0] → Blue channel value
image[row, col, 1] → Green channel value
image[row, col, 2] → Red channel value
For a grayscale image:
image[row, col] → returns a single brightness value
Remember: row = y (vertical), col = x (horizontal)
image[0, 0] is the top-left pixel
image[height-1, width-1] is the bottom-right pixel
Machine Learning follows a structured pipeline from data to deployment
Pixel Access Examples
import cv2
import numpy as np
image = cv2.imread("photo.jpg")
h, w, c = image.shape
# Access a single pixel — returns [Blue, Green, Red]
pixel = image[100, 150]
print(f"Pixel at (row=100, col=150): {pixel}")
print(f" Blue: {pixel[0]}")
print(f" Green: {pixel[1]}")
print(f" Red: {pixel[2]}")
# Access corner pixels
corners = {
"Top-left": image[0, 0],
"Top-right": image[0, w-1],
"Bottom-left": image[h-1, 0],
"Bottom-right": image[h-1, w-1],
"Centre": image[h//2, w//2],
}
for name, px in corners.items():
print(f"{name:15s}: B={px[0]:3d} G={px[1]:3d} R={px[2]:3d}")
# Access an entire row of pixels
row_50 = image[50, :] # All columns in row 50
print(f"Row 50 shape: {row_50.shape}") # (width, 3)
# Access an entire column of pixels
col_100 = image[:, 100] # All rows in column 100
print(f"Col 100 shape: {col_100.shape}") # (height, 3)