- Conv2: https://www.mathworks.com/help/matlab/ref/conv2.html
- meshgrid: https://www.mathworks.com/help/matlab/ref/interp2.html
Smoothing/interpolation of a 3D surface colormap
23 views (last 30 days)
Show older comments
I'm trying to smooth or interpolate away the "steps" building up to a intensity maximum, and at the same time preserving the shape of the surface so that the estimated width does not get shifted or changed. I have tried interpolating to a higher resolution using 2D spline, but the "steps" are still present and the estimated maxima region changes. Could someone please help me in smoothing/interpolating away those steps to make the surface look more continuous while conserving the surface shape?
The x, y and z data used to attain these results is attached. As always, many thanks in advance for any advice or help!!
Uninterpolated
Interpolated
Attempt
conversion = 1/49.0196; % in mm/pixel
new_xData = linspace(xData(1), xData(end), 10*length(xData)); % xData(1):0.1:xData(end);
new_yData = linspace(yData(1), yData(end), 10*length(yData)).'; % (yData(1):0.1:yData(end)).';
new_zData = interp2(xData, yData, zData, new_xData, new_yData, 'spline');
xData = new_xData; yData = new_yData; zData = new_zData;
h = figure();
surf(xData, yData, zData, 'LineStyle', 'none', 'FaceColor', 'interp');
xlim([xData(1) xData(end)])
ylim([yData(1) yData(end)])
zlim([0 ceil(max(zData(:)))]);
colormap(jet(4096))
xlabel('mm'); ylabel('mm'); zlabel('ln(Intensity)');
set(gca,'Ydir', 'reverse')
grid on
shading interp
view(3); axis vis3d; camlight;
% Change x and y tick labels from pixels to mm
addMM = @(x) sprintf('%.1f', x * conversion);
xticklabels(cellfun(addMM,num2cell(xticks'),'UniformOutput',false));
yticklabels(cellfun(addMM,num2cell(yticks'),'UniformOutput',false));
% Find maxium intensity region and plot it
hold on
maxValue = max(zData(:));
[rowsOfMaxes, colsOfMaxes] = find(zData == maxValue);
x_minind = min(colsOfMaxes(:));
x_maxind = max(colsOfMaxes(:));
p = zeros(1, 2);
p(1) = plot3(linspace(xData(x_minind), xData(x_minind), ...
length(yData)), yData, zData(:, x_minind), 'Linestyle', '-.', ...
'Color', 'black', 'LineWidth', 2.0);
p(2) = plot3(linspace(xData(x_maxind), xData(x_maxind), ...
length(yData)), yData, zData(:, x_maxind), 'Linestyle', '-.', ...
'Color', 'black', 'LineWidth', 2.0);
hold off
legend(p(1), 'Estimated width of maximum intensity', 'Location', 'North')
set(gca, 'FontSize', 14)
0 Comments
Answers (1)
Jaswanth
on 24 Oct 2024
Hi,
To achieve a smoother 3D surface colormap while preserving the shape of the data, one approach is to use Gaussian smoothing, which can reduce the steps in your data.
Refer to the following steps to integrate Gaussian smoothing into your existing MATLAB code:
% Gaussian smoothing parameters
sigma = 2; % Increased standard deviation for Gaussian kernel
kernelSize = 7; % Increased size of the Gaussian kernel
% Create a Gaussian kernel
[xKernel, yKernel] = meshgrid(-floor(kernelSize/2):floor(kernelSize/2), -floor(kernelSize/2):floor(kernelSize/2));
gaussianKernel = exp(-(xKernel.^2 + yKernel.^2) / (2 * sigma^2));
gaussianKernel = gaussianKernel / sum(gaussianKernel(:));
Convolving your ‘zData’ with a Gaussian kernel helps smooth the surface while maintaining its overall shape. You can adjust the ‘kernelSize’ and ‘sigma’ parameters to control the level of smoothing. A larger ‘sigma’ results in more smoothing.
% Apply Gaussian smoothing to zData multiple times
smoothed_zData = zData;
for i = 1:3 % Apply smoothing multiple times
smoothed_zData = conv2(smoothed_zData, gaussianKernel, 'same');
end
After smoothing, you can interpolate to a higher resolution using “interp2” to further enhance the visual quality of the surface.
% Interpolate to a higher resolution
[new_xGrid, new_yGrid] = meshgrid(linspace(min(xData), max(xData), 10 * length(xData)), ...
linspace(min(yData), max(yData), 10 * length(yData)));
new_zData = interp2(xData, yData, smoothed_zData, new_xGrid, new_yGrid, 'cubic');
Refer to the following MathWorks documentation to know more about the functions mentioned above:
I hope the solution provided above is helpful.
0 Comments
See Also
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!