Challenge - 5 Problems
Image Resizing and Cropping Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of resizing an image with imresize
What is the size of the output image after running this code?
img = zeros(100, 200, 3); % black image 100x200 RGB resized_img = imresize(img, 0.5);
MATLAB
img = zeros(100, 200, 3); resized_img = imresize(img, 0.5); size_resized = size(resized_img);
Attempts:
2 left
💡 Hint
Think about what scaling by 0.5 means for each dimension.
✗ Incorrect
The original image is 100 rows by 200 columns. Scaling by 0.5 reduces each dimension by half, so the output size is 50 rows by 100 columns, with 3 color channels unchanged.
❓ Predict Output
intermediateResult of cropping an image with imcrop
What is the size of the cropped image after running this code?
img = zeros(300, 400, 3); cropped_img = imcrop(img, [50 100 149 99]);
MATLAB
img = zeros(300, 400, 3); cropped_img = imcrop(img, [50 100 149 99]); cropped_size = size(cropped_img);
Attempts:
2 left
💡 Hint
Remember that the rectangle vector is [xmin ymin width height].
✗ Incorrect
imcrop returns an image of size height by width (rows x columns). Here width=149 and height=99, so size is 99 rows by 149 columns with 3 channels.
🔧 Debug
advancedIdentify the error in this image resizing code
What error will this code produce?
img = imread('peppers.png');
new_img = imresize(img, [200]);MATLAB
img = imread('peppers.png'); new_img = imresize(img, [200]);
Attempts:
2 left
💡 Hint
Check the documentation for imresize size input format.
✗ Incorrect
imresize expects the size argument as a two-element vector [numrows numcols]. Passing a single number causes an error.
🧠 Conceptual
advancedUnderstanding aspect ratio preservation in resizing
Which option correctly resizes an image to have width 300 pixels while preserving the aspect ratio?
Attempts:
2 left
💡 Hint
NaN can be used to preserve one dimension automatically.
✗ Incorrect
Using [NaN 300] tells imresize to set width to 300 and adjust height to preserve aspect ratio. Other options either fix height or distort the image.
❓ Predict Output
expertOutput pixel value after cropping and resizing
Given this code, what is the value of pixel (1,1) in the final image?
img = uint8(reshape(1:16, 4, 4)); cropped = imcrop(img, [2 2 1 1]); resized = imresize(cropped, [2 2]); pixel_val = resized(1,1);
MATLAB
img = uint8(reshape(1:16, 4, 4)); cropped = imcrop(img, [2 2 1 1]); resized = imresize(cropped, [2 2]); pixel_val = resized(1,1);
Attempts:
2 left
💡 Hint
Check which pixels are selected by cropping and how resizing interpolates values.
✗ Incorrect
The crop [2 2 1 1] selects the pixel at (2,2) which is 6. Resizing the 1x1 image to 2x2 using bilinear interpolation fills all pixels with 6.
