In computer vision, an image is represented as a matrix of pixels. Each pixel contains intensity values that define its brightness and color. Understanding pixels, RGB, Grayscale, and HSV is the foundation of image processing in OpenCV.
A pixel (picture element) is the smallest unit of an image. Images are stored as a grid of pixels, where each pixel has values that represent either intensity (black & white) or color (RGB/HSV).
The RGB model represents images using three channels: Red, Green, and Blue.
Each pixel has a combination of values (0–255) for each channel.
For example: (255, 0, 0) is pure red, (0, 255, 0) is pure green, and (0, 0, 255) is pure blue.
import cv2
import matplotlib.pyplot as plt
img = cv2.imread("image.jpg") # Reads in BGR format
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img_rgb)
plt.title("RGB Image")
plt.show()
A Grayscale image uses only one channel (0–255 intensity values). It removes color information and keeps brightness only, which is useful for tasks like edge detection and thresholding.
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow("Grayscale", gray)
cv2.waitKey(0)
cv2.destroyAllWindows()
The HSV model separates image color into three components:
🔹 Hue – type of color (red, green, blue, etc.)
🔹 Saturation – intensity of the color
🔹 Value – brightness of the pixel
HSV is often used in color detection and tracking.
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
cv2.imshow("HSV Image", hsv)
cv2.waitKey(0)
cv2.destroyAllWindows()
✅ To summarize:
• Pixels are the smallest building blocks of an image.
• RGB uses Red, Green, and Blue channels.
• Grayscale removes color and keeps intensity only.
• HSV separates colors into hue, saturation, and brightness.
In the next lesson, we will explore how to read, write, and display images in OpenCV.
