Bird
Raised Fist0
Computer Visionml~20 mins

Color spaces (RGB, BGR, grayscale, HSV) in Computer Vision - ML Experiment: Train & Evaluate

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
Experiment - Color spaces (RGB, BGR, grayscale, HSV)
Problem:You have an image loaded in BGR format using OpenCV. You want to convert it to RGB, grayscale, and HSV color spaces to prepare it for different computer vision tasks.
Current Metrics:Currently, the image is only in BGR format, which causes incorrect color display when shown with libraries expecting RGB. No color space conversions are applied.
Issue:The image colors appear wrong when displayed, and grayscale or HSV representations are not available for further processing.
Your Task
Convert the input BGR image to RGB, grayscale, and HSV color spaces correctly and display all versions side by side.
Use OpenCV functions for color space conversion.
Do not change the original image data.
Display images using matplotlib which expects RGB format.
Hint 1
Hint 2
Hint 3
Solution
Computer Vision
import cv2
import matplotlib.pyplot as plt

# Load image in BGR format
image_bgr = cv2.imread('sample.jpg')

# Convert BGR to RGB
image_rgb = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2RGB)

# Convert BGR to Grayscale
image_gray = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2GRAY)

# Convert BGR to HSV
image_hsv = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2HSV)

# Display all images side by side
fig, axs = plt.subplots(1, 4, figsize=(16, 5))

axs[0].imshow(image_rgb)  # Show original image in RGB for correct colors
axs[0].set_title('RGB (Original)')
axs[0].axis('off')

axs[1].imshow(image_rgb)  # Correct color display
axs[1].set_title('RGB')
axs[1].axis('off')

axs[2].imshow(image_gray, cmap='gray')  # Grayscale image
axs[2].set_title('Grayscale')
axs[2].axis('off')

axs[3].imshow(image_hsv)  # HSV shown as RGB will look strange
axs[3].set_title('HSV (shown as RGB)')
axs[3].axis('off')

plt.tight_layout()
plt.show()
Added conversion from BGR to RGB for correct color display in matplotlib.
Added conversion from BGR to grayscale for single channel image processing.
Added conversion from BGR to HSV for color-based segmentation tasks.
Displayed all images side by side for easy comparison.
Fixed display of original image to use RGB instead of BGR for correct colors.
Results Interpretation

Before: Image displayed in BGR format looks incorrect in matplotlib because it expects RGB.

After: RGB image shows correct colors, grayscale image shows intensity, HSV image shows color components differently.

Understanding and correctly converting color spaces is essential in computer vision to ensure images are processed and displayed as intended.
Bonus Experiment
Try converting the image from RGB to LAB color space and display it alongside the others.
💡 Hint
Use cv2.cvtColor() with cv2.COLOR_RGB2LAB and remember to convert BGR to RGB first.

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