Bird
Raised Fist0
MATLABdata~20 mins

Color space conversion in MATLAB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Color Space Conversion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of RGB to Grayscale Conversion
What is the output of this MATLAB code that converts an RGB pixel to grayscale using the luminance method?
MATLAB
rgb = [0.2, 0.4, 0.6];
gray = 0.2989 * rgb(1) + 0.5870 * rgb(2) + 0.1140 * rgb(3);
disp(gray);
A0.3630
B0.4000
C0.5000
D0.6000
Attempts:
2 left
💡 Hint
Recall the weighted sum formula for luminance grayscale conversion.
❓ Predict Output
intermediate
2:00remaining
Output of RGB to HSV Conversion
What is the output of this MATLAB code converting an RGB pixel to HSV?
MATLAB
rgb = [1, 0, 0];
hsv = rgb2hsv(rgb);
disp(hsv);
A[0 1 1]
B[0.5 1 1]
C[1 1 1]
D[0 0 1]
Attempts:
2 left
💡 Hint
Red color in HSV has hue 0, saturation 1, and value 1.
🔧 Debug
advanced
2:30remaining
Identify the error in RGB to YCbCr conversion code
This MATLAB code attempts to convert an RGB pixel to YCbCr but produces incorrect results. What is the error?
MATLAB
rgb = [0.5, 0.5, 0.5];
Y  = 0.299*rgb(1) + 0.587*rgb(2) + 0.114*rgb(3);
Cb = 128 - 0.168736*rgb(1) - 0.331264*rgb(2) + 0.5*rgb(3);
Cr = 128 + 0.5*rgb(1) - 0.418688*rgb(2) - 0.081312*rgb(3);
disp([Y Cb Cr]);
AY formula coefficients sum to more than 1 causing overflow
BMissing conversion of rgb values from 0-1 to 0-255 before calculation
CCb and Cr formulas use incorrect offsets; 128 should not be added for normalized RGB
Drgb vector indexing is wrong; should use rgb(3) for blue but uses rgb(2)
Attempts:
2 left
💡 Hint
YCbCr formulas expect RGB in 0-255 range, not 0-1.
📝 Syntax
advanced
2:00remaining
Syntax error in MATLAB code converting RGB to CMYK
Which option contains the correct syntax to convert an RGB pixel to CMYK in MATLAB?
Argb = [0.1 0.2 0.3]; K = 1 - max(rgb); C = (1 - rgb(1) - K) / (1 - K); M = (1 - rgb(2) - K) / (1 - K); Y = (1 - rgb(3) - K) / (1 - K); disp([C M Y K]);
Brgb = [0.1 0.2 0.3]; K = 1 - max(rgb); C = (1 - rgb(1) - K) / (1 - K); M = (1 - rgb(2) - K) / (1 - K); Y = (1 - rgb(3) - K) / (1 - K); disp([C, M, Y, K])
Crgb = [0.1 0.2 0.3]; K = 1 - max(rgb); C = (1 - rgb(1) - K) / (1 - K); M = (1 - rgb(2) - K) / (1 - K); Y = (1 - rgb(3) - K) / (1 - K); disp([C M Y K])
Drgb = [0.1 0.2 0.3]; K = 1 - max(rgb); C = (1 - rgb(1) - K) / (1 - K); M = (1 - rgb(2) - K) / (1 - K); Y = (1 - rgb(3) - K) / (1 - K); disp([C, M, Y, K]);
Attempts:
2 left
💡 Hint
Check MATLAB syntax for disp and vector construction.
🚀 Application
expert
3:00remaining
Number of pixels converted correctly in batch RGB to grayscale
Given this MATLAB code converting a 3x3 RGB image to grayscale, how many pixels have grayscale value exactly 0.5?
MATLAB
rgbImage = cat(3, [0.5 0.5 0.5; 0.2 0.5 0.8; 0.5 0.5 0.5], ...
                  [0.5 0.5 0.5; 0.2 0.5 0.8; 0.5 0.5 0.5], ...
                  [0.5 0.5 0.5; 0.2 0.5 0.8; 0.5 0.5 0.5]);
grayImage = 0.2989 * rgbImage(:,:,1) + 0.5870 * rgbImage(:,:,2) + 0.1140 * rgbImage(:,:,3);
count = sum(grayImage(:) == 0.5);
disp(count);
A6
B5
C7
D9
Attempts:
2 left
💡 Hint
Calculate grayscale for each pixel and count those equal to 0.5.

Practice

(1/5)
1. What does the MATLAB function rgb2gray do?
easy
A. Converts an image from CMYK to RGB
B. Converts a grayscale image to RGB
C. Converts an image from HSV to RGB
D. Converts a color image from RGB to grayscale

