Reputation: 21
I have a problem with a project using the flipdim
command in MATLAB. I need to use it to flip the red and green of a RGB image. I managed to flip the red and blue with flipdim(e,3);
but not sure how to solve this one. Can anyone help?
This is the code I have so far:
%call an image
d=uigetfile('*.jpg','choose an image file');
%read an image ito an array
e=imread(d);
%define plot 1
subplot(1,2,1)
%show image original image
imshow(e)
%hold figure
hold
%rotate original image 90 degreesR
l=flip(e(:,:,1:2),3);
%define plot 2
subplot(1,2,2)
%show altered image
imshow(l)
Upvotes: 2
Views: 286
Reputation: 104535
Doing flipdim(e,3)
will reverse all of the channels, so that's why the red and blue are flipped and the green stays untouched.... makes sense, because if red is the first slice, green is the second and blue is the third, flipping this would make blue the first, green the second and red the third.
If you want to flip the red and green channels only and use just flipdim
, use flipdim
in the third dimension as you stated, but only work on the first two channels. Supposing that e
is the original image, create a new image... call it l
like in your code, then apply flipdim
to the first two channels of this new image:
l = e;
l(:,:,1:2) = flipdim(l(:,:,1:2), 3);
... however, there isn't a need to use flipdim
at all.... I would simply be smart about indexing into the third dimension:
l = e(:,:,[2 1 3]);
That effectively permutes the red and green channels in the right order that you want.
Upvotes: 3