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.

조회 수: 5 (최근 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