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:
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 个评论
采纳的回答
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
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Performance 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!