Ursa Major
Ursa Major

Reputation: 901

flip and rotate a color image in MATLAB

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.

Upvotes: 22

Views: 67079

Answers (4)

gnovice
gnovice

Reputation: 125874

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.

Upvotes: 24

Amro
Amro

Reputation: 124563

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

Upvotes: 22

Staszek
Staszek

Reputation: 951

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.

Upvotes: 0

Chethan
Chethan

Reputation: 213

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

Upvotes: 2

Related Questions