How to save the output of 'surf' into a 3-D matrix?

Hi,
I input a 2-D image to ' surf' to generate a 3-D surface image. I would like to save the output of ' surf' in the form of a 3-D matrix as follows.
Assume a 2-D (2x2)image;
image=[2 0; 6 2]
image =
2 0
6 2
Now, I would like to represent this image as a 3D surface matrix; image3d of size (2x2xC). The third dimension (i.e. C) here stands for the number of sub-images generated from the input image based on the individual pixel values.
For example, if I assume that I will consider 7 sub-images (i.e. sampled slices); given that the maximum pixel value is 6 (+1 for zero pixel-value)in the input image. I will have C=7, and my image3D would be of size 2x2x7, and it will presumably look as follows:
image3D(:,:,1) =
NaN 0
NaN NaN
image3D(:,:,2) =
NaN NaN
NaN NaN
image3D(:,:,3) =
2 NaN
NaN 2
image3D(:,:,4) =
NaN NaN
NaN NaN
image3D(:,:,5) =
NaN NaN
NaN NaN
image3D(:,:,6) =
NaN NaN
NaN NaN
image3D(:,:,7) =
NaN NaN
6 NaN
Assuming that my understanding of the way ' surf' works is correct, how to implement this in Matlab? Is there a feasible way to extract the result generated by ' surf' and store it in a 3-D matrix as shown above?
The reason for doing this is that I need to input the 3-D image generated by ' surf' to a program designed to handle a 3-D volume matrix.
I read the documentation of ' surf' where it mentions that some sampling procedure is followed to generate the 3-D surface image, but I did not find any information on how to store the result in a matrix format. http://www.mathworks.com/help/techdoc/ref/surf.html
Thanks in advance for your help,
Khalid

 Accepted Answer

An interesting task. surf is definitely not sufficient to solve it, because it creates a 3D visualization, as you can find out by reading the documentation again.
A dull approach - [untested]:
data = [2 0; 6 2];
list = transpose(unique(data(:)));
result = NaN(size(Data, 1), size(Data, 2), max(list) + 1);
value = [NaN, 0];
for aData = list
value(2) = aData;
result(:, :, aData + 1) = value((data == aData) + 1);
end
There are nicer solutions. Because Matt fig does not post his lovely puzzlers anymore, let's take the chance to find nice/efficient/funny solutions!

6 Comments

Thanks very much Jan. This works brilliantly. However, I am not sure if this is the way that 'surf' handles it as I will need to do this on (gray scale) 2-D images. I think my understanding explained in my initial post is primitive. Is there a way to visualize this result in order to compare it with that generated by 'surf'?
SURF generates a 3D plot of a surface. The created 3D array is not a surface, but simply a 3D array. You can interprete it as volumetric data, but there is no direct connection to SURF then. SURF draws a surface in 3D using the coordinates of the corners and a corresponding color for each sub-area.
Perhaps your problem is getting more clear, if you explain, what you have as inputs and what you want as outputs.
Thanks Jan for your response. My input is only one gray scale (2-D) image of size AxB. I need to convert it to a 3-D volume of size (AxBxC) and save the result in a 3-D matrix, where C is the range of pixel values in the Z direction (C needs to be appropriately sampled between zero and one for double; or between 0 and 255 for uint8).
I thought that a feasible way to achieve this is to imitate 'surf' in Matlab, and generate a 3-D surface image. At each point on the z-axis, I think of the 3-D image generated by 'surf' as a 2-D slice (image) of size AxB. The number of slices is C.
I am looking into some how extracting these slices (from Matlab's surf) in order to put them in the 3-D matrix as explained above. I am not sure how can I do that?
I am not sure if there is a more efficient way to achieve this?
Thank you.
Sorry, I get deeply confused by reading the problem you want to solve and seeing that you are convinced that SURF could be helpful. SURF does not create slices. Therefore you cannot extract them.
SURF draws into an AXES object. Even if SURF would create anything which matches your idea of "slices", there is no interface to pick anything from inside the built-in function.
Therefore my personal suggestion is: Forget SURF, because this idea blocks your creativity to find a working solution. I admit, I should be true: It blocks *my* creativity.
Hi Jan,
Thanks for your response. OK, I agree with what you've said. I am forgetting about SURF. I would appreciate if you point me to some idea to generate a 3-D (volume) matrix from a single gray scale image in Matlab?
Thanks once again.
I have been looking for the same answer. I have 2D image containing 3D data(e.g. xData- Voltage, yData- Current, zData- efficiency). So using GRABIT it is possible to generate matrices of lines and create 3D surface using surf.
As @Jan answered, matlab is not generating points inbetween lines. The surf plot is probably not usefull for this purpose. Although when I used meshgrid for suf-plot and moved mouse cursor over grid crosss sections it shows the XZY data values inbetween the curves(the data that are not in the XYZ Matrix that was used)

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Asked:

on 22 May 2012

Commented:

on 11 Apr 2021

Community Treasure Hunt

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

Start Hunting!