I have created a depth map using a time of flight sensor by measuring the distances to create a depth map I am not sure if it is correct.

1 次查看(过去 30 天)
clc;
clear all;
close all;
%% Step 1: Load the Data File
filePath = 'C:\Users\omeir\Downloads\time of flight data.csv'; % Update with the file path
data = readtable(filePath, 'VariableNamingRule', 'preserve');
% Extract relevant columns
Z = data.("Distance (mm)"); % Distance values
totalPoints = numel(Z); % Total number of data points
% Define target grid dimensions
M = 60; % Rows
N = 50; % Columns
New_L = M * N; % Total points required for reshaping
% Check if data needs padding or trimming
if totalPoints < New_L
Z_padded = [Z; zeros(New_L - totalPoints, 1)]; % Pad with zeros
else
Z_padded = Z(1:New_L); % Trim excess data
end
% Reshape depth data into a matrix
Z_matrix = reshape(Z_padded, [M, N]);
% Replace very small or invalid depth values
Z_matrix(Z_matrix < 50) = NaN; % Replace extremely small values with NaN
% Optional: Interpolate missing values
[X, Y] = meshgrid(1:N, 1:M); % Original grid
validMask = ~isnan(Z_matrix); % Mask of valid (non-NaN) points
Z_interpolated = griddata(X(validMask), Y(validMask), Z_matrix(validMask), X, Y, 'linear');
% Replace Z_matrix with interpolated data
Z_matrix = Z_interpolated;
%% Step 2: Visualize the Depth Map
figure;
surf(X, Y, Z_matrix, 'EdgeColor', 'none'); % Surface plot
shading interp; % Smooth shading
colormap('jet'); % Use a jet colormap
colorbar;
xlabel('X');
ylabel('Y');
zlabel('Depth');
title('Enhanced Depth Map');
view(3); % Set to 3D view
I have used the following csv file. It will be helpful if you could give feedbacks.

回答(1 个)

Mathieu NOE
Mathieu NOE 2024-12-5
编辑:Mathieu NOE 2024-12-5
hello
the code seems correct but the look of the 3D plot seems 'maybe) questionnable . I had a feeling that the Z values below 1500 should be removed to get a smoother surface without the "holes" .
So I made this very basic change :
Z_matrix(Z_matrix < 1500) = NaN; % Replace extremely small values with NaN
shouldn't it be like this ?
%% Step 1: Load the Data File
filePath = 'C:\Users\omeir\Downloads\time of flight data.csv'; % Update with the file path
data = readtable(filePath, 'VariableNamingRule', 'preserve');
% Extract relevant columns
Z = data.("Distance (mm)"); % Distance values
totalPoints = numel(Z); % Total number of data points
% Define target grid dimensions
M = 60; % Rows
N = 50; % Columns
New_L = M * N; % Total points required for reshaping
% Check if data needs padding or trimming
if totalPoints < New_L
Z_padded = [Z; zeros(New_L - totalPoints, 1)]; % Pad with zeros
else
Z_padded = Z(1:New_L); % Trim excess data
end
% Reshape depth data into a matrix
Z_matrix = reshape(Z_padded, [M, N]);
% Replace very small or invalid depth values
% Z_matrix(Z_matrix < 50) = NaN; % Replace extremely small values with NaN
Z_matrix(Z_matrix < 1500) = NaN; % Replace extremely small values with NaN
% Optional: Interpolate missing values
[X, Y] = meshgrid(1:N, 1:M); % Original grid
validMask = ~isnan(Z_matrix); % Mask of valid (non-NaN) points
Z_interpolated = griddata(X(validMask), Y(validMask), Z_matrix(validMask), X, Y, 'linear');
% Replace Z_matrix with interpolated data
Z_matrix = Z_interpolated;
%% Step 2: Visualize the Depth Map
figure;
surf(X, Y, Z_matrix, 'EdgeColor', 'none'); % Surface plot
shading interp; % Smooth shading
colormap('jet'); % Use a jet colormap
colorbar;
xlabel('X');
ylabel('Y');
zlabel('Depth');
title('Enhanced Depth Map');
view(3); % Set to 3D view
  2 个评论
Mathieu NOE
Mathieu NOE 2024-12-5
A tiny code modification : the "small value removal" is done directly on the 1D data , instead of doing it after 2D conversion, and I added a tad of smoothing (if that is what you need) using smoothn available here :
Result
code :
%% Step 1: Load the Data File
filePath = 'C:\Users\omeir\Downloads\time of flight data.csv'; % Update with the file path
data = readtable(filePath, 'VariableNamingRule', 'preserve');
% Extract relevant columns
Z = data.("Distance (mm)"); % Distance values
totalPoints = numel(Z); % Total number of data points
% Replace very small or invalid depth values
Z(Z < 1500) = NaN; % Replace extremely small values with NaN
% Define target grid dimensions
M = 60; % Rows
N = 50; % Columns
New_L = M * N; % Total points required for reshaping
% Check if data needs padding or trimming
if totalPoints < New_L
Z_padded = [Z; zeros(New_L - totalPoints, 1)]; % Pad with zeros
else
Z_padded = Z(1:New_L); % Trim excess data
end
% Replace very small or invalid depth values
Z_padded(Z_padded < 1500) = NaN; % Replace extremely small values with NaN
% Smooth a bit the data
Z_padded = smoothn(Z_padded,10000);
% Fex : https://fr.mathworks.com/matlabcentral/fileexchange/25634-smoothn?requestedDomain=
% Reshape depth data into a matrix
Z_matrix = reshape(Z_padded, [M, N]);
% Optional: Interpolate missing values
[X, Y] = meshgrid(1:N, 1:M); % Original grid
validMask = ~isnan(Z_matrix); % Mask of valid (non-NaN) points
Z_interpolated = griddata(X(validMask), Y(validMask), Z_matrix(validMask), X, Y, 'linear');
% Replace Z_matrix with interpolated data
Z_matrix = Z_interpolated;
%% Step 2: Visualize the Depth Map
figure;
surf(X, Y, Z_matrix, 'EdgeColor', 'none'); % Surface plot
shading interp; % Smooth shading
colormap('jet'); % Use a jet colormap
colorbar;
xlabel('X');
ylabel('Y');
zlabel('Depth');
title('Enhanced Depth Map');
view(3); % Set to 3D view

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Interpolation 的更多信息

产品


版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by