How to colour a 3D image with a continuous spectrum of colours
2 views (last 30 days)
Show older comments
I have a 3D image. I would like to shade it in a continuous colour. I.e. where the colour of the image is conditional on its location in 3D space. How can I do this for for my image? For example:
EG: http://4g-portal.com/wp-content/uploads/2013/03/Matlab.png EG2: http://www.math.utah.edu/lab/ms/matlab/gif/surface.png
My code to generate the image is below.
thanks
%%first download these two files:
%www.mathworks.co.uk/matlabcentral/fileexchange/30923-fast-stl-import-function
%www.cs.technion.ac.il/~gershon/EscherForReal/PenroseTriangleStl1.zip
myStr = 'D:\PenroseTriangle.stl';
[vertices, norms] = import_stl_fast(myStr,2);
n = length(vertices)/3;
X = reshape(vertices(:,1),3,n);
Y = reshape(vertices(:,2),3,n);
Z = reshape(vertices(:,3),3,n);
C = zeros(3,n);
%%Render
figure;
set(gcf, 'Color', 'white');
as = 0.95;
strt=1;
stop=6000;
patch(X(:,strt:stop),Y(:,strt:stop),Z(:,strt:stop),C(:,strt:stop),...
'FaceColor', [0 0 1], ...
'EdgeColor', 'none', ...
'FaceLighting', 'phong', ...
'AmbientStrength', as);
strt=6001;
stop=6299;
patch(X(:,strt:stop),Y(:,strt:stop),Z(:,strt:stop),C(:,strt:stop),...
'FaceColor', [0 1 0], ...
'EdgeColor', 'none', ...
'FaceLighting', 'phong', ...
'AmbientStrength', as);
strt=6300;
stop=6719;
patch(X(:,strt:stop),Y(:,strt:stop),Z(:,strt:stop),C(:,strt:stop),...
'FaceColor', [1 0 0], ...
'EdgeColor', 'none', ...
'FaceLighting', 'phong', ...
'AmbientStrength', as);
view([-135 35]);
axis('image');
axis('off');
% colormap('hsv');
% shading('interp');
camlight('headlight');
material('shiny');
0 Comments
Accepted Answer
Jason Nicholson
on 17 Jun 2014
Edited: Jason Nicholson
on 17 Jun 2014
The patch command's fourth argument, C, is the color of the that patch. Your C matrix is C=0. That is the problem. Instead use a function that defines the color matrix C. Matlab will scale C matrix to the default color values.
Try this:
% Euclidean distance in 3d
C = sqrt(X.^2 + Y.^2 + Z.^2);
Here is what it does to your code:
%%first download these two files:
%www.mathworks.co.uk/matlabcentral/fileexchange/30923-fast-stl-import-function
%www.cs.technion.ac.il/~gershon/EscherForReal/PenroseTriangleStl1.zip
myStr = 'PenroseTriangle.stl';
[vertices, norms] = import_stl_fast(myStr,2);
n = length(vertices)/3;
X = reshape(vertices(:,1),3,n);
Y = reshape(vertices(:,2),3,n);
Z = reshape(vertices(:,3),3,n);
C = sqrt(X.^2 + Y.^2 + Z.^2);
%%Render
figure;
set(gcf, 'Color', 'white');
as = 0.95;
strt=1;
stop=6000;
patch(X(:,strt:stop),Y(:,strt:stop),Z(:,strt:stop),C(:,strt:stop),...
'FaceColor', 'interp', ...
'EdgeColor', 'none', ...
'FaceLighting', 'phong', ...
'AmbientStrength', as);
strt=6001;
stop=6299;
patch(X(:,strt:stop),Y(:,strt:stop),Z(:,strt:stop),C(:,strt:stop),...
'FaceColor', 'interp', ...
'EdgeColor', 'none', ...
'FaceLighting', 'phong', ...
'AmbientStrength', as);
strt=6300;
stop=6719;
patch(X(:,strt:stop),Y(:,strt:stop),Z(:,strt:stop),C(:,strt:stop),...
'FaceColor', 'interp', ...
'EdgeColor', 'none', ...
'FaceLighting', 'phong', ...
'AmbientStrength', as);
view([-135 35]);
axis('image');
axis('off');
colormap('hsv');
camlight('headlight');
material('shiny');
6 Comments
Image Analyst
on 18 Jun 2014
OK - that sounds about right/typical (for everyone except Greg of course!) And too bad Walter (our most prolific contributor to date) seems to have fallen off the planet. Wonder what happened to him. One time before he left for about 3 months before coming back so maybe he'll return some day.
Cedric
on 18 Jun 2014
Edited: Cedric
on 18 Jun 2014
Yes I was wondering as well. Hopefully he is just on a sunny island and was wise enough not to take his keyboard! Speaking of ratio, I built the code attached lately, just for fun (well, I was waiting at a bus stop, so .. "for more fun than just waiting").
More Answers (0)
See Also
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!