Bird
Raised Fist0
MATLABdata~10 mins

Image resizing and cropping in MATLAB - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Image resizing and cropping
Load Image
↓
Resize Image
↓
Crop Image
↓
Display Result
↓
End
The image is first loaded, then resized to new dimensions, cropped to a specific area, and finally displayed.
Execution Sample
MATLAB
img = imread('peppers.png');
resized_img = imresize(img, [100 150]);
cropped_img = imcrop(resized_img, [20 10 50 40]);
imshow(cropped_img);
This code loads an image, resizes it to 100x150 pixels, crops a 50x40 pixel rectangle starting at (20,10), and shows the cropped image.
Execution Table
StepActionInput/StateOutput/StateNotes
1Load ImageNoneimg (size 384x512x3)Image loaded from file 'peppers.png'
2Resize Imageimg (384x512x3)resized_img (100x150x3)Image resized to 100 rows and 150 columns
3Crop Imageresized_img (100x150x3)cropped_img (40x50x3)Cropped rectangle at (20,10) width=50, height=40
4Display Imagecropped_img (40x50x3)Image shown on screenFinal cropped image displayed
5EndN/AN/AExecution complete
💡 All steps completed successfully; image resized and cropped as specified.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
imgundefined384x512x3 uint8 image384x512x3 uint8 image384x512x3 uint8 image384x512x3 uint8 image
resized_imgundefinedundefined100x150x3 uint8 image100x150x3 uint8 image100x150x3 uint8 image
cropped_imgundefinedundefinedundefined40x50x3 uint8 image40x50x3 uint8 image
Key Moments - 2 Insights
What is the size of the cropped image?
The crop rectangle [20 10 50 40] specifies top-left (20,10), width=50 columns, height=40 rows, so size is 40x50x3 (rows x columns x 3).
What happens if the resize dimensions are larger than the original image?
The image is enlarged by interpolation, increasing pixel count as in step 2. This can make the image bigger but may reduce sharpness.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the size of 'resized_img' after step 2?
A384x512x3
B100x150x3
C40x50x3
DUndefined
💡 Hint
Check the 'Output/State' column for step 2 in the execution_table.
At which step does the image get cropped?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look for the action 'Crop Image' in the execution_table.
If the crop rectangle changed to [1 1 150 100], what would be the size of cropped_img?
A99x149x3
B101x151x3
C100x150x3
DUndefined
💡 Hint
The crop rectangle specifies width=150 columns and height=100 rows for exact output size 100x150x3.
Concept Snapshot
Image resizing and cropping in MATLAB:
- Use imread() to load an image.
- Use imresize(image, [rows cols]) to resize.
- Use imcrop(image, [x y width height]) to crop.
- Crop size is height x width pixels (rows x columns).
- Display with imshow().
Full Transcript
This example shows how MATLAB processes image resizing and cropping step-by-step. First, the image is loaded from a file. Then it is resized to new dimensions using imresize, changing its pixel size. Next, a rectangular area is cropped from the resized image using imcrop, where the crop rectangle specifies the top-left corner and size. The cropped image is then displayed. The execution table tracks each step's input and output states, showing how variables change. Key moments clarify why crop size includes an extra pixel and how resizing affects image size. The visual quiz tests understanding of sizes and steps. This helps beginners see exactly how MATLAB handles image resizing and cropping visually and clearly.

Practice

(1/5)
1. What does the imresize function do in MATLAB when working with images?
easy
A. Converts an image to grayscale
B. Extracts a rectangular part of an image
C. Changes the size of an image by scaling or specifying new dimensions
D. Rotates an image by a specified angle

Solution

  1. Step 1: Understand the purpose of imresize

    The imresize function is used to change the size of an image either by scaling it up or down or by specifying exact new dimensions.
  2. Step 2: Differentiate from cropping

    Unlike cropping, which extracts a part of the image, resizing changes the entire image size.
  3. Final Answer:

    Changes the size of an image by scaling or specifying new dimensions -> Option C
  4. Quick Check:

    imresize = Resize image size [OK]
Hint: Resize changes whole image size, crop extracts part [OK]
Common Mistakes:
  • Confusing resize with crop
  • Thinking resize changes image colors
  • Assuming resize rotates image
2. Which of the following is the correct syntax to crop an image img to a rectangle starting at (50, 30) with width 100 and height 80 in MATLAB?
easy
A. cropped = imcrop(img, [50 30 100 80]);
B. cropped = imresize(img, [50 30 100 80]);
C. cropped = crop(img, 50, 30, 100, 80);
D. cropped = imcrop(img, [30 50 80 100]);

