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

3 ビュー (過去 30 日間)
Matlab2010
Matlab2010 2014 年 6 月 17 日
編集済み: Cedric 2014 年 6 月 18 日
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 Center および File ExchangeAudio and Video Data についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by