How to store a 3-D surf image into a 3-D matrix?

I have an 'image' (2D Matrix) of size AxB. The 'surf(image)' command in Matlab generates a 3-D surface image. I would like to store the generated 3-D surface image in a single 3-D matrix say 'image3D' of size AxBxC; thus it can be used for additional operations. 'C' here is the size pertaining to the dimension in the Z direction, and it has to do with the individual pixel values in the original 'image'.
Is there a feasible way to do this in Matlab?

Answers (2)

I am not sure I understand your question. If you input a 2-D matrix into surf() then the values of your matrix are the values plotted by surf. You can get the 'ZData' from the surf() plot, but that will be the same as your input matrix.
x = randn(20,20);
h = surf(x);
x1 = get(h,'ZData');
isequal(x,x1)

1 Comment

Thanks Wayne for your response. Well, here is precisely what I am trying to do.
Assume a 2D (3x3)image;
image=[1 2 0; 6 2 3; 1 0 15]
image =
1 2 0
6 2 3
1 0 15
Now, I would like to represent this image as a 3D surface matrix; image3d of size (3x3xC). 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 16 sub-images; given that the maximum pixel value is 15 (+1 for zero pixel-value)in the input image. I will have C=16, and my image3D would be of size 3x3x16, and it will presumably look as follows:
image3D(:,:,1) =
NaN NaN 0
NaN NaN NaN
NaN 0 NaN
image3D(:,:,2) =
1 NaN NaN
NaN NaN NaN
1 NaN NaN
image3D(:,:,3) =
NaN 2 NaN
NaN 2 NaN
NaN NaN NaN
image3D(:,:,4) =
NaN NaN NaN
NaN NaN 3
NaN NaN NaN
image3D(:,:,5) =
NaN NaN NaN
NaN NaN NaN
NaN NaN NaN
image3D(:,:,6) =
NaN NaN NaN
NaN NaN NaN
NaN NaN NaN
image3D(:,:,7) =
NaN NaN NaN
6 NaN NaN
NaN NaN NaN
image3D(:,:,8) =
NaN NaN NaN
NaN NaN NaN
NaN NaN NaN
image3D(:,:,9) =
NaN NaN NaN
NaN NaN NaN
NaN NaN NaN
image3D(:,:,10) =
NaN NaN NaN
NaN NaN NaN
NaN NaN NaN
image3D(:,:,11) =
NaN NaN NaN
NaN NaN NaN
NaN NaN NaN
image3D(:,:,12) =
NaN NaN NaN
NaN NaN NaN
NaN NaN NaN
image3D(:,:,13) =
NaN NaN NaN
NaN NaN NaN
NaN NaN NaN
image3D(:,:,14) =
NaN NaN NaN
NaN NaN NaN
NaN NaN NaN
image3D(:,:,15) =
NaN NaN NaN
NaN NaN NaN
NaN NaN NaN
image3D(:,:,16) =
NaN NaN NaN
NaN NaN NaN
NaN NaN 15
Now, I want to implement this procedure on images. However, I am encountered with the following two issues. First, how do I precisely determine the number C (i.e. the third dimension). Is it merely based on the maximum pixel value as I did above? Is there a certain sampling procedure in the Z-direction that needs to be implemented?
Second, the above representation creates from a single input matrix many (surface) slices and to create the 3D surface image, one simply needs to systematically stack these slices on top of each other. However, when there is a slice with NaN value(s), as shown above, how to interpolate the pixels in the 3D image pertaining to the other slice(s) with certain pixel values that are beneath and/or above such NaN pixels?
I assume that 'surf' in Matlab handles a similar procedure somehow, but I would like it to be sorted out in a 3D matrix as I explained above.
I hope I made myself clear this time.
Thanks very much,
Khalid

Sign in to comment.

I think 'surf' in Matlab does not do it the way I described above. It turned out that it is way easier than I initially thought. It merely replicates (i.e. stacks) the original image for C number of times in the Z-direction:
So, for:
image=[1 2 0; 6 2 3; 1 0 15]
image=
1 2 0
6 2 3
1 0 15
One would have the 3D image of size, image3D(1:3,1:3,1:16)
Thus,
image3D(:,:,1) =
1 2 0
6 2 3
1 0 15
image3D(:,:,2) =
1 2 0
6 2 3
1 0 15
And so on, till,
image3D(:,:,16) =
1 2 0
6 2 3
1 0 15
Thus, I need to replicate the '*image*' 16 times. I used this command and it worked,
image3D=repmat(image, [1 1 16])
The question remains, for images which typically have variant pixel values, how does 'surf' decides on the number of replications (i.e. C)? Does it really matter? Does it look for how many different pixel values in the input 2-D image, and then assigns it to C? If so, how can I count the number of (different) pixel values in an image?
Thanks.

Categories

Asked:

on 10 May 2012

Community Treasure Hunt

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

Start Hunting!