Solution

  1. Step 1: Understand the function name

    The function name rgb2gray suggests conversion from RGB to gray scale.
  2. Step 2: Recall MATLAB image processing basics

    In MATLAB, rgb2gray converts a color image (RGB) to a grayscale image by removing color information.
  3. Final Answer:

    Converts a color image from RGB to grayscale -> Option D
  4. Quick Check:

    rgb2gray = RGB to grayscale [OK]
Hint: Look for 'rgb2gray' meaning RGB to gray conversion [OK]
Common Mistakes:
  • Confusing rgb2gray with gray2rgb
  • Thinking it converts HSV images
  • Assuming it changes image size
2. Which of the following is the correct syntax to convert an RGB image stored in variable img to HSV in MATLAB?
easy
A. hsvImage = rgb2hsv(img);
B. hsvImage = hsv2rgb(img);
C. hsvImage = rgb2gray(img);
D. hsvImage = gray2hsv(img);

Solution

  1. Step 1: Identify the function for RGB to HSV conversion

    MATLAB uses rgb2hsv to convert RGB images to HSV color space.
  2. Step 2: Check the syntax correctness

    The syntax hsvImage = rgb2hsv(img); correctly calls the function with the image variable.
  3. Final Answer:

    hsvImage = rgb2hsv(img); -> Option A
  4. Quick Check:

    rgb2hsv syntax = correct [OK]
Hint: Remember rgb2hsv converts RGB to HSV [OK]
Common Mistakes:
  • Using hsv2rgb instead of rgb2hsv
  • Using rgb2gray for color conversion
  • Using non-existent gray2hsv function
3. What will be the output size of the variable grayImage after running this code?
rgbImage = imread('peppers.png');
grayImage = rgb2gray(rgbImage);
medium
A. Same height, width, and channels as rgbImage
B. Same height and width as rgbImage but single channel
C. Half the height and width of rgbImage
D. Double the height and width of rgbImage

Solution

  1. Step 1: Understand input image dimensions

    RGB images have height x width x 3 channels (for red, green, blue).
  2. Step 2: Effect of rgb2gray on image size

    rgb2gray converts to grayscale, keeping height and width but reducing channels to 1.
  3. Final Answer:

    Same height and width as rgbImage but single channel -> Option B
  4. Quick Check:

    rgb2gray output = height x width x 1 [OK]
Hint: Grayscale keeps size but reduces channels to 1 [OK]
Common Mistakes:
  • Assuming rgb2gray changes image dimensions
  • Thinking output has 3 channels
  • Confusing with resizing functions
4. Identify the error in this MATLAB code snippet for converting an RGB image to grayscale:
img = imread('image.jpg');
grayImg = rgb2gray(img, 'double');
medium
A. imread cannot read 'image.jpg'
B. Variable names are invalid in MATLAB
C. rgb2gray does not accept a second argument like 'double'
D. Missing semicolon after imread

Solution

  1. Step 1: Check rgb2gray function usage

    rgb2gray only takes one input argument: the RGB image.
  2. Step 2: Analyze the second argument 'double'

    Passing 'double' as a second argument causes an error because rgb2gray does not support it.
  3. Final Answer:

    rgb2gray does not accept a second argument like 'double' -> Option C
  4. Quick Check:

    rgb2gray single input only [OK]
Hint: Check function docs for allowed arguments [OK]
Common Mistakes:
  • Adding unsupported extra arguments
  • Assuming imread fails without reason
  • Thinking semicolon is mandatory for correctness
5. You want to convert an RGB image to HSV and then extract only the Hue channel for further processing. Which MATLAB code snippet correctly does this?
hard
A. hsvImage = rgb2hsv(rgbImage); hueChannel = hsvImage(:,:,1);
B. hsvImage = rgb2gray(rgbImage); hueChannel = hsvImage(:,:,1);
C. hsvImage = hsv2rgb(rgbImage); hueChannel = hsvImage(:,:,2);
D. hsvImage = rgb2hsv(rgbImage); hueChannel = hsvImage(:,:,3);

Solution

  1. Step 1: Convert RGB to HSV correctly

    Use rgb2hsv to convert the RGB image to HSV color space.
  2. Step 2: Extract the Hue channel

    In HSV, the first channel (:,:,1) is the Hue component.
  3. Final Answer:

    hsvImage = rgb2hsv(rgbImage); hueChannel = hsvImage(:,:,1); -> Option A
  4. Quick Check:

    Hue channel is first in HSV [OK]
Hint: Hue is channel 1 after rgb2hsv conversion [OK]
Common Mistakes:
  • Using rgb2gray instead of rgb2hsv
  • Extracting wrong channel index
  • Using hsv2rgb instead of rgb2hsv
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard