MATLAB: “Data dimensions must agree” Error

[code]im = im2double(imread('rice.png'));
[X Y]= meshgrid(1:size(im,1),1:size(im,2));
surf(zeros(size(im)),X,Y,im,'EdgeColor','none');[/code]
when i run this script it worked me fine but when i tried to change the image to RGB image it gives me this 2 errors
??? Error using ==> surf at 78 Data dimensions must agree.
Error in ==> CoOrdinating at 6 surf(zeros(size(im)),X,Y,im,'EdgeColor','none');
i tried to convert the image to grayscale but it didn't work with me and gave me the same errors
any help ?

Best Answer

Here is a demo that works:
rgb_img = imread('ngc6543a.jpg');
imshow(rgb_img)
I = .2989*rgb_img(:,:,1)...
+.5870*rgb_img(:,:,2)...
+.1140*rgb_img(:,:,3);
figure;
imshow(I)
I = double(I);
[X,Y]= meshgrid(1:size(I,1),1:size(I,2));
figure;
surf(zeros(size(I')),X,Y,I','EdgeColor','none');
colormap('gray')
Probably you didn't take the transpose of I in the surf command. I don't know why you need to do this, but you do.
Related Question