I am plotting a lot of surfaces, and the code is very slow, is there a faster/ more efficient way to plot this in a 3d graph?

58 visualizzazioni (ultimi 30 giorni)
I am currently plotting 150 surfaces of 550 by 550. This is very slow in rendering and loading.
axis_images= handles.figure.CurrentAxes;
for k=1:length(slices) %plots the registration slices
%the next line makes a great big box, handles, which contains all the
%plot data, but in a cell array so must be refrenced by using handles.plot{1}
%instead of handles.plot1 or handles.plot(1)
hold on
handles.registration_slices{k}= surf(X,Y,Z.*(slices(k)), ...
'CData',image(:,:,:,k,:), 'CDataMapping','direct', ...
'EdgeColor','none', 'FaceColor', 'texturemap', ...
'FaceAlpha','texturemap','AlphaData',masks(:,:,:,k));
%FaceColor determines the color with the data in Cdata
%FaceAlpha determines the opacity of a given slice based on the
%texturemap given in AlphaData
end
freezeColors;
view(3), box on, %axis tight square
pause(1);
for i = 1:size(atlas_slices,3) %creates the atlas masks
atlas_mask=atlas_slices(:,:,i);
atlas_mask(atlas_mask<=50)=0;
atlas_mask(atlas_mask>50)=120;
atlas_masks=cat(4,atlas_masks,atlas_mask);
end
% Y_scaled=scale(Y,atlas_scale);
for k=1:length(slices) %plots the atlas slices
hold on
handles.atlas_slices{k}=surf(X+Atlas_x_correction,Y+Atlas_y_correction,Z.*(slices(k)-0.5), ...
'CData',atlas_slices(:,:,k), 'CDataMapping','direct', ...
'EdgeColor','none','FaceColor', 'texturemap', ...
'FaceAlpha','texturemap','AlphaData',atlas_masks(:,:,:,k));
end
colormap(handles.figure.CurrentAxes, gray);
%axis equal;
%axis(axis_atlas,'off')

Risposta accettata

dpb
dpb il 3 Ott 2025 alle 15:42
Modificato: dpb il 3 Ott 2025 alle 18:04
>> N=500; M=150;
>> [X,Y]=meshgrid(1:N,1:N);
>> Z=repmat(peaks(N),1,1,M);
>> tic;for i=1:size(Z,3), surf(X,Y,Z(:,:,i),Z(:,:,i),'EdgeColor','none'); if i==1, hold on, end, end, toc
Elapsed time is 0.788279 seconds.
>>
The faster version would be to use surface instead, the primitive that doesn't do the new plot overhead every time...
>> figure
>> tic;for i=1:size(Z,3), surface(X,Y,Z(:,:,i),Z(:,:,i),'EdgeColor','none'); end, toc, view(3)
Elapsed time is 0.451042 seconds.
>>
% >> ver
% -----------------------------------------------------------------------------------------------------
% MATLAB Version: 9.11.0.2358333 (R2021b) Update 7
% MATLAB License Number: xxxx
% Operating System: Microsoft Windows 10 Pro Version 10.0 (Build 19045)
% Java Version: Java 1.8.0_202-b08 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
% -----------------------------------------------------------------------------------------------------
% MATLAB Version 9.11 (R2021b)
% Curve Fitting Toolbox Version 3.6 (R2021b)
% MATLAB Compiler Version 8.3 (R2021b)
% Optimization Toolbox Version 9.2 (R2021b)
% Signal Processing Toolbox Version 8.7 (R2021b)
% Statistics and Machine Learning Toolbox Version 12.2 (R2021b)
% >>
Without all the ancillary stuff your code references, I didn't try to add the special effects.
And, cutting the size of the array x,y dimension in half was reflected linearly in the time; might want to investigate if there really is any significant difference if were to decimate the size somewhat.
I did observe a noticeable time lag of 10-15 seconds, maybe, from the time the command window displayed the calculational timing result before the final image appeared on the screen so there is some noticeable rendering time for so many objects to be drawn and displayed.
  3 Commenti
dpb
dpb il 3 Ott 2025 alle 16:49
Modificato: dpb il 3 Ott 2025 alle 17:25
The code timing itself or the rendering delay after the code is complete?
Have you experimented with the size to see if there might be a geometric growth or other nonlinear change in time just as exploratory exercise?
Have you run the most basic of test as my demo code without the additional niceties to see if some or one of those features may be the prime culprit?
I suspect, however, it is an issue mostly related to the new graphics system and using webgl; if none of the above work to resolve the issue, submit a support request.at <Product Support Page>
Rushan
Rushan il 16 Ott 2025 alle 10:32
Dear dpd,
sorry for the late response, I decided to tackle this a completely different way, since I wanted to view a 3D model, so I used the veiwer3D from the imaging processing toolbox, This greatly sped up the process!
Thank you for your time, but I am going to close this question
Kind regard,

Accedi per commentare.

Più risposte (0)

Prodotti


Release

R2025b

Community Treasure Hunt

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

Start Hunting!

Translated by