Bird
Raised Fist0
Computer Visionml~5 mins

Image as numerical data (pixels, channels) in Computer Vision

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction

We turn images into numbers so computers can understand and work with them. This helps us teach machines to see and recognize things.

When building a program to recognize faces in photos.
When creating an app that sorts pictures by content.
When training a robot to identify objects by looking.
When analyzing medical images like X-rays or MRIs.
When enhancing photos by adjusting colors or brightness.
Syntax
Computer Vision
image_array = [[[R, G, B], [R, G, B], ...],  # row 1
               [[R, G, B], [R, G, B], ...],  # row 2
               ...]

# R, G, B are numbers from 0 to 255 representing colors

Images are stored as 3D arrays: height x width x channels.

Each pixel has values for Red, Green, and Blue channels.

Examples
This pixel is pure red because red is 255 and green and blue are 0.
Computer Vision
pixel = [255, 0, 0]  # bright red pixel
This is a tiny 2x2 image with black, white, gray, and dark gray pixels.
Computer Vision
image = [[[0, 0, 0], [255, 255, 255]],
         [[128, 128, 128], [64, 64, 64]]]  # 2x2 image
This creates a 100 by 100 black image using a numpy array filled with zeros.
Computer Vision
import numpy as np
image_np = np.zeros((100, 100, 3), dtype=np.uint8)  # 100x100 black image
Sample Model

This code creates a small 3x3 image with different colors. It shows how to check the image size, get a pixel's color, and find the average color of the whole image.

Computer Vision
import numpy as np

# Create a 3x3 image with 3 color channels (RGB)
image = np.array([
    [[255, 0, 0], [0, 255, 0], [0, 0, 255]],  # red, green, blue
    [[255, 255, 0], [0, 255, 255], [255, 0, 255]],  # yellow, cyan, magenta
    [[0, 0, 0], [128, 128, 128], [255, 255, 255]]  # black, gray, white
], dtype=np.uint8)

# Print shape of image
print(f"Image shape: {image.shape}")

# Access pixel at row 1, column 2
pixel = image[1, 2]
print(f"Pixel at (1,2): {pixel}")

# Calculate average color of the image
avg_color = image.mean(axis=(0,1))
print(f"Average color (RGB): {avg_color.astype(int)}")
OutputSuccess
Important Notes

Pixel values usually range from 0 to 255 for each color channel.

Images can have more channels, like an alpha channel for transparency.

Converting images to numbers is the first step before feeding them to machine learning models.

Summary

Images are stored as numbers in arrays with height, width, and color channels.

Each pixel has values for red, green, and blue colors.

Understanding this helps us prepare images for machine learning tasks.

Practice

(1/5)
1. What does each pixel in a color image usually represent?
easy
A. A single number representing brightness only
B. A sound wave frequency
C. A text label describing the image
D. A set of numbers for red, green, and blue colors

Solution

  1. Step 1: Understand pixel representation in color images

    Each pixel stores values for red, green, and blue channels to show color.
  2. Step 2: Compare options to pixel data

    Only A set of numbers for red, green, and blue colors correctly describes pixels as sets of RGB numbers.
  3. Final Answer:

    A set of numbers for red, green, and blue colors -> Option D
  4. Quick Check:

    Pixel = RGB values [OK]
Hint: Pixels hold RGB numbers, not text or sound [OK]
Common Mistakes:
  • Thinking pixels store text labels
  • Confusing pixel with brightness only
  • Assuming pixels represent sound
2. Which Python code correctly creates a 3x3 image with 3 color channels filled with zeros?
easy
A. image = np.zeros((3, 3, 3))
B. image = np.zeros(3, 3, 3)
C. image = np.zeros[3, 3, 3]
D. image = zeros((3, 3, 3))

Solution

  1. Step 1: Recall numpy zeros syntax

    np.zeros requires a single tuple argument for shape, like (3, 3, 3).
  2. Step 2: Check each option's syntax

    image = np.zeros((3, 3, 3)) uses correct tuple and function call syntax. Others have syntax errors or missing np.
  3. Final Answer:

    image = np.zeros((3, 3, 3)) -> Option A
  4. Quick Check:

    np.zeros((3,3,3)) creates 3x3 RGB image [OK]
Hint: Use np.zeros with shape tuple inside parentheses [OK]
Common Mistakes:
  • Passing multiple arguments instead of a tuple
  • Using square brackets instead of parentheses
  • Forgetting np. prefix
3. Given this code:
import numpy as np
image = np.array([[[255, 0, 0], [0, 255, 0]],
                  [[0, 0, 255], [255, 255, 0]]])
print(image.shape)

What is the output?
medium
A. (2, 3, 2)
B. (3, 2, 2)
C. (2, 2, 3)
D. (3, 3, 3)

Solution

  1. Step 1: Analyze the array structure

    The array has 2 rows, each with 2 pixels, each pixel has 3 color values (RGB).
  2. Step 2: Determine shape order

    Shape is (height=2, width=2, channels=3), so (2, 2, 3).
  3. Final Answer:

    (2, 2, 3) -> Option C
  4. Quick Check:

    Shape = (rows, cols, channels) = (2, 2, 3) [OK]
Hint: Shape is (height, width, channels) in that order [OK]
Common Mistakes:
  • Mixing up dimensions order
  • Counting channels as first dimension
  • Assuming square shape without checking
4. What is wrong with this code snippet for accessing the green channel of an image?
green_channel = image[:, :, 1:2]
medium
A. It returns a 3D array instead of 2D
B. It causes an index error
C. It accesses the red channel instead
D. It modifies the original image

Solution

  1. Step 1: Understand slicing with 1:2

    Slicing with 1:2 keeps the channel dimension, returning shape (height, width, 1).
  2. Step 2: Compare with expected 2D array

    To get a 2D array, use index 1 without slice, like image[:, :, 1].
  3. Final Answer:

    It returns a 3D array instead of 2D -> Option A
  4. Quick Check:

    Slicing with 1:2 keeps channel dim [OK]
Hint: Use single index, not slice, for 2D channel array [OK]
Common Mistakes:
  • Using slice returns extra dimension
  • Confusing channel indices
  • Assuming it changes original image
5. You have a grayscale image stored as a 2D array with shape (100, 100). You want to convert it to a 3-channel RGB image by repeating the grayscale values across all channels. Which code correctly does this?
hard
A. rgb_image = np.repeat(gray_image, 3)
B. rgb_image = np.stack([gray_image]*3, axis=2)
C. rgb_image = gray_image.reshape(100, 100, 3)
D. rgb_image = np.concatenate(gray_image, 3)

Solution

  1. Step 1: Understand the goal

    We want to create a 3D array where each pixel's grayscale value repeats in 3 channels.
  2. Step 2: Check each method

    rgb_image = np.stack([gray_image]*3, axis=2) stacks the grayscale image 3 times along new channel axis correctly. rgb_image = np.repeat(gray_image, 3) repeats flattening data, wrong shape. rgb_image = gray_image.reshape(100, 100, 3) reshapes without adding channels, causing error. rgb_image = np.concatenate(gray_image, 3) has wrong syntax.
  3. Final Answer:

    rgb_image = np.stack([gray_image]*3, axis=2) -> Option B
  4. Quick Check:

    Stack repeats grayscale across channels [OK]
Hint: Use np.stack with axis=2 to add channels [OK]
Common Mistakes:
  • Using np.repeat without axis
  • Reshaping without adding channel dimension
  • Wrong function syntax for concatenation