Bird
Raised Fist0
Computer Visionml~10 mins

Color space conversion in Computer Vision - Interactive Code Practice

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to convert an image from BGR to grayscale using OpenCV.

Computer Vision
gray_image = cv2.cvtColor(color_image, [1])
Drag options to blanks, or click blank then click option'
Acv2.COLOR_BGR2GRAY
Bcv2.COLOR_RGB2GRAY
Ccv2.COLOR_GRAY2BGR
Dcv2.COLOR_BGR2RGB
Attempts:
3 left
💡 Hint
Common Mistakes
Using cv2.COLOR_RGB2GRAY which assumes the image is in RGB format.
Using cv2.COLOR_GRAY2BGR which converts grayscale to color, not the other way.
2fill in blank
medium

Complete the code to convert an image from BGR to HSV color space.

Computer Vision
hsv_image = cv2.cvtColor(input_image, [1])
Drag options to blanks, or click blank then click option'
Acv2.COLOR_RGB2HSV
Bcv2.COLOR_BGR2HSV
Ccv2.COLOR_HSV2BGR
Dcv2.COLOR_BGR2GRAY
Attempts:
3 left
💡 Hint
Common Mistakes
Using cv2.COLOR_RGB2HSV which is incorrect for BGR images.
Using cv2.COLOR_HSV2BGR which converts back from HSV to BGR.
3fill in blank
hard

Fix the error in the code to convert an image from HSV back to BGR.

Computer Vision
bgr_image = cv2.cvtColor(hsv_image, [1])
Drag options to blanks, or click blank then click option'
Acv2.COLOR_BGR2HSV
Bcv2.COLOR_RGB2HSV
Ccv2.COLOR_HSV2BGR
Dcv2.COLOR_BGR2GRAY
Attempts:
3 left
💡 Hint
Common Mistakes
Using cv2.COLOR_BGR2HSV which converts in the wrong direction.
Using cv2.COLOR_BGR2GRAY which changes color space incorrectly.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps color names to their HSV ranges.

Computer Vision
hsv_ranges = {color: ([1], [2]) for color in colors}
Drag options to blanks, or click blank then click option'
Alower_bounds
Bupper_bounds
Ccolor
Drange
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'color' or 'range' instead of the HSV bound variables.
Swapping lower and upper bounds.
5fill in blank
hard

Fill all three blanks to create a mask for a color range and apply it to the image.

Computer Vision
mask = cv2.inRange(hsv_image, [1], [2])
result = cv2.bitwise_and(original_image, original_image, mask=[3])
Drag options to blanks, or click blank then click option'
Alower_hsv
Bupper_hsv
Cmask
Dhsv_image
Attempts:
3 left
💡 Hint
Common Mistakes
Using the image instead of mask in the bitwise_and function.
Swapping lower and upper HSV bounds.

Practice

(1/5)
1. What is the main purpose of converting an image from RGB to grayscale in computer vision?
easy
A. To increase the number of color channels for better detail
B. To change the image format to JPEG
C. To reduce the image to a single channel representing brightness
D. To add color saturation to the image

Solution

  1. Step 1: Understand RGB and grayscale formats

    RGB images have three color channels (red, green, blue), while grayscale images have one channel representing brightness.
  2. Step 2: Purpose of conversion

    Converting to grayscale simplifies the image by reducing it to brightness information only, which helps in many vision tasks.
  3. Final Answer:

    To reduce the image to a single channel representing brightness -> Option C
  4. Quick Check:

    RGB to grayscale = single brightness channel [OK]
Hint: Grayscale means one brightness channel, not colors [OK]
Common Mistakes:
  • Thinking grayscale adds colors
  • Confusing grayscale with increasing channels
  • Assuming conversion changes file format
2. Which OpenCV function is used to convert an image from one color space to another?
easy
A. cv2.cvtColor()
B. cv2.changeColor()
C. cv2.convertColor()
D. cv2.colorTransform()

Solution

  1. Step 1: Recall OpenCV color conversion functions

    OpenCV provides a function named cvtColor to convert images between color spaces.
  2. Step 2: Identify correct function name

    The correct function is cv2.cvtColor(), not any other variant.
  3. Final Answer:

    cv2.cvtColor() -> Option A
  4. Quick Check:

    OpenCV color conversion = cvtColor() [OK]
Hint: Remember 'cv' stands for color and 't' for transform [OK]
Common Mistakes:
  • Using incorrect function names like convertColor
  • Confusing with other OpenCV functions
  • Misspelling cvtColor
3. What will be the output shape of the image after converting a 100x100 RGB image to HSV using OpenCV?
medium
A. (100, 3, 100)
B. (100, 100, 3)
C. (3, 100, 100)
D. (100, 100)

Solution

  1. Step 1: Understand input image shape

    The input RGB image has shape (100, 100, 3) representing height, width, and 3 color channels.
  2. Step 2: Effect of color space conversion on shape

    Converting to HSV changes color representation but keeps the same shape with 3 channels.
  3. Final Answer:

    (100, 100, 3) -> Option B
  4. Quick Check:

    RGB to HSV keeps shape (height, width, 3) [OK]
Hint: Color space change keeps image shape, only channel meaning changes [OK]
Common Mistakes:
  • Assuming shape changes to 2D
  • Mixing channel dimension order
  • Thinking channels increase or decrease
4. Identify the error in this OpenCV code snippet for converting BGR to grayscale:
import cv2
img = cv2.imread('image.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
medium
A. cv2.imread reads image in BGR, but conversion uses COLOR_RGB2GRAY
B. The image path is incorrect
C. cv2.cvtColor should be cv2.convertColor
D. Missing import for numpy

Solution

  1. Step 1: Check image reading format

    cv2.imread reads images in BGR format by default, not RGB.
  2. Step 2: Check color conversion code

    The code uses COLOR_RGB2GRAY which expects RGB input, causing wrong conversion.
  3. Final Answer:

    cv2.imread reads image in BGR, but conversion uses COLOR_RGB2GRAY -> Option A
  4. Quick Check:

    BGR input needs COLOR_BGR2GRAY [OK]
Hint: Remember OpenCV reads images as BGR, not RGB [OK]
Common Mistakes:
  • Using COLOR_RGB2GRAY with BGR images
  • Misspelling cvtColor
  • Assuming numpy import needed here
5. You want to detect red objects in an image using HSV color space. Which HSV range is best to isolate red color?
hard
A. Hue: 100-140, Saturation: 200-255, Value: 200-255
B. Hue: 30-90, Saturation: 50-150, Value: 50-150
C. Hue: 90-150, Saturation: 0-50, Value: 0-50
D. Hue: 0-10 and 160-180, Saturation: 100-255, Value: 100-255

Solution

  1. Step 1: Understand HSV hue for red color

    Red color in HSV wraps around hue values near 0 and near 180 degrees, so two ranges are needed.
  2. Step 2: Saturation and value ranges for clear red detection

    High saturation and value help isolate bright red objects, so ranges 100-255 are appropriate.
  3. Final Answer:

    Hue: 0-10 and 160-180, Saturation: 100-255, Value: 100-255 -> Option D
  4. Quick Check:

    Red hue wraps around 0 and 180 in HSV [OK]
Hint: Red hue wraps around low and high ends of HSV scale [OK]
Common Mistakes:
  • Using only one hue range for red
  • Choosing low saturation/value ranges
  • Confusing hue ranges for other colors