Volumetric 3D data plotting from 2D map in MATLAB?

旧巷老猫 提交于 2021-01-29 07:19:06

问题


I have a heat map

and want to convert this 2D matrix to a 3D volume/shape/surface data points for further processing. Not simply display it in 3D using surf.

What would be a good way to do this?

With a lot of help from this community I could come closer:

I shrunk the size to 45x45 px for simplicity.

I = (imread("TESTGREYPLASTIC.bmp"))./2+125;
Iinv = 255-(imread("TESTGREYPLASTIC.bmp"))./2-80;%

for i = 1:45
for j = 1:45
A(i, j, I(i,j) ) = 1;
A(i, j, Iinv(i,j) ) = 1;
end
end
volshow(A)

Its not ideal but the matrix is what I wanted now. Maybe the loop can be improved to run faster when dealing with 1200x1200 points.

How do I create a real closed surface now?


回答1:


Following your conversation with @BoilermakerRV, I guess you are looking for one of the following two results:

  1. A list of 3d points, where x and y are index of pixels in the image, and z is value of corresponding pixels. The result will be an m*n by 3 matrix.

  2. An m by n by 256 volume of zeros and ones, that for (i,j)-th pixel in the image, all voxels of the (i, j)-the pile of the volume are 0, except the one at I(i, j).

Take a look at the following example that generates both results:

    close all; clc; clear variables;
    I = rgb2gray(imread('data2.png'));
    imshow(I), title('Data as image') 

    % generating mesh grid
    [m, n] = size(I);
    [X, Y] = meshgrid(1:n, 1:m);

    % converting image to list of 3-d points
    P = [Y(:), X(:), I(:)];
    figure 
    scatter3(P(:, 1), P(:, 2), P(:, 3), 3, P(:, 3), '.')
    colormap jet
    title('Same data as a list of points in R^3')

    % converting image to 256 layers of voxels
    ind = sub2ind([m n 256], Y(:), X(:), I(:));
    V = zeros(m, n, 256);
    V(ind) = 1.0;
    figure
    h = slice(V, [250], [250], [71]) ;
    [h.EdgeColor] = deal('none');
    colormap winter
    camlight
    title('And finally, as a matrix of 0/1 voxels')




回答2:


The contour plot that is shown can't be generated with "2D" data. It requires three inputs as follows:

[XGrid,YGrid] = meshgrid(-4:.1:4,-4:.1:4);
C = peaks(XGrid,YGrid);

contourf(XGrid,YGrid,C,'LevelStep',0.1,'LineStyle','none')
colormap('gray')
axis equal

Where XGrid, YGrid and C are all NxN matrices defining the X values, Y values and Z values for every point, respectively.

If you want this to be "3D", simply use surf:

surf(XGrid,YGrid,C)



来源:https://stackoverflow.com/questions/64235246/volumetric-3d-data-plotting-from-2d-map-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!