Bird
Raised Fist0
MATLABdata~10 mins

Reading and displaying images in MATLAB - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to read an image file named 'photo.jpg'.

MATLAB
img = [1]('photo.jpg');
Drag options to blanks, or click blank then click option'
Aimread
Bimshow
Cimwrite
Dimage
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'imshow' instead of 'imread' to load the image.
Using 'imwrite' which saves images, not reads them.
2fill in blank
medium

Complete the code to display the image stored in variable 'img'.

MATLAB
[1](img);
Drag options to blanks, or click blank then click option'
Aimshow
Bimwrite
Cimage
Dimread
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'imread' which reads images but does not display them.
Using 'image' which can display but is less common for images.
3fill in blank
hard

Fix the error in the code to read and display 'picture.png'.

MATLAB
img = imread('picture.png');
[1](img);
Drag options to blanks, or click blank then click option'
Aimwrite
Bimage
Cimread
Dimshow
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'imwrite' which saves images instead of displaying.
Using 'imread' again instead of displaying.
4fill in blank
hard

Fill both blanks to read 'img.jpg' and display it with a title 'My Image'.

MATLAB
img = [1]('img.jpg');
[2](img);
title('My Image');
Drag options to blanks, or click blank then click option'
Aimread
Bimshow
Cimage
Dimwrite
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'imwrite' instead of 'imread' to load the image.
Using 'image' instead of 'imshow' to display.
5fill in blank
hard

Fill all three blanks to read 'photo.tif', display it, and add axis labels.

MATLAB
img = [1]('photo.tif');
[2](img);
xlabel('[3]');
Drag options to blanks, or click blank then click option'
Aimread
Bimshow
CX Axis
Dimwrite
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'imwrite' instead of 'imread' to load the image.
Using 'image' instead of 'imshow' to display.
Forgetting to put the axis label as a string.

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