what (:, :, :) syntax does?

18 views (last 30 days)
Emmanuel Arias Polanco
Emmanuel Arias Polanco on 24 May 2022
Edited: Jonas on 24 May 2022
Hi, I'm new in MatLab and my teacher gave us a code to crop images and split them into his RGB components. This section it's suposed to crop the image (I1) I think (am I right?) and display the cropped image (I2)
u=round(ginput(2));
P12 = u(1,2);
P22 = u(2,2);
P11 = u(1,1);
P21 = u(2,1);
I2=I1(P12:P22,P11:P21,:);
figure
imshow(I2)
axis equal
I think I know what the first line does, it gets two inputs from the mouse and then round it to the nearest integer to finally save them into a vector 'u' right?, in that case, my doubt is what those u(1,2) means, how do they work? what are those 1 and 2 into them? And, how that syntax I2=I1(P12:P22,P11:P21,:) works? why is only the : in the final?
I know I could get the answer by reading the documentation, but what documentation do I have to read? Thank you in advance.
  2 Comments
Voss
Voss on 24 May 2022
@Emmanuel Arias Polanco: Here is some documentation you can read that will answer all of those questions:

Sign in to comment.

Accepted Answer

Jonas
Jonas on 24 May 2022
Edited: Jonas on 24 May 2022
u=round(ginput(2));
gets two points with rounded x and y values. the values are stored as [x1 y1; x2 y2]
it follows that
P12 = u(1,2); % first y coordinate
P22 = u(2,2); % second y coordinate
P11 = u(1,1); % first x coordinate
P21 = u(2,1); % second x coordinate
following
I1(firstY:secondY,firstX:secondX,:)
which cuts in horizontal and vertical between the specified x and y values.
the : in the third dimension means that all entries along the third dimension are kept. the object has three dimensions since it has 3 layers per point, containing the rgb values of the colored pixel
so the given code just cuts the image but does not split into the rgb contents, since all entries are kept along the 3rd dimension.
if you want to see the split components, e.g. to see only the red channel also displayed in red, use e.g.
I3=I2; % crrate copy of the cut image part
I3(:,:,2:3)=0; % set all values of the g and b layer to 0
imshow(I3);

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!

Translated by