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).
(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 asNumPy arrays in Python, we access pixels using row and column indexing: image[Y, X]. (Notice it is Y first, then X!).
python
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].
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.
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
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
forloop 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 arrayimage[Y, X], it is Row (Y) then Column (X). Mixing these up will crash your program.
12. Exercises
- 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
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
forloops 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'scv2.cvtColor() function, you can instantly convert a BGR image to Grayscale, HSV, or any other color mapping mathematically in a fraction of a second.