Convert xy Level Coordinates to Matrix, which data is negative and non-integer numbers

2 次查看(过去 30 天)
My goal is to get the edge of the light spot according to the density of the contour line. After using the getContourLineCoordinates function to the contour matrix which is return by the contour function, I get the X,Y and Level values (Contour height value) of the contour, I want to convert these values to matrix just according the X,Y,Level values(no interpolation required), and let the matrix display in imagesc has the same effect as contour.

回答(1 个)

Shubham Dhanda
Shubham Dhanda 2023-6-13
Hi,
I understand that you want to create a matrix based on X, Y, & Level values and display the matrix usingimagescwith same colour as the contour. From the information provided by you, I am assuming the following values of X, Y and Level as an example.
X = [0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0]
Y = [0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0]
Level = [0.4, -0.1, -0.3, -0.2, 0.1, 0.3, 0.7, 0.9, 0.6, 0.2]
Below is the MATLAB code to create a matrix and display the matrix using
imagesc:
X = [0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0]
X = 1×10
0.2000 0.4000 0.6000 0.8000 1.0000 1.2000 1.4000 1.6000 1.8000 2.0000
Y = [0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0]
Y = 1×10
0.2000 0.4000 0.6000 0.8000 1.0000 1.2000 1.4000 1.6000 1.8000 2.0000
Level = [0.4, -0.1, -0.3, -0.2, 0.1, 0.3, 0.7, 0.9, 0.6, 0.2]
Level = 1×10
0.4000 -0.1000 -0.3000 -0.2000 0.1000 0.3000 0.7000 0.9000 0.6000 0.2000
% Determine the size of the matrix
x_min = floor(min(X));
x_max = ceil(max(X));
y_min = floor(min(Y));
y_max = ceil(max(Y));
matrix_size = [y_max - y_min + 1, x_max - x_min + 1];
% Create the matrix and initialize it with NaN values
matrix = NaN(matrix_size);
% Convert X,Y values to linear indices
x_int = round((X - x_min) + 1); % Convert X values to integers
y_int = round((Y - y_min) + 1); % Convert Y values to integers
coords = [x_int(:), y_int(:)]; % Combine the X,Y values into a matrix
linear_indices = sub2ind(matrix_size, coords(:,2), coords(:,1)); % Compute the linear indices
% Fill in the values at the corresponding indices in the matrix
matrix(linear_indices) = Level(:);
% Display the matrix using imagesc
imagesc(matrix);
colorbar;
  1 个评论
xiaoxiao liang
xiaoxiao liang 2023-6-14
Thanks for your help, I solved the probelm yesterday, my solution is similar to your, but I used the round the funtion to reduce the number of decimal places of the contour matrix,and then used the linpace function to produce equal interval x,y values with the same decimal places of the contour matrix after dealing with round function, finally using the find fuction to obtion the correspondeing index.
The specific code is as follows.
[C,H]=contour(im3,1);
contourTable = getContourLineCoordinates(C);
y_0= contourTable.Y;
x_0 = contourTable.X;
y_round=round(y_0,1);
x_round=round(x_0,1);
m=(max(y_round)-min(y_round))/0.1;
n=(max(x_round)-min(x_round))/0.1;
y= round(transpose(linspace(min(y_round), max(y_round),m)),1);
x =round(transpose(linspace(min(x_round), max(x_round),n)),1)
matrix = zeros(length(y), length(x));
for i = 1:height(contourTable)
y_idx= find(y == y_round(i));
x_idx = find(x == x_round(i));
matrix(y_idx, x_idx) = contourTable.Level(i);
end

请先登录,再进行评论。

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by