5

This is probably very basic matlab, so forgive me. I use the command sphere to create a 3D sphere and have the x,y,z matrices that will produce it using surf. For example:

[x,y,z]=sphere(64);

I'd like to project (or sum) this 3D sphere into one of the Cartesian 2D planes (for example X-Y plane) to obtain a 2D matrix that will be the projection of that sphere. Using imshow or imagesc on the output should look something like this:

enter image description here

simple summing obviously doesn't work, how can I accomplish that in Matlab?

user1118321
  • 25,567
  • 4
  • 55
  • 86

3 Answers3

1

It is possible that I completely misunderstand your question, in which case I apologize; but I think one of the following three methods may in fact be what you need. Note that method 3 gives an image that looks a lot like the example you provided... but I got there with a very different route (not using the sphere command at all, but computing "voxels inside" and "voxels outside" by working directly with their distance from the center). I inverted the second image compared to the third on since it looked better that way - filling the sphere with zeros made it look almost like a black disk.

enter image description here

%% method 1: find the coordinates, and histogram them
[x y z]=sphere(200);
xv = linspace(-1,1,40);
[xh xc]=histc(x(:), xv);
[yh yc]=histc(y(:), xv);

% sum the occurrences of coordinates using sparse:
sm = sparse(xc, yc, ones(size(xc)));
sf = full(sm);

figure; 
subplot(1,3,1);
imagesc(sf); axis image; axis off
caxis([0 sf(19,19)]) % add some clipping
title 'projection of point density'

%% method 2: fill a sphere and add its volume elements:
xv = linspace(-1,1,100);
[xx yy zz]=meshgrid(xv,xv,xv);
rr = sqrt(xx.^2 + yy.^2 + zz.^2);
vol = zeros(numel(xv)*[1 1 1]);
vol(rr<1)=1;
proj = sum(vol,3);
subplot(1,3,2)
imagesc(proj); axis image; axis off; colormap gray
title 'projection of volume'

%% method 3: visualize just a thin shell:
vol2 = ones(numel(xv)*[1 1 1]);
vol2(rr<1) = 0;
vol2(rr<0.95)=1;
projShell = sum(vol2,3);
subplot(1,3,3);
imagesc(projShell); axis image; axis off; colormap gray
title 'projection of a shell'
Floris
  • 45,857
  • 6
  • 70
  • 122
0

You can project on the X-Y plane in Matlab by using:

[x,y,z] = sphere(64);
surf(x,y,zeros(size(z)));

But I think you should not use Matlab for this, because the problem is so simple you can do this analytically...

reverse_engineer
  • 4,239
  • 4
  • 18
  • 27
  • I'm not interested in the plotting the outcome, but to have a 2D matrix that is the projection of the 3D sphere. And if it is so simple, why won't you give that as an answer? –  Mar 28 '13 at 15:48
  • But what do you mean with 2D matrix that is the projection of a sphere? How can a matrix be a projection of a sphere? That makes no sense... – reverse_engineer Mar 28 '13 at 16:02
  • a sphere is in essence a 3D object, a projection is a 2D object. in Matlab 2D objects are represented by a matrix. If I use `imagesc` on that matrix I expect to get something like the image I attached, where there will be accumulation of points (or higher pixel values) along the "ring", and much less so in the middle. –  Mar 28 '13 at 16:16
  • okay I see what you mean, so you would want a sphere representation where you have more intensity where the sphere is thicker (in 3D) and less intensity where the sphere is thinner... I don't see a direct way to do that... – reverse_engineer Mar 28 '13 at 16:43
0

I would look at map projections, which are designed for this purpose.

A search on "map projections matlab" yields documentation on a Matlab mapping toolbox. However, if you want or need to roll your own, there is a good summary at the USGS website, as well as a wikipedia article.

comingstorm
  • 25,557
  • 3
  • 43
  • 67
  • Tough the map toolbox seem very useful, again, it won't output a 2D matrix of that projection, and it is mainly for the purpose of display. Also, the projection I'm interested in will sum all the points of the sphere that fall into a pixel in the 2D plane, whereas in the map toolbox (looking at Vertical Perspective Azimuthal Projection for example), half of the sphere is missing since it shows only the relevant half the globe. –  Mar 28 '13 at 17:05
  • There are many different kinds of projection. Some may only be able to show half the sphere, but others (like Stereographic or Mercator projection) will show essentially all the sphere. (note that it is impossible to map a sphere continuously to a plane without at least one singular point...) – comingstorm Mar 28 '13 at 17:29