How to solve "Error using ' Transpose on ND array is not defined. Use PERMUTE instead." ?

i have convert image from rgb to gray using this code :
axes(handles.axes3)
cropface=imcrop(X,face);
newsize=imresize(cropface,[151 151]);
grayscale=rgb2gray(newsize);
and then i save that image using this code :
img = getframe(gca);
[filename2,pathname2] = uiputfile(...
{'*.jpg','jpeg image(*.jpg)';
'*.*','All file(*.*)'},...
'Save Image','H:\SKRIPSI\Citra Latih\');
imwrite(img.cdata,fullfile(pathname2,filename2));
then I intend to creating the image matrix X using grayscale image that I have and I save with the code above, use the following code:
X = [];
for i = 1 : imgcount
str = strcat(datapath,'\',int2str(i),'.jpg');
img = imread(str);
img = rgb2gray(img);
[r c] = size(img);
temp = reshape(img',r*c,1);
I want to ask is why should I rgbgray code reuse in the process of creating the image matrix X, whereas the image that I use have grayscale. when I remove the rgb2gray code, then I see an error like this:

 Accepted Answer

A rgb image has three dimensions: pixel rows, pixel columns, colour planes. It is therefore a 3D matrix.
A greyscale image has two dimensions: pixel rows, pixel columns. There is only one colour plane. It is therefore a 2D matrix.
As per the error message, transpose (the ') is only defined for 2D matrices, since for more dimensions, it's not clear which two dimensions you want to swap. To do the same for a colour image you need to use permute and tell permute you want to transpose the 1st and 2nd dimension. So:
permute(rgbimg, [2 1 3])
will transpose the rows and columns of a colour image. Note that it will also work for greyscale images.
edit: by the way, for the same reason (3 dimensions), your
[r, c] = size(img);
is not going to work for colour images. c will be three times as big as it will the number of columns time the number of colour planes. You need to specifically ask size for three outputs even if you don't use the third one (use a ~ to ignore it).
[r, c, ~] = size(img);

7 Comments

Can you tell me where should i put your code?
Thank you so much for your explanation sir, that's really help me
excuse me, i want to ask, i have saved grayscale images before, but why that still have 3D matrix and not 2D matrix although the image has been my saved as grayscale?
Do you mean that you've saved a greyscale image and when you've loaded it back into matlab, it came back as a 3D matrix?
If that is the case, it is because the format that you've saved your image in does not support greyscale (for example, BMP or JPG). In which case, the image had to be converted to rgb, with all three colour channels equal, so the image still appear grey.
You can go back to a 2D matrix by using any of the colour channel:
img = imread('somegreyimage.jpg');
img = img(:, 1); %if it's a grey image, img(:, 1) is the same as img(:, 2) and img(:, 3)
or using rgb2gray.
PNG is one of the rare format that truly support grey images.
Yes i mean that i've saved a greyscale image and when i've loaded it back into matlab, it came back as a 3D matrix. So it because i save that in .jpg format? what if i saved it into .tif format? is it still came back as a 3D matrix?
I don't know about TIF but as I said, you won't have this problem with PNG. I recommend you use PNG, it's one of the best format
In any case, never use JPG for images that you want to continue processing. JPG uses a lossy compression meaning every time you save the image, some information is lost.

Sign in to comment.

More Answers (1)

My guess is that your variable "img" has more than 2 dimensions. Identify which 2 dimensions of "img = imread(str);" are the ones you actually want.

2 Comments

It's not a guess! a rgb image always have three dimensions.
excuse me, i want to ask, i have saved grayscale images before, but why that still have 3D matrix and not 2D matrix although the image has been my saved as grayscale?

Sign in to comment.

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!