22

How do I flip a color image (RGB) in MATLAB? The 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.

Amro
  • 123,847
  • 25
  • 243
  • 454
Ursa Major
  • 851
  • 7
  • 25
  • 47
  • 1
    I have no problems rotating color images with `imrotate` (R2013a). – Cape Code Dec 30 '13 at 13:43
  • `fliplr(img)` is equivalent to `flip(img, 2)`. And it deal not only with 2D arrays. For multidimensional arrays, `fliplr` operates on the planes formed by the first and second dimensions. – Temak Sep 28 '15 at 17:18

4 Answers4

24

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.

gnovice
  • 125,304
  • 15
  • 256
  • 359
22

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)

alt text

Amro
  • 123,847
  • 25
  • 243
  • 454
2

imrotate rotates color images B = IMROTATE(A,ANGLE) rotates image A by ANGLE degrees in a counterclockwise direction around its center point.

Chethan
  • 213
  • 3
  • 7
  • 19
0

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.

Staszek
  • 849
  • 11
  • 28