Skip to main content
Computer Vision Tutorial
CHAPTER 04 Beginner

Working with Pixels and Colors

Updated: May 14, 2026
20 min read

# CHAPTER 4

Working with Pixels and Colors

1. Introduction

To truly master Computer Vision, you must stop looking at pictures as visual art and start manipulating them as mathematical arrays. In this chapter, we will learn how to isolate specific pixels, change their colors, draw shapes, and explore different color models (like RGB and HSV) to isolate objects based on their color.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Access and modify individual pixels using array coordinates.
  • Understand the BGR color format used in OpenCV.
  • Explain the HSV color model and why it is superior for color tracking.
  • Draw lines, rectangles, and text directly onto an image matrix.

3. Beginner-Friendly Explanation

Imagine a massive grid of LED light bulbs on the wall of a stadium. To draw a smiley face on the wall, you don't use a paintbrush. You use a computer terminal to type: "Turn on the bulb at Row 5, Column 10 to Yellow." Working with pixels in Python is exactly the same. We use coordinates (X and Y) to pinpoint an exact pixel in the image matrix and manually change its numerical value from Blue to Red, or from Black to White. This is how we draw "Bounding Boxes" around faces in security footage!

4. Pixel Coordinates

In math class, the origin (0,0) is usually the bottom-left corner of a graph. In Computer Vision (and computer graphics in general), the origin (0,0) is the TOP-LEFT corner of the image.
  • The X-axis runs horizontally (left to right).
  • The Y-axis runs vertically (top to bottom).
So, the pixel at (X: 50, Y: 100) is 50 pixels to the right, and 100 pixels down from the top-left corner.

5. Accessing and Modifying Pixels

Because images are loaded as NumPy arrays in Python, we access pixels using row and column indexing: image[Y, X]. (Notice it is Y first, then X!).
python
1234567891011
import cv2

# Load an image
img = cv2.imread("photo.jpg")

# Get the color of the pixel at Y=100, X=50
pixel_color = img[100, 50]
print(pixel_color) # Output: [255, 0, 0] (This is Pure Blue in BGR!)

# Let's change that specific pixel to Pure Red
img[100, 50] = [0, 0, 255] # [Blue, Green, Red]

6. The RGB vs BGR Trap

As mentioned in Chapter 2, OpenCV reads color channels in Blue, Green, Red (BGR) format, NOT the standard Red, Green, Blue (RGB).
  • Pure Red in OpenCV is [0, 0, 255].
  • Pure Blue in OpenCV is [255, 0, 0].
Always remember this when manually setting colors in OpenCV!

7. The HSV Color Space (Hue, Saturation, Value)

If you want to build an AI that tracks a Yellow Tennis Ball, you might try looking for pixels with high Red and Green values (which mix to make yellow). But if the ball moves into a shadow, the RGB values drastically change, and your code breaks! To track colors robustly, CV engineers use the HSV Color Model:
  • H (Hue): The actual color type (e.g., Yellow, Red, Blue).
  • S (Saturation): How intense/pure the color is (Washed out vs Vibrant).
  • V (Value): How bright or dark the pixel is.
If the tennis ball moves into a shadow, the "Value" changes, but the "Hue" remains exactly the same! This makes HSV the industry standard for color detection.

8. Drawing on Images

Computer Vision applications need a way to show humans what they found. When an AI detects a car, we must draw a rectangle around it.
python
123456789101112
import cv2

# Load image
img = cv2.imread("street.jpg")

# Draw a Green Rectangle (Start X,Y), (End X,Y), (B,G,R color), Thickness
cv2.rectangle(img, (100, 100), (300, 400), (0, 255, 0), 3)

# Write text on the image
cv2.putText(img, "Car Detected", (100, 90), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)

cv2.imwrite("output.jpg", img)

9. Mini Project

Color Tracker Logic: You want to build a robot that follows a bright orange cone. Should you write an algorithm that looks for specific RGB values, or specific HSV values? Why? *(Answer: HSV values. If a cloud passes over the sun, the RGB values of the cone will completely change, breaking the robot. With HSV, the Hue (Orange) remains constant even if the lighting changes).*

10. Best Practices

  • Use NumPy Slicing: Never use a for loop to change the color of thousands of pixels one by one in Python. It will be incredibly slow. Use NumPy array slicing (e.g., image[0:100, 0:100] = [0,0,0]) to instantly black out a 100x100 square in milliseconds.

11. Common Mistakes

  • Confusing X and Y indices: When dealing with OpenCV functions like cv2.rectangle(), the coordinates are usually (X, Y). But when accessing the raw NumPy array image[Y, X], it is Row (Y) then Column (X). Mixing these up will crash your program.

12. Exercises

  1. 1. In the OpenCV BGR color format, what array of numbers represents the color Pure Green?

13. Coding Challenges

Challenge 1: Write pseudocode to draw a red bounding box around a face if the face is detected at coordinates X=50, Y=50 with a width of 100 and a height of 100.
text
1234567891011
face_x = 50
face_y = 50
face_width = 100
face_height = 100

// Calculate the bottom right corner
end_x = face_x + face_width
end_y = face_y + face_height

// Draw the rectangle (Using BGR for Red: 0, 0, 255)
draw_rectangle(image, start=(face_x, face_y), end=(end_x, end_y), color=(0, 0, 255))

14. MCQs with Answers

Question 1

Where is the coordinate (0,0) located on a digital image in computer vision?

Question 2

Why is the HSV color space better than RGB for tracking a specifically colored object in a video?

15. Interview Questions

  • Q: Explain why accessing individual pixels using nested for loops in Python is a bad idea, and what you should use instead.
  • Q: Describe a scenario where converting an image from BGR to the HSV color space is necessary for a Computer Vision task.

16. FAQs

Q: Can I change the color of an entire image at once? A: Yes! Using OpenCV's cv2.cvtColor() function, you can instantly convert a BGR image to Grayscale, HSV, or any other color mapping mathematically in a fraction of a second.

17. Summary

In Chapter 4, we learned how to act like digital painters. By treating images as 2D and 3D coordinate grids, we can pinpoint exact pixels, alter their color values, and draw shapes and text directly onto the matrix. Furthermore, by understanding advanced models like HSV, we can build robust color-tracking algorithms that don't break the moment the sun goes behind a cloud.

18. Next Chapter Recommendation

We know how to alter single pixels, but how do we manipulate the whole image to blur it, sharpen it, or rotate it? Proceed to Chapter 5: Image Transformations and Filtering to learn about mathematical convolutions.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·