Solution

  1. Step 1: Recall imcrop syntax

    The imcrop function takes the image and a rectangle vector [x y width height]. Here, x=50, y=30, width=100, height=80.
  2. Step 2: Check option correctness

    cropped = imcrop(img, [50 30 100 80]); matches the correct syntax. cropped = imresize(img, [50 30 100 80]); uses imresize which is wrong. cropped = crop(img, 50, 30, 100, 80); uses a non-existent function. cropped = imcrop(img, [30 50 80 100]); swaps x and y and width and height incorrectly.
  3. Final Answer:

    cropped = imcrop(img, [50 30 100 80]); -> Option A
  4. Quick Check:

    imcrop(img, [x y width height]) = correct crop syntax [OK]
Hint: imcrop uses [x y width height] rectangle vector [OK]
Common Mistakes:
  • Swapping x and y coordinates
  • Using imresize instead of imcrop
  • Incorrect rectangle vector order
3. What will be the size of the output image resized_img after running the following MATLAB code?
img = imread('peppers.png');
resized_img = imresize(img, 0.5);
medium
A. Half the width and half the height of the original image
B. Double the width and double the height of the original image
C. Same size as the original image
D. Width halved but height unchanged

Solution

  1. Step 1: Understand imresize with scale factor

    Using imresize(img, 0.5) scales the image to 50% of its original size in both width and height.
  2. Step 2: Determine output size

    The output image will have half the width and half the height of the original image.
  3. Final Answer:

    Half the width and half the height of the original image -> Option A
  4. Quick Check:

    imresize with 0.5 scales both dimensions by 0.5 [OK]
Hint: Scale 0.5 halves both width and height [OK]
Common Mistakes:
  • Thinking only width or height changes
  • Assuming scale factor doubles size
  • Confusing imresize with imcrop
4. Identify the error in this MATLAB code snippet intended to crop an image img to a 100x100 square starting at (20, 20):
cropped_img = imcrop(img, [20 20 100]);
medium
A. The image variable name is incorrect
B. The rectangle vector is missing the height value
C. imcrop cannot crop square images
D. The starting coordinates should be (100, 100)

Solution

  1. Step 1: Check imcrop rectangle vector format

    The rectangle vector must have four values: [x y width height]. Here only three values are given.
  2. Step 2: Identify missing parameter

    The height value is missing, causing a syntax error or unexpected behavior.
  3. Final Answer:

    The rectangle vector is missing the height value -> Option B
  4. Quick Check:

    imcrop needs [x y width height] vector [OK]
Hint: Rectangle vector must have 4 values [OK]
Common Mistakes:
  • Providing only 3 values instead of 4
  • Confusing coordinates with size
  • Assuming imcrop auto-fills missing values
5. You want to resize an image img to exactly 200x150 pixels and then crop a 100x100 square from the center. Which MATLAB code sequence correctly achieves this?
hard
A. cropped = imcrop(img, [75 50 100 100]); resized = imresize(cropped, [200 150]);
B. resized = imresize(img, [200 150]); cropped = imcrop(resized, [50 25 100 100]);
C. cropped = imcrop(img, [50 25 100 100]); resized = imresize(cropped, [200 150]);
D. resized = imresize(img, [150 200]); cropped = imcrop(resized, [50 25 100 100]);

Solution

  1. Step 1: Resize image to 200x150 pixels

    Use imresize(img, [150 200]) to set width=200 and height=150.
  2. Step 2: Crop 100x100 square from center

    The center of resized image is at (100, 75). To crop 100x100, start at (50, 25) because 50 = 100 - 100/2 and 25 = 75 - 100/2.
  3. Step 3: Verify option correctness

    resized = imresize(img, [150 200]); cropped = imcrop(resized, [50 25 100 100]); matches the correct order and coordinates. resized = imresize(img, [200 150]); cropped = imcrop(resized, [50 25 100 100]); swaps width and height. Options C and D crop before resizing, which is not what is asked.
  4. Final Answer:

    resized = imresize(img, [150 200]); cropped = imcrop(resized, [50 25 100 100]); -> Option D
  5. Quick Check:

    Resize first, then crop center rectangle [OK]
Hint: Resize first, then crop center using half-dimensions [OK]
Common Mistakes:
  • Swapping width and height in resize
  • Cropping before resizing
  • Incorrect crop start coordinates
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