Bird
Raised Fist0
Computer Visionml~20 mins

Color space conversion 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 space conversion
Problem:You want to convert images from RGB color space to grayscale and HSV color space to prepare data for a computer vision model.
Current Metrics:N/A - currently only RGB images are used without color space conversion.
Issue:Using only RGB images may limit the model's ability to learn color and brightness features effectively.
Your Task
Implement color space conversion to grayscale and HSV for input images and compare model training accuracy using these different color spaces.
Use OpenCV or similar library for color conversion.
Keep the model architecture the same for fair comparison.
Use the same dataset split for training and validation.
Hint 1
Hint 2
Hint 3
Solution
Computer Vision
import cv2
import numpy as np
from sklearn.model_selection import train_test_split
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
from tensorflow.keras.utils import to_categorical

# Dummy dataset creation: 100 images of 64x64 with 3 channels (RGB)
np.random.seed(42)
X_rgb = np.random.randint(0, 256, (100, 64, 64, 3), dtype=np.uint8)
y = np.random.randint(0, 2, 100)  # Binary classification

def convert_to_grayscale(images):
    return np.array([cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) for img in images])

def convert_to_hsv(images):
    return np.array([cv2.cvtColor(img, cv2.COLOR_RGB2HSV) for img in images])

# Convert images
X_gray = convert_to_grayscale(X_rgb)
X_hsv = convert_to_hsv(X_rgb)

# Reshape grayscale to add channel dimension
X_gray = X_gray.reshape(-1, 64, 64, 1)

# Normalize all images
X_rgb = X_rgb / 255.0
X_gray = X_gray / 255.0
X_hsv = X_hsv / 255.0

# Prepare labels
y_cat = to_categorical(y, 2)

# Split data
X_train_rgb, X_val_rgb, y_train, y_val = train_test_split(X_rgb, y_cat, test_size=0.2, random_state=42)
X_train_gray, X_val_gray, _, _ = train_test_split(X_gray, y_cat, test_size=0.2, random_state=42)
X_train_hsv, X_val_hsv, _, _ = train_test_split(X_hsv, y_cat, test_size=0.2, random_state=42)

# Define simple CNN model
def create_model(input_shape):
    model = Sequential([
        Conv2D(16, (3,3), activation='relu', input_shape=input_shape),
        MaxPooling2D((2,2)),
        Flatten(),
        Dense(32, activation='relu'),
        Dense(2, activation='softmax')
    ])
    model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
    return model

# Train and evaluate on RGB
model_rgb = create_model((64,64,3))
history_rgb = model_rgb.fit(X_train_rgb, y_train, epochs=5, batch_size=16, validation_data=(X_val_rgb, y_val), verbose=0)

# Train and evaluate on Grayscale
model_gray = create_model((64,64,1))
history_gray = model_gray.fit(X_train_gray, y_train, epochs=5, batch_size=16, validation_data=(X_val_gray, y_val), verbose=0)

# Train and evaluate on HSV
model_hsv = create_model((64,64,3))
history_hsv = model_hsv.fit(X_train_hsv, y_train, epochs=5, batch_size=16, validation_data=(X_val_hsv, y_val), verbose=0)

# Collect final validation accuracies
val_acc_rgb = history_rgb.history['val_accuracy'][-1] * 100
val_acc_gray = history_gray.history['val_accuracy'][-1] * 100
val_acc_hsv = history_hsv.history['val_accuracy'][-1] * 100

print(f"Validation Accuracy RGB: {val_acc_rgb:.2f}%")
print(f"Validation Accuracy Grayscale: {val_acc_gray:.2f}%")
print(f"Validation Accuracy HSV: {val_acc_hsv:.2f}%")
Added functions to convert RGB images to grayscale and HSV color spaces using OpenCV.
Normalized images after conversion for model input.
Trained the same CNN model architecture separately on RGB, grayscale, and HSV images.
Compared validation accuracy to evaluate which color space helps the model learn better.
Results Interpretation

Before: Model trained only on RGB images without color space conversion.

After: Model trained separately on RGB, grayscale, and HSV images with color space conversion.

  • Validation accuracy is similar across color spaces on random data but in real datasets, color space choice can impact performance.
  • Grayscale reduces input size and focuses on brightness.
  • HSV separates color and brightness, which can help models learn color features better.
Color space conversion is a useful preprocessing step in computer vision. It can help models learn different aspects of images like brightness or color separately, potentially improving performance depending on the task.
Bonus Experiment
Try converting images to the LAB color space and train the model to see if it improves validation accuracy compared to RGB, grayscale, and HSV.
💡 Hint
LAB color space separates lightness from color channels and is designed to be perceptually uniform, which might help the model learn better color features.

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