Bird
Raised Fist0
Computer Visionml~20 mins

Color spaces (RGB, BGR, grayscale, HSV) in Computer Vision - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
🎖️
Color Space Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output shape after converting an RGB image to grayscale using OpenCV?
Given an RGB image with shape (100, 150, 3), what will be the shape of the image after converting it to grayscale using OpenCV's cvtColor function?
Computer Vision
import cv2
import numpy as np

image_rgb = np.random.randint(0, 256, (100, 150, 3), dtype=np.uint8)
gray_image = cv2.cvtColor(image_rgb, cv2.COLOR_RGB2GRAY)
print(gray_image.shape)
A(100, 150)
B(150, 100)
C(100, 150, 1)
D(100, 150, 3)
Attempts:
2 left
💡 Hint
Grayscale images have only one channel, so the shape loses the last dimension.
🧠 Conceptual
intermediate
1:30remaining
Why does OpenCV use BGR instead of RGB by default?
OpenCV uses BGR color order by default instead of RGB. What is the main reason for this?
ABecause BGR matches the order used by Windows bitmap files historically.
BBecause BGR is more efficient for GPU processing.
CBecause RGB is patented and requires licensing fees.
DBecause BGR uses less memory than RGB.
Attempts:
2 left
💡 Hint
Think about historical image file formats and compatibility.
Metrics
advanced
1:30remaining
Which metric is best to compare color similarity in HSV space?
You want to measure how similar two colors are in HSV space. Which metric is most appropriate?
ACosine similarity on grayscale intensities
BManhattan distance on RGB values
CEuclidean distance on HSV values
DHamming distance on binary color codes
Attempts:
2 left
💡 Hint
Consider the numeric nature of HSV and how distances reflect similarity.
🔧 Debug
advanced
2:00remaining
Why does this code produce a blueish image instead of red?
You load an image using OpenCV and try to display a pure red color by setting pixel values to (255, 0, 0). The displayed image looks blueish. Why?
Computer Vision
import cv2
import numpy as np

image = np.zeros((100, 100, 3), dtype=np.uint8)
image[:] = (255, 0, 0)  # Set to red
cv2.imshow('Image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
AThe data type uint8 causes color distortion.
BOpenCV uses BGR order, so (255, 0, 0) is blue, not red.
CThe display window is not refreshing properly.
DThe image array is not initialized correctly and contains random values.
Attempts:
2 left
💡 Hint
Check the color channel order OpenCV expects.
Model Choice
expert
2:30remaining
Which color space is best for skin tone detection in images?
You want to build a model to detect skin tones in images robustly under different lighting. Which color space is generally best to use as input features?
ARGB, because it is the raw color data from cameras.
BGrayscale, because it simplifies the image to intensity only.
CBGR, because OpenCV uses it by default.
DHSV, because it separates color information from brightness.
Attempts:
2 left
💡 Hint
Think about separating color from light intensity to handle lighting changes.

Practice

(1/5)
1. Which color space is commonly used by OpenCV as the default when reading images?
easy
A. HSV
B. BGR
C. Grayscale
D. RGB

Solution

  1. Step 1: Understand OpenCV image reading default

    OpenCV reads images using the BGR color space by default, not RGB.
  2. Step 2: Compare common color spaces

    RGB is common in many libraries, but OpenCV specifically uses BGR order for color images.
  3. Final Answer:

    BGR -> Option B
  4. Quick Check:

    OpenCV default color space = BGR [OK]
Hint: Remember OpenCV uses BGR, not RGB by default [OK]
Common Mistakes:
  • Confusing RGB with BGR as default
  • Thinking grayscale is default for color images
  • Assuming HSV is default color space
2. Which of the following is the correct OpenCV Python code to convert an image from BGR to grayscale?
easy
A. gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
B. gray = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
C. gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
D. gray = cv2.cvtColor(image, cv2.COLOR_HSV2GRAY)

Solution

  1. Step 1: Identify correct color conversion code

    To convert from BGR to grayscale, use cv2.COLOR_BGR2GRAY in cv2.cvtColor.
  2. Step 2: Check other options for correctness

    Options A, C, and D use wrong conversions or directions.
  3. Final Answer:

    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) -> Option C
  4. Quick Check:

    BGR to grayscale uses cv2.COLOR_BGR2GRAY [OK]
Hint: Use cv2.COLOR_BGR2GRAY to convert BGR to grayscale [OK]
Common Mistakes:
  • Using RGB instead of BGR conversion code
  • Trying to convert grayscale to BGR instead
  • Using HSV conversion code incorrectly
3. What will be the shape of the output image after converting a color image of shape (480, 640, 3) from BGR to grayscale using OpenCV?
medium
A. (480, 640)
B. (640, 480)
C. (480, 640, 3)
D. (480, 640, 1)

Solution

  1. Step 1: Understand input image shape

    The input image has shape (480, 640, 3), meaning height=480, width=640, and 3 color channels.
  2. Step 2: Effect of BGR to grayscale conversion

    Converting to grayscale removes color channels, resulting in a 2D array with shape (480, 640).
  3. Final Answer:

    (480, 640) -> Option A
  4. Quick Check:

    Grayscale image shape = (height, width) [OK]
Hint: Grayscale images have 2D shape, no color channels [OK]
Common Mistakes:
  • Assuming grayscale keeps 3 channels
  • Swapping height and width in shape
  • Expecting a single channel dimension like (480,640,1)
4. You wrote this code to convert an image from BGR to HSV but got incorrect results:
hsv = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)

What is the likely cause of the incorrect results?
medium
A. cv2.cvtColor cannot convert to HSV color space
B. cv2.COLOR_RGB2HSV is not a valid conversion code
C. The image must be grayscale before converting to HSV
D. The image is in BGR, but conversion expects RGB input

Solution

  1. Step 1: Check image color space and conversion code

    The image is in BGR format by default, but the code uses COLOR_RGB2HSV which expects RGB input.
  2. Step 2: Identify mismatch causing incorrect results

    Using COLOR_RGB2HSV on a BGR image causes incorrect conversion; correct code is COLOR_BGR2HSV.
  3. Final Answer:

    The image is in BGR, but conversion expects RGB input -> Option D
  4. Quick Check:

    Use matching color space codes for input image [OK]
Hint: Match input image color space with conversion code [OK]
Common Mistakes:
  • Using RGB conversion code on BGR images
  • Thinking grayscale is needed before HSV
  • Believing cv2.cvtColor can't convert to HSV
5. You want to detect red objects in an image using HSV color space. Which step is essential before applying a color range mask for red detection?
hard
A. Convert the image from BGR to HSV using cv2.COLOR_BGR2HSV
B. Convert the image from grayscale to HSV
C. Convert the image from RGB to grayscale
D. Apply a Gaussian blur before converting to BGR

Solution

  1. Step 1: Understand color detection in HSV

    HSV color space separates color information, making it easier to detect specific colors like red.
  2. Step 2: Convert image to HSV from correct input space

    Since OpenCV images are BGR by default, convert from BGR to HSV using cv2.COLOR_BGR2HSV before masking.
  3. Final Answer:

    Convert the image from BGR to HSV using cv2.COLOR_BGR2HSV -> Option A
  4. Quick Check:

    Convert BGR to HSV before color masking [OK]
Hint: Always convert BGR to HSV before color range masking [OK]
Common Mistakes:
  • Skipping conversion or using wrong color space
  • Trying to convert grayscale to HSV
  • Applying blur before correct color conversion