MATLAB Answers

0

how to separate a 3D shape with repetitions to each repetition

Asked by hila naaman on 16 Sep 2018 at 7:05
Latest activity Commented on by hila naaman on 17 Sep 2018 at 6:18

Hello, I have a data of figure eight shape with repetitions (many times figure eight shape in the same data) [3X6140 array [x,y,z] for the rows]. I need to separate each figure eight for itself, I need to know the index for where the first shape start and where it ends and where the next one start and ends and so on. for example, I attach the mat file named matrix AND a figure of it.

Thanks.

  0 Comments

Sign in to comment.

1 Answer

Answer by Image Analyst
on 16 Sep 2018 at 14:29
 Accepted Answer

Try this:

clc;    % Clear the command window.
clearvars;
close all;  % Close all figures (except those of imtool.)
workspace;  % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
s = load('matrix.mat')
FE = s.FE
x = FE(1, :);
y = FE(2, :);
z = FE(3, :);
subplot(2, 2, 1);
plot3(x, y, z, 'b.');
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
zlabel('Z', 'FontSize', fontSize);
grid on;
title('Original Single Curve', 'FontSize', fontSize);
numPoints = length(x)
% Find distances from starting point.
distances = sqrt((x - x(1)).^ 2 + (y - y(1)).^ 2 +(z - z(1)).^ 2);
subplot(2, 2, 2);
plot(distances);
xlabel('Index', 'FontSize', fontSize);
ylabel('Distance from point 1', 'FontSize', fontSize);
% Find valleys by finding peaks of inverted signal.
[peakValues, indexesOfPeaks] = findpeaks(-distances);
peakValues = -peakValues; % Invert again.
% Add on very first and very last points
indexesOfPeaks = [1, indexesOfPeaks, numPoints];
peakValues = [distances(1), peakValues, distances(end)];
hold on;
plot(indexesOfPeaks, peakValues, 'rv');
grid on;
title('Distances from point #1', 'FontSize', fontSize);
% Extract out each part of the curve
subplot(2, 2, 3:4);
curveCount = 0;
for k = 1 : 2 : length(indexesOfPeaks) - 1
	index1 = indexesOfPeaks(k);
	index2 = indexesOfPeaks(k + 2) - 1;
	fprintf('Extracting points %d to %d.\n', index1, index2);
	xSingles{k} = x(index1:index2);
	ySingles{k} = y(index1:index2);
	zSingles{k} = z(index1:index2);
	plot3(xSingles{k}, ySingles{k}, zSingles{k}, '-', 'LineWidth', 2);
	hold on;
	curveCount = curveCount + 1;
end
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
zlabel('Z', 'FontSize', fontSize);
caption = sprintf('%d curves', curveCount);
title(caption, 'FontSize', 20);
grid on;

  1 Comment

Sign in to comment.