How can I convert a contour to a binary matrix?

11 Ansichten (letzte 30 Tage)
Goncalo Costa
Goncalo Costa am 14 Mär. 2024
I have the contour of a 3D surface at different levels and I have extracted its coordinates (for each level). I want to then convert this into a 30x30 matrix. I have tried this with the code below, but this did not work.
% PART 1
figure;
t = tiledlayout(1,1);
ax1 = axes(t);
%determining the levels for the data
levels = linspace(0.05, max(max(z_data)), 15);
% obtaining the coordinates and respective colours
for ii = 1:length(levels)
[CC_plt,c1] = contourf(ax1,x1,y1,z_data, [levels(ii) levels(ii)],'ShowText', 'off');
coords{ii} = CC_plt;
color_bar{ii} = c1;
end
clim([0.01 1]); zlim([0.01 1]);
colormap(ax1, 'magma');
% clean noise
for ii = 1:length(levels)
y_to_eliminate = coords{1,ii}(1,:) < 0.5 | coords{1,ii}(1,:) > 12;
coords{1,ii}(:, y_to_eliminate) = [];
x_to_eliminate = coords{1,ii}(2,:) > 12;
coords{1,ii}(:, x_to_eliminate) = [];
% PART 2
% translate these into a binary matrix
%chose 30x30 but I could make this any size, as long as the matrix is
%squared
BW{ii,:} = poly2mask(coords{1,ii}(1,:),coords{1,ii}(2,:),30,30);
end
The final BW, often gives matrices with very few points (especially for the lower levels).
I plotted the final coords to verify if it kept the same structure as when simply doing,
contourf(x1,y1,z_data,15); %x1 and y1 result from a meshgrid and z_data was obtained using griddata (interpolated)
, and it mostly does, but it is not perfect.
Another way I have tried to do this was, by keep part 1 and doing the following:
% PART 3
%find the minimum and max of all cells (not each cell, ALL CELLS)
coords_concat = coords{1,1};
for x = 2:length(levels)
coords_concat = cat(2, coords_concat, coords{1,x});
end
x_max = max(coords_concat(1,:));
x_min = min(coords_concat(1,:));
y_max = max(coords_concat(2,:));
y_min = min(coords_concat(2,:));
% Calculate the dimensions of the binary matrix
num_rows = y_max - y_min + 1;
num_cols = x_max - x_min + 1;
num_rows = round(num_rows);
num_cols = round(num_cols);
% Convert coordinates to indices in the binary matrix
for x = 1 : length(levels)
binary_matrix = zeros(num_rows, num_cols);
indices_x = round(coords{1,x}(1, :) - x_min) + 1;
indices_y = round(coords{1,x}(2, :) - y_min) + 1;
% Set the corresponding cells in the binary matrix to 1
binary_matrix(sub2ind(size(binary_matrix), indices_y, indices_x)) = 1;
bin{1,x} = binary_matrix;
end
But this also does not produce good results and results in a 12x12 matrix.
For better context: similar to another question of mine here on this platform, I have a 2D data matrix (30x30) and I have contourplots that I have aligned with the 2D data (based on shape).
I want to know the height/level of each data pixel. For this I got the shape's contours at 15 levels. I thought that if I could obtain these level contours in the shape of binary matrices, I could extract the 2D data for set heights.
Thanks for your help.

Antworten (1)

Dr.GADDALA JAYA RAJU
Dr.GADDALA JAYA RAJU am 15 Mär. 2024
To convert the coordinates of the contour of a 3D surface into a 30x30 matrix, you can follow these steps:
  1. Normalize the coordinates to fit within the range [0, 1].
  2. Create a 30x30 matrix.
  3. Map the normalized coordinates to the matrix indices.
  4. Set the corresponding matrix elements to indicate the presence of the contour.
Here's a Python code example illustrating these steps:
import numpy as np
# Example contour coordinates (replace with your own data)
contour_coords = np.array([[0.1, 0.2],
[0.2, 0.3],
[0.3, 0.4],
[0.4, 0.5]])
# Normalize coordinates to fit within [0, 1]
normalized_coords = (contour_coords - contour_coords.min(axis=0)) / (contour_coords.max(axis=0) - contour_coords.min(axis=0))
# Create a 30x30 matrix
matrix_size = 30
matrix = np.zeros((matrix_size, matrix_size))
# Map normalized coordinates to matrix indices
matrix_indices = (normalized_coords * (matrix_size - 1)).astype(int)
# Set matrix elements corresponding to contour coordinates
for idx in matrix_indices:
matrix[idx[0], idx[1]] = 1
print(matrix)
This code will convert the contour coordinates into a 30x30 matrix, where the elements corresponding to the contour will have a value of 1, and all other elements will be 0.
Make sure to replace contour_coords with your actual contour coordinates. If your contour has multiple levels, you can repeat this process for each level and combine the resulting matrices. Additionally, you may need to adjust the normalization and mapping process based on the range of your coordinates and the desired size of the matrix.

Kategorien

Mehr zu Contour Plots finden Sie in Help Center und File Exchange

Produkte


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by