Bird
Raised Fist0
MATLABdata~20 mins

Image types (grayscale, RGB) in MATLAB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Image Type Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of size for grayscale vs RGB image
What is the output size of the variable img after running the code below?
MATLAB
img = imread('peppers.png');
size_img = size(img);
A[512 384 3]
B[384 512 3]
C[256 256]
D[512 512]
Attempts:
2 left
💡 Hint
Think about the dimensions of a color image in MATLAB: height x width x color channels.
❓ Predict Output
intermediate
2:00remaining
Output class type of grayscale image
What is the output of the following code?
MATLAB
img = rgb2gray(imread('peppers.png'));
class_type = class(img);
A'uint8'
B'double'
C'logical'
D'uint16'
Attempts:
2 left
💡 Hint
Check the default class type returned by rgb2gray when input is uint8.
🔧 Debug
advanced
2:00remaining
Identify the error in RGB channel extraction
What error will the following code produce?
MATLAB
img = imread('peppers.png');
redChannel = img(:,:,4);
ANo error, returns the 4th channel.
BUndefined function or variable 'img'.
CSyntax error: missing semicolon.
DIndex exceeds matrix dimensions.
Attempts:
2 left
💡 Hint
Check how many color channels an RGB image has.
🧠 Conceptual
advanced
2:00remaining
Difference between grayscale and RGB image matrices
Which statement correctly describes the difference between grayscale and RGB images in MATLAB?
ABoth grayscale and RGB images are always 3D matrices.
BRGB images are 2D matrices; grayscale images are 3D matrices.
CGrayscale images are 2D matrices; RGB images are 3D matrices.
DGrayscale images have 3 channels; RGB images have 1 channel.
Attempts:
2 left
💡 Hint
Think about how color information is stored in image arrays.
🚀 Application
expert
3:00remaining
Count unique intensity values in grayscale image
Given a grayscale image img, which code correctly counts the number of unique intensity values?
MATLAB
img = rgb2gray(imread('peppers.png'));
AnumUnique = length(unique(img));
BnumUnique = numel(unique(img));
CnumUnique = size(unique(img),1);
DnumUnique = sum(img(:));
Attempts:
2 left
💡 Hint
unique returns a vector of unique values; count how many elements it has.

Practice

(1/5)
1. Which of the following correctly describes a grayscale image in MATLAB?
easy
A. It has one channel per pixel representing shades of gray.
B. It has three channels representing red, green, and blue colors.
C. It stores color information in four channels including alpha transparency.
D. It is always stored as a 3D matrix with size MxNx3.

Solution

  1. Step 1: Understand grayscale image structure

    Grayscale images have only one channel per pixel, showing intensity from black to white.
  2. Step 2: Compare with RGB image structure

    RGB images have three channels (red, green, blue), so grayscale images differ by having only one channel.
  3. Final Answer:

    It has one channel per pixel representing shades of gray. -> Option A
  4. Quick Check:

    Grayscale = 1 channel [OK]
Hint: Check if image size third dimension equals 1 for grayscale [OK]
Common Mistakes:
  • Confusing grayscale with RGB images having 3 channels
  • Assuming grayscale images have multiple color channels
  • Thinking grayscale images are always 3D matrices
2. Which MATLAB command correctly checks if an image img is RGB?
easy
A. size(img, 3) == 1
B. ndims(img) == 2
C. length(img) == 3
D. size(img, 3) == 3

Solution

  1. Step 1: Recall RGB image dimension

    RGB images have three color channels, so the third dimension size is 3.
  2. Step 2: Identify correct MATLAB syntax

    Using size(img, 3) == 3 checks if the third dimension equals 3, confirming RGB.
  3. Final Answer:

    size(img, 3) == 3 -> Option D
  4. Quick Check:

    Third dimension = 3 means RGB [OK]
Hint: Use size(img,3) to check color channels quickly [OK]
Common Mistakes:
  • Using ndims instead of size for channel count
  • Checking length instead of size for dimensions
  • Confusing 2D grayscale with RGB dimensions
3. Given the MATLAB code:
img = imread('peppers.png');
dim = size(img);
channels = dim(3);

What is the value of channels for this image?
medium
A. 1
B. 2
C. 3
D. 4

Solution

  1. Step 1: Understand the image type of 'peppers.png'

    'peppers.png' is a standard RGB image with three color channels.
  2. Step 2: Analyze the code output

    The size(img) returns a vector where the third element is the number of channels, which is 3 for RGB.
  3. Final Answer:

    3 -> Option C
  4. Quick Check:

    peppers.png is RGB, so channels = 3 [OK]
Hint: Standard color images usually have 3 channels [OK]
Common Mistakes:
  • Assuming PNG images are grayscale by default
  • Confusing number of dimensions with number of channels
  • Expecting 4 channels for PNG without alpha
4. Consider this MATLAB code snippet:
img = imread('cameraman.tif');
if size(img, 3) == 3
disp('RGB Image');
else
disp('Grayscale Image');
end

What is the output when running this code?
medium
A. Grayscale Image
B. RGB Image
C. Error: Index exceeds matrix dimensions.
D. No output

Solution

  1. Step 1: Identify image type of 'cameraman.tif'

    'cameraman.tif' is a grayscale image with only one channel.
  2. Step 2: Evaluate the condition in the code

    Since size(img, 3) returns 1 (or may not exist), the else branch runs, printing 'Grayscale Image'.
  3. Final Answer:

    Grayscale Image -> Option A
  4. Quick Check:

    cameraman.tif is grayscale, so output = Grayscale Image [OK]
Hint: Grayscale images have no 3rd dimension or size 1 [OK]
Common Mistakes:
  • Expecting 3 channels for all images
  • Assuming error due to missing third dimension
  • Ignoring else branch output
5. You have an image matrix img in MATLAB. Write a code snippet to convert an RGB image to grayscale only if it is RGB. Which of the following is correct?
hard
A. if ndims(img) == 2 img_gray = rgb2gray(img); else img_gray = img; end
B. if size(img, 3) == 3 img_gray = rgb2gray(img); else img_gray = img; end
C. if size(img, 3) == 1 img_gray = rgb2gray(img); else img_gray = img; end
D. if size(img, 3) == 4 img_gray = rgb2gray(img); else img_gray = img; end

Solution

  1. Step 1: Check if image is RGB by third dimension size

    RGB images have 3 channels, so size(img, 3) == 3 confirms RGB.
  2. Step 2: Convert RGB to grayscale only if condition is true

    Use rgb2gray(img) to convert RGB to grayscale; otherwise keep image as is.
  3. Final Answer:

    if size(img, 3) == 3 img_gray = rgb2gray(img); else img_gray = img; end -> Option B
  4. Quick Check:

    Convert only if 3 channels (RGB) [OK]
Hint: Check size(img,3)==3 before rgb2gray conversion [OK]
Common Mistakes:
  • Checking for 1 channel instead of 3
  • Using ndims instead of size for channels
  • Assuming 4 channels means RGB
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