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.
To vertically flip the image, we need to change the position of pixels. For example, the last row of the matrix will become the first row and the first who will become the last row, and so on. We can use the Matlab built-in function flip() to flip an image or a matrix.
Description. B = rot90( A ) rotates array A counterclockwise by 90 degrees. For multidimensional arrays, rot90 rotates in the plane formed by the first and second dimensions. B = rot90( A , k ) rotates array A counterclockwise by k*90 degrees, where k is an integer.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With