How do I flip a color image (RGB) in MATLAB?
T开发者_C百科he fliplr
does not seem to work without losing the color contents, as it only deals with 2D.
As well, the imrotate
may not rotate color images.
The function flipdim
will work for N-D matrices, whereas the functions flipud
and fliplr
only work for 2-D matrices:
img = imread('peppers.png'); %# Load a sample image
imgMirror = flipdim(img,2); %# Flips the columns, making a mirror image
imgUpsideDown = flipdim(img,1); %# Flips the rows, making an upside-down image
NOTE: In more recent versions of MATLAB (R2013b and newer), the function flip
is now recommended instead of flipdim
.
An example:
I = imread('onion.png');
I2 = I(:,end:-1:1,:); %# horizontal flip
I3 = I(end:-1:1,:,:); %# vertical flip
I4 = I(end:-1:1,end:-1:1,:); %# horizontal+vertical flip
subplot(2,2,1), imshow(I)
subplot(2,2,2), imshow(I2)
subplot(2,2,3), imshow(I3)
subplot(2,2,4), imshow(I4)
imrotate
rotates color images
B = IMROTATE(A,ANGLE) rotates image A by ANGLE degrees in a
counterclockwise direction around its center point.
I know it is late, but since flipdim is now depreciated, other answers are not valid anymore. You could use flip, or do it in other, smart way:
I = imread('onion.png');
% flip left-right, or up-down:
Iflipud = flip(I, 1)
Ifliplr = flip(I, 2)
% or:
Iflipud = I(size(I,1):-1:1,:,:);
Ifliplr = I(:,size(I,1):-1:1,:);
% flip both left-right, and up-down, stupid way:
Iflipboth = I(size(I,1):-1:1,size(I,1):-1:1,:);
% flip both left-right, and up-down, smart way:):
Iflipboth = imrotate(I, 180)
As already pointed, imrotate deals with color images as well as with greyscale.
精彩评论