MATLAB Answers

How can I create a surface with points distributed randomly ?

32 views (last 30 days)

Accepted Answer

Walter Roberson
Walter Roberson on 6 Apr 2019
N = 25;
x = rand(1,N) * 150; %wider than it is tall
y = rand(1,N) * 50;
pointsize = 20;
scatter(x, y, pointsize, 'filled')

More Answers (1)

Image Analyst
Image Analyst on 6 Apr 2019
Try scatteredInterpolant()
Image Analyst
Image Analyst on 10 Apr 2019
Try this demo:
% Demo to show how scatteredInterpolant works to create a complete image out of arbitrarily placed sample points.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
numTrainingPoints = 50; % Number of points we'll have to try to estimate our image everywhere.
imageWidth = 300;
% Create sample surface
z = peaks(imageWidth);
[rows, columns] = size(z);
% Plot it as a flat image.
subplot(2, 2, 1);
imshow(z, []);
title('Original Image with Training Points in Red', 'FontSize', fontSize, 'Interpreter', 'None');
% Show it as a perspective 3-D-ish image.
subplot(2, 2, 3);
surf(z, 'EdgeColor', 'none');
title('Original Surface', 'FontSize', fontSize, 'Interpreter', 'None');
% Create sample coordinates
xi = imageWidth * rand(numTrainingPoints, 1);
yi = imageWidth * rand(numTrainingPoints, 1);
% Plot these over the image
subplot(2, 2, 1);
hold on;
plot(xi, yi, 'r.', 'MarkerSize', 15);
% Get z values. For this demo, let's get them from the peaks image.
% Then we'll try to recreate the peaks shape from the few points in x and y.
for k = 1 : length(xi)
zi(k) = z(ceil(yi(k)), ceil(xi(k)));
zi = zi'; % Make zi a column vector like scatteredInterpolant requires.
%================================ MAIN PART RIGHT HERE ==============================================
% Make the scattered interpolant.
F = scatteredInterpolant(xi, yi, zi)
% Get a grid of points at every pixel location in the RGB image.
[xGrid, yGrid] = meshgrid(1:columns, 1:rows);
xq = xGrid(:);
yq = yGrid(:);
% Evaluate the interpolant at query locations (xq,yq).
vq = F(xq, yq);
fittedImage = reshape(vq, rows, columns);
%================================ END OF MAIN PART ==============================================
% Plot it as a flat image.
subplot(2, 2, 2);
imshow(fittedImage, []);
title('Interpolated Image with Training Points in Red', 'FontSize', fontSize, 'Interpreter', 'None');
% Plot the training points over the image.
hold on;
plot(xi, yi, 'r.', 'MarkerSize', 15);
% Show it as a perspective 3-D-ish image.
subplot(2, 2, 4);
surf(fittedImage, 'EdgeColor', 'none');
title('Interpolated Surface', 'FontSize', fontSize, 'Interpreter', 'None');
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by