how to do surface plot on my image?

13 views (last 30 days)
how can i do a surface 3d plot of a 2d matrix?
my code doesnt extract my image
z = peaks;
x = repmat(1:size(z,1),size(z,1),1);
y = x';
c = z - min(z(:));
c = c./max(c(:));
c = round(255*c) + 1;
cmap = colormap(jet(256));
c = cmap(c,:);
points = [x(:),y(:),z(:),c];
x = 1:50;
y = 1:50;
z = sin(x) + cos(y);
x = x';
y = y';
z = z';
figure
plot3(x,y,z,'.b');
grid on
title('Actual Data Points In 3D Space For Demonstration 1/2');
[x_3d,y_3d,z_3d] = vecsurf(x,y,z);
figure
surf(x_3d,y_3d,z_3d)
colormap(jet(256))
camlight right
lighting phong
title(sprintf('Demonstration 1:\nBasic Function Purpose - Organizing Vector Data In A 3D Surface Form'));
x = 1:50;
y = 1:50;
z = sin(x) + cos(y);
x = x';
y = y';
z = z';
[x_3d,y_3d,z_3d] = vecsurf(x,y,z,'MosaicPicture');
figure
surf(x_3d,y_3d,z_3d)
colormap(jet(256))
camlight right
lighting phong
title(sprintf(['Demonstration 2:\nComeplete Missing Data In The 3D\n' ...
'Surface Using The Mosaic Picture Option']));
clear
x =-8:0.5:8;
y =x';
[xi,yi] = meshgrid(x,y);
r = sqrt(xi.^2+yi.^2)+eps;
zi= sin(r)./r;
X(1,1:33) = xi(1:33,17);
Y(1,1:33) = yi(1:33,17);
Z(1,1:33) = zi(1:33,17);
X(1,34:66) = xi(17,1:33);
Y(1,34:66) = yi(17,1:33);
Z(1,34:66) = zi(17,1:33);
x = X';
y = Y';
z = Z';
figure
plot3(x,y,z,'.r');
title('Actual Data Points In 3D Space For Demonstration 3');
grid on
[x_3d,y_3d,z_3d] = vecsurf(x,y,z,'MosaicPicture');
figure
surf(x_3d,y_3d,z_3d);
colormap(hsv(256))
camlight right
lighting phong
title(sprintf(['Demonstration 3:\nComeplete Missing Data In The 3D\n' ...
'Surface Using The Mosaic Picture Option']));
narginchk(1,3);
if nargin == 3
% [x,y,z] = disperse(varargin);
x = varargin{1};
y = varargin{2};
z = varargin{3};
elseif nargin == 1
% Asssume matlab.graphics.primitive.Surface
h = varargin{1};
x = h.XData;
y = h.YData;
z = h.ZData;
else
error('surfarea accepts exactly one or three inputs.')
end
if isvector(x) && isvector(y)
[x,y] = meshgrid(x,y);
end
if ~isequal(size(x),size(y),size(z))
error('x, y, and z must all be the same size.')
end
v0 = cat(3, x(1:end-1,1:end-1), y(1:end-1,1:end-1), z(1:end-1,1:end-1));
v1 = cat(3, x(1:end-1,2:end ), y(1:end-1,2:end ), z(1:end-1,2:end ));
v2 = cat(3, x(2:end ,1:end-1), y(2:end ,1:end-1), z(2:end ,1:end-1));
v3 = cat(3, x(2:end ,2:end ), y(2:end ,2:end ), z(2:end ,2:end ));
a = v1 - v0;
b = v2 - v0;
c = v3 - v0;
A1 = cross(a,c,3);
A2 = cross(b,c,3);
A1 = sqrt(sum(A1.^2,3))/2;
A2 = sqrt(sum(A2.^2,3))/2;
areas = A1 + A2;
totalArea = sum(areas(:));
PW = ((v0 + v1 + v3)/3.*A1 + (v0 + v2 + v3)/3.*A2 )/totalArea;
centroid = sum(reshape(PW,numel(areas),3));
if exist('h','var')
h.UserData.totalArea = totalArea;
h.UserData.areas = areas;
h.UserData.centroid = centroid;
end
%%Function Option - Saving Time With Larg Calculations Using Representational Three Dimensional Surface Option
% - Using Progress Option To See Function Progress
% - Using Smooth Option To Create A Smooth Surface
clear
x = linspace(-3,3,1000000);
f = exp(-(x.^2));
X = x(1,1:1000000);
Y(1,1:1000000) = 0;
X(1,1000001:2000000) = 0;
Y(1,1000001:2000000) = x(1,1:1000000);
Z(1,1:1000000) = f;
Z(1,1000001:2000000) = f;
X = X';
Y = Y';
Z = Z';
figure
plot3(X,Y,Z,'.m');
grid on
title('Actual Data Points In 3D Space For Demonstration 6/7');
[x_3d,y_3d,z_3d] = vecsurf(X,Y,Z,'Representation',[60,60],'Progress');
figure
surf(x_3d,y_3d,z_3d);
colormap cool
title(sprintf(['Demonstration 6:\nSaving Time With Larg Calculations Using Representational\n' ...
'Three Dimensional Surface Option. Using Progress Option To See Function Progress.\n' ....
'For This Demonstration I Used 1000000 Sameples Of Data.']));
shading interp
lighting phong
light
material shiny
[x_3d,y_3d,z_3d] = vecsurf(X,Y,Z,'Representation',[60,60],'Progress','Smooth',[2,2,1]);
figure
surf(x_3d,y_3d,z_3d);
colormap cool
title(sprintf(['Demonstration 7:\nWhen Representational Three Dimensional Surface Option\n' ...
'Is Used, Smooth Option Can Be Used To Smoothness The Surface.']));
shading interp
lighting phong
light
material shiny
- Using Plot Option To Make The Function Plot The Result
clear
n1 = 15;
n2 = 15;
theta = rand(n1,1)*pi/2;
r = rand(1,n2);
x = cos(theta)*r;
y = sin(theta)*r;
x=x(:);
y=y(:);
X = [[0 0 1 1]';x;x;1-x;1-x];
Y = [[0 1 0 1]';y;1-y;y;1-y];
Z = sin(4*X+5*Y).*cos(7*(X-Y))+exp(X+Y);
vecsurf(X,Y,Z,'Representation',[60,60],'Plot',1,'Smooth',[2,2,1]);
title(sprintf('Demonstration 8:\nUsing Plot Option To Make The Function Plot The Result.'));
colormap(hot(256))
camlight right
lighting phong
vecsurf(X,Y,Z,'Representation',[60,60],'Plot',1,'Smooth',[2,2,2],'RepOneBoun',[0.2,0.7],'RepSecBoun',[0.2,0.7]);
title(sprintf(['Demonstration 9:\n Zoom In On Specific Parts Of The 3D Surface Without Losing Detail\n' ...
'Using Representational Three Dimensional Surface Grid Value Boundry.']));
colormap(hot(256))
camlight right
lighting phong

Accepted Answer

Image Analyst
Image Analyst on 2 Apr 2018
First, please read this link and format your code.

More Answers (1)

Image Analyst
Image Analyst on 2 Apr 2018
For example, see this:
% Code to display the orange MATLAB peak logo.
% Display the MATLAB logo in the current axes.
function Display_MATLAB_Logo()
try
caption = sprintf('A MATLAB Demo');
text(0.5,1.15, caption, 'Color','r', 'FontSize', 18, 'FontWeight','b', 'HorizontalAlignment', 'Center') ;
L = 40*membrane(1,25);
logoax = axes('CameraPosition', [-193.4013 -265.1546 220.4819],...
'CameraTarget',[26 26 10], ...
'CameraUpVector',[0 0 1], ...
'CameraViewAngle',9.5, ...
'DataAspectRatio', [1 1 .9],...
'Visible','off', ...
'XLim',[1 51], ...
'YLim',[1 51], ...
'ZLim',[-13 40], ...
'parent',gcf);
s = surface(L, ...
'EdgeColor','none', ...
'FaceColor',[0.9 0.2 0.2], ...
'FaceLighting','phong', ...
'AmbientStrength',0.3, ...
'DiffuseStrength',0.6, ...
'Clipping','off',...
'BackFaceLighting','lit', ...
'SpecularStrength',1, ...
'SpecularColorReflectance',1, ...
'SpecularExponent',7, ...
'Tag','TheMathWorksLogo', ...
'parent',logoax);
l1 = light('Position',[40 100 20], ...
'Style','local', ...
'Color',[0 0.8 0.8], ...
'parent',logoax);
l2 = light('Position',[.5 -1 .4], ...
'Color',[0.8 0.8 0], ...
'parent',logoax);
catch ME
errorMessage = sprintf('Error running DisplayLogo().\n\nThe error message is:\n%s', ...
ME.message);
errordlg(errorMessage);
end
return; % from DisplayLogo()

Community Treasure Hunt

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

Start Hunting!