Bird
Raised Fist0
MATLABdata~20 mins

Reading and displaying images in MATLAB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Image Display Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this image display code?
Consider the following MATLAB code that reads and displays an image. What will be the result shown in the figure window?
MATLAB
img = imread('peppers.png');
imshow(img);
title('Original Image');
AA figure window opens showing the 'peppers.png' image with the title 'Original Image'.
BA figure window opens but the image is shown in grayscale regardless of original colors.
CThe code produces an error because 'imshow' cannot display color images.
DNothing happens because 'imshow' requires an additional argument to display images.
Attempts:
2 left
💡 Hint
Remember that 'imread' loads the image as a matrix and 'imshow' displays it as is.
❓ Predict Output
intermediate
2:00remaining
What happens if you try to display a grayscale image with imshow?
Given this MATLAB code snippet, what will be the output?
MATLAB
img = imread('cameraman.tif');
imshow(img);
AThe image is displayed but in color instead of grayscale.
BA figure window opens showing the grayscale 'cameraman.tif' image correctly.
CThe code throws an error because 'imshow' only supports color images.
DThe image is displayed upside down.
Attempts:
2 left
💡 Hint
Check how 'imshow' handles grayscale images.
🔧 Debug
advanced
2:00remaining
Why does this code produce an error when displaying an image?
Look at this MATLAB code. It tries to read and display an image but causes an error. What is the cause?
MATLAB
img = imread('nonexistent.jpg');
imshow(img);
AThe code is missing a semicolon after 'imread'.
B'imshow' cannot display images with the .jpg extension.
CThe variable 'img' is not defined before calling 'imshow'.
DThe file 'nonexistent.jpg' does not exist, so 'imread' throws an error.
Attempts:
2 left
💡 Hint
Check if the image file path is correct and the file exists.
🧠 Conceptual
advanced
2:00remaining
What is the class type of the variable after reading an image with imread?
After running this code, what is the data type (class) of the variable 'img'?
MATLAB
img = imread('peppers.png');
A'uint8' - an 8-bit unsigned integer array representing pixel values.
B'logical' - a boolean array representing the image.
C'char' - a character array containing image data.
D'double' - a double precision floating point array.
Attempts:
2 left
💡 Hint
Most images are stored as 8-bit unsigned integers for each color channel.
❓ Predict Output
expert
2:00remaining
What is the size of the image matrix after reading a color image?
Given this code, what will be the size of the variable 'img'?
MATLAB
img = imread('peppers.png');
size_img = size(img);
A[384 512] - height and width only, no color channels.
B[512 384 3] - width 512 pixels, height 384 pixels, 3 color channels (RGB).
C[384 512 3] - height 384 pixels, width 512 pixels, 3 color channels (RGB).
D[512 384] - width and height only, no color channels.
Attempts:
2 left
💡 Hint
Color images have three layers for red, green, and blue channels.

Practice

(1/5)
1. What is the primary function of imread in MATLAB when working with images?
easy
A. To display an image on the screen
B. To save an image to a file
C. To load an image file into MATLAB as a matrix
D. To convert an image to grayscale

Solution

  1. Step 1: Understand the purpose of imread

    imread reads image files and loads them into MATLAB as arrays (matrices) representing pixel data.
  2. Step 2: Differentiate from other functions

    imshow displays images, imwrite saves images, and conversion functions change image formats. Only imread loads images.
  3. Final Answer:

    To load an image file into MATLAB as a matrix -> Option C
  4. Quick Check:

    imread = load image [OK]
Hint: Remember: 'read' means load data into MATLAB [OK]
Common Mistakes:
  • Confusing imread with imshow
  • Thinking imread displays images
  • Assuming imread saves images
2. Which of the following is the correct syntax to display an image stored in variable img in MATLAB?
easy
A. imshow(img);
B. imread(img);
C. display(img);
D. show(img);

Solution

  1. Step 1: Identify the function to display images

    imshow is the MATLAB function designed to show images stored in variables.
  2. Step 2: Check syntax correctness

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

    imshow(img); -> Option A
  4. Quick Check:

    Display image = imshow [OK]
Hint: Use imshow to show images, not imread [OK]
Common Mistakes:
  • Using imread to display images
  • Using non-existent functions like show
  • Using display which does not render images
3. What will be the output of the following MATLAB code?
img = imread('peppers.png');
imshow(img);
size_img = size(img);

What does size_img contain?
medium
A. [384 512 3]
B. [512 384 3]
C. [384 512]
D. [512 384]

Solution

  1. Step 1: Understand image dimensions

    The image 'peppers.png' is a color image with height 384 pixels, width 512 pixels, and 3 color channels (RGB).
  2. Step 2: Interpret size output

    size(img) returns a vector with [rows, columns, color channels], so it is [384 512 3].
  3. Final Answer:

    [384 512 3] -> Option A
  4. Quick Check:

    Image size = [height width channels] = [384 512 3] [OK]
Hint: Remember size returns [rows columns channels] for images [OK]
Common Mistakes:
  • Swapping width and height values
  • Ignoring the third dimension for color channels
  • Assuming grayscale image size format
4. Identify the error in the following MATLAB code snippet:
img = imread('flower.jpg')
imshow(img)

What is the likely cause if MATLAB throws an error?
medium
A. Missing semicolon after imread line
B. Variable img is not defined
C. Using imshow without parentheses
D. Incorrect file extension or file not found

Solution

  1. Step 1: Check syntax correctness

    Missing semicolons do not cause errors, only suppress output. Parentheses are correctly used.
  2. Step 2: Consider file-related errors

    If MATLAB cannot find 'flower.jpg' or the extension is wrong, imread throws an error.
  3. Final Answer:

    Incorrect file extension or file not found -> Option D
  4. Quick Check:

    File path/name errors cause imread failure [OK]
Hint: Check file name and path carefully if imread errors [OK]
Common Mistakes:
  • Thinking missing semicolon causes error
  • Assuming imshow syntax is wrong
  • Ignoring file existence and path issues
5. You want to read an image and display only its red color channel in MATLAB. Which code snippet correctly achieves this?
hard
A. img = imread('image.png'); imshow(img(:,:,3));
B. img = imread('image.png'); imshow(img(:,:,1));
C. img = imread('image.png'); imshow(img(1,:,:));
D. img = imread('image.png'); imshow(img(:,1,:));

Solution

  1. Step 1: Understand image color channels

    In MATLAB, the third dimension of an RGB image is color channels: 1=Red, 2=Green, 3=Blue.
  2. Step 2: Extract and display red channel

    Using img(:,:,1) extracts the red channel matrix. Displaying it with imshow shows only red intensity.
  3. Final Answer:

    img = imread('image.png'); imshow(img(:,:,1)); -> Option B
  4. Quick Check:

    Red channel = third dimension index 1 [OK]
Hint: Use (:,:,1) to get red channel from RGB image [OK]
Common Mistakes:
  • Using wrong channel index for red
  • Trying to slice rows or columns incorrectly
  • Confusing color channel order
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