简体   繁体   中英

Image transpose in MATLAB

How do you transpose an image with RGB values? That is all pixels in line one should become pixels in column 1 and so on.

For a simple 1D case this is:

B=A';

I am after something like

img=imread('name.bmp');

R=R';
G=G';
B=B';

And then to have them assemble into a new image somehow.

You can use short & simple permute -

permute(img,[2 1 3])

Basically it exchanges the rows with columns keeping the third dimension intact, ie transposes each 3D slice. Advantage with this method would be that it would work even if you have 4 slices in the third dimension, for example for images with depth information usually stored as the 4th slice in dim3.

Just for completeness you can use imrotate and flipdim The order does matter though. Here we are flipping the columns (thats what the 2 is) and then rotating 90 degrees

imshow(imrotate(flipdim(im,2),90))

if we do the rotate first we would have to flip the rows (thats what the 1 is)

imshow(flipdim(imrotate(im,90),1))

Since "Transpose on ND array is not defined" in Matlab, you can transpose each channel separately:

[r,c,z] = size(im);
tmp = zeros(c,r,z);
for k=1:z
    tmp(:,:,k) = im(:,:,k)';
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM