Crop an image in half without loops or built in functions

15 views (last 30 days)
I'm supposed to crop a given image and remove the top half. So, the first quarter rows from the top and first quarter columns from the left. The resulting image should have the size floor(height/2) by floor(width/2). NO LOOPS or built-in functions are allowed.
I went about it the following way:
function halved_image = half_the_image(img)
[height width] = size(img)
halved_image = img(floor(height/4):height, floor(width/4):width);
end
What's happening is that the resulting image seems to clone itself? It also loses all of its color.
  1 Comment
Steven Lord
Steven Lord on 5 Sep 2022
Your code fails to satisfy your stated requirements.
which size
built-in (/MATLAB/toolbox/matlab/elmat/size)
which /
built-in (/MATLAB/toolbox/matlab/ops/mrdivide)
which floor
built-in (/MATLAB/toolbox/matlab/elfun/@char/floor) % char method
which :
built-in (/MATLAB/toolbox/matlab/ops/@char/colon) % char method
All four of those commands are built-in functions used in your code.

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 5 Sep 2022
[height width] = size(img)
sz1,...,szN — Dimension lengths listed separately
Dimension lengths listed separately, returned as nonnegative integer scalars separated by commas.
  • When dim is not specified and fewer than ndims(A) output arguments are listed, then all remaining dimension lengths are collapsed into the last argument in the list. For example, if A is a 3-D array with size [3 4 5], then [sz1,sz2] = size(A) returns sz1 = 3 and sz2 = 20.
You are using RGB images, which are 3 dimensional, but your size() is only expecting two outputs.
halved_image = img(floor(height/4):height, floor(width/4):width)
and you are only indexing two dimensions in taking the half-image.
  3 Comments
Poojha Palle
Poojha Palle on 5 Sep 2022
Edited: Poojha Palle on 5 Sep 2022
I see, thanks! Although, how does this explain the change from color to grayscale?
EDIT:
Realized you already answered that question. I'm actually having another issue with dimensions. So when checking with my tester, my dimensions are always one off (either one too many rows or one too few columns).
halved_image = img(floor(height/2):height, floor(width/2):width, :);
Does this not actually give you a resulting halved image? Am I not seeing something?
Walter Roberson
Walter Roberson on 5 Sep 2022
Suppose the image has 11 rows. Then floor(height/2) would be floor(11/2) which would be 5. You would then be indexing 5:11 which is 6 rows not 5.
You need ceil() not floor() in this context.

Sign in to comment.


Image Analyst
Image Analyst on 5 Sep 2022
It's because you did this:
[height width] = size(img)
Never do that, especially when you don't know if img will be color or gray scale.. Why not? See Steve's blog:
Do this:
[rows, columns, numberOfColorChannels] = size(img);

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!