How to colour a 3D image with a continuous spectrum of colours

5 次查看(过去 30 天)
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:
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');

采纳的回答

Jason Nicholson
Jason Nicholson 2014-6-17
编辑:Jason Nicholson 2014-6-17
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 个评论
Image Analyst
Image Analyst 2014-6-18
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
Cedric 2014-6-18
编辑:Cedric 2014-6-18
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").

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graphics Performance 的更多信息

标签

尚未输入任何标签。

Community Treasure Hunt

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

Start Hunting!

Translated by