How to use the size of a matrix while coding
4 views (last 30 days)
Show older comments
Hello there,
I am trying to representing a 2D image in 3D, " representing matrix with the RGB value as a z-axis while keeping the x- and y-axes".
I am woriking on the following code, but i have a problem while using the dimentions of the image.
let say [X,Y,D] = size(image);
how to use the X and Y inside my following code, i wrote something related to C, can someone help me make matlab accept the syntax?
rgb_image=imread('animalCell.jpg');
% Converting image format and getting colormap
[C,map] = rgb2ind(rgbim,32);
% Setting X,Y dimensions, where X and Y has the same dimensions.
[X,Y] = meshgrid(-floor(size(rgbim, 2)/2):floor(size(rgbim, 2)/2)-1, -floor(size(rgbim, 1)/2):floor(size(rgbim, 1)/2)-1);
% Dimensions X and Y must be the same as C. This can be accomplished by adjusting the meshgrid. Automatically done below.
i = 0;
j = 0;
while ( size{X}{1} ~= size{C}{1} ) % While dimensions of x different for X and C, Note X == Y.
i=i+1;
[X,Y] = meshgrid(-floor(size(rgbim, 2)/2):floor(size(rgbim, 2)/2) + 10 - j, -floor(size(rgbim, 1)/2):floor(size(rgbim, 1)/2) + 10 - i);
end
while ( size{X}{2} ~= size{C}{2} ) % While dimensions of x different for X and C, Note X == Y.
j=j+1;
[X,Y] = meshgrid(-floor(size(rgbim, 2)/2):floor(size(rgbim, 2)/2) + 10 - j, -floor(size(rgbim, 1)/2):floor(size(rgbim, 1)/2) + 10 - i);
end
% If error persists, print array dimensions of X, Y, and C
XC = isequal(size(X),size(C));
YC = isequal(size(Y),size(C));
if XC ~= 1 || YC ~= 1
fprintf('size(X): %i %i\n', size{X}{1}, size{X}{2});
fprintf('size(Y): %i %i\n', size{Y}{1}, size{Y}{2});
fprintf('size(C): %i %i\n', size{C}{1}, size{C}{2});
end
% Setting a reasonable restriction on the Z (C) axis, which in turn enables the exportation of a reasonable 3D object
k = max(C(:)) / max(X(:));
[Z] = double(C/k);
% Setting 3D figure output
figure;
surf(X, Y, Z, double(C)), shading flat;
colormap(map); % This command returns the original image colour
I am getting the error, Undefined variable "size" or class "size".
1 Comment
dpb
on 3 Aug 2019
What does
which -all size
return? Try
clear size
and retry..
But, you read in the image to the variable rgb_image but then use rgbim which isn't defined.
Also, you compute the size() of the same thing over and over...why not use
[nr,nc]=size(rgb_image);
and then use nr,nc where needed instead? Much easier code to read that way even if the JIT optimizer can avoid redoing the call every time...
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!