简体   繁体   中英

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

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?

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)

曲面图

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')

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM