We read images to use them in our programs. Displaying images helps us see the results and understand the data visually.
Reading and displaying images in MATLAB
Introduction
Syntax
MATLAB
img = imread('filename.ext');
imshow(img);imread loads the image file into a variable.
imshow shows the image in a figure window.
Examples
MATLAB
img = imread('peppers.png');
imshow(img);MATLAB
img = imread('cameraman.tif');
imshow(img);MATLAB
img = imread('flower.jpg');
figure; imshow(img);Sample Program
This program reads the 'peppers.png' image and displays it with a title above the image.
MATLAB
img = imread('peppers.png'); imshow(img); title('Displayed Image: Peppers');
Important Notes
Make sure the image file is in your current folder or provide the full path.
Supported image formats include PNG, JPG, TIFF, BMP, and GIF.
You can add a title or labels to the image using title and other plotting functions.
Summary
Use imread to load images into MATLAB.
Use imshow to display images in a figure window.
Always check the image file location to avoid errors.
Practice
1. What is the primary function of
imread in MATLAB when working with images?easy
Solution
Step 1: Understand the purpose of
imreadimreadreads image files and loads them into MATLAB as arrays (matrices) representing pixel data.Step 2: Differentiate from other functions
imshowdisplays images,imwritesaves images, and conversion functions change image formats. Onlyimreadloads images.Final Answer:
To load an image file into MATLAB as a matrix -> Option CQuick Check:
imread= load image [OK]
Hint: Remember: 'read' means load data into MATLAB [OK]
Common Mistakes:
- Confusing
imreadwithimshow - Thinking
imreaddisplays images - Assuming
imreadsaves images
2. Which of the following is the correct syntax to display an image stored in variable
img in MATLAB?easy
Solution
Step 1: Identify the function to display images
imshowis the MATLAB function designed to show images stored in variables.Step 2: Check syntax correctness
The syntaximshow(img);correctly calls the function with the image variable.Final Answer:
imshow(img); -> Option AQuick Check:
Display image =imshow[OK]
Hint: Use
imshow to show images, not imread [OK]Common Mistakes:
- Using
imreadto display images - Using non-existent functions like
show - Using
displaywhich does not render images
3. What will be the output of the following MATLAB code?
What does
img = imread('peppers.png');
imshow(img);
size_img = size(img);What does
size_img contain?medium
Solution
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).Step 2: Interpret
sizeoutputsize(img)returns a vector with [rows, columns, color channels], so it is [384 512 3].Final Answer:
[384 512 3] -> Option AQuick 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:
What is the likely cause if MATLAB throws an error?
img = imread('flower.jpg')
imshow(img)What is the likely cause if MATLAB throws an error?
medium
Solution
Step 1: Check syntax correctness
Missing semicolons do not cause errors, only suppress output. Parentheses are correctly used.Step 2: Consider file-related errors
If MATLAB cannot find 'flower.jpg' or the extension is wrong,imreadthrows an error.Final Answer:
Incorrect file extension or file not found -> Option DQuick Check:
File path/name errors causeimreadfailure [OK]
Hint: Check file name and path carefully if
imread errors [OK]Common Mistakes:
- Thinking missing semicolon causes error
- Assuming
imshowsyntax 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
Solution
Step 1: Understand image color channels
In MATLAB, the third dimension of an RGB image is color channels: 1=Red, 2=Green, 3=Blue.Step 2: Extract and display red channel
Usingimg(:,:,1)extracts the red channel matrix. Displaying it withimshowshows only red intensity.Final Answer:
img = imread('image.png'); imshow(img(:,:,1)); -> Option BQuick 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
10 questions ~5 mineasy hard
