# How can I create 3D pixels image

22 views (last 30 days)
Marcelo hernandez on 26 Oct 2018
Answered: Image Analyst on 30 Oct 2018
I need to create a sphere in a box of 100 pixels per side
The code to form a circle is the following
imageSizeX = 100;
imageSizeY = 100;
[columnsInImage rowsInImage] = meshgrid(1:imageSizeX, 1:imageSizeY);
% Next create the circle in the image.
centerX = 50;
centerY = 50;
circlePixels = (rowsInImage - centerY).^2 + (columnsInImage - centerX).^2 <= radius.^2;
% circlePixels is a 2D "logical" array.
% Now, display it.
image(circlePixels) ;
colormap([0 0 0; 1 1 1]);
imwrite(circlePixels,'circle.bmp');
How could the previous code be modified for a sphere?
Rik on 26 Oct 2018
Today I formatted your code, next time, use the {}Code button. See here for a GIF showing how to do it.

Rik on 26 Oct 2018
The code generating the binary mask is relatively easy to extend, but you'll have to think about the image format you want to save it in, and the way you want to show the 3D object in a Matlab figure.
imageSizeX = 100;
imageSizeY = 100;
imageSizeZ = 100;
[X,Y,Z] = ndgrid(1:imageSizeX, 1:imageSizeY, 1:imageSizeZ);
% Next create the circle in the image.
centerX = 50;
centerY = 50;
centerZ = 50;
circlePixels = (Z-centerZ).^2 + (Y-centerY).^2 + (X-centerX).^2 <= radius.^2;
Rik on 27 Oct 2018
If you want to superimpose them, why split them up in the first place? You can sum them (or calculate the mean, which save you from scaling the output).
I=mean(circlePixels,3);%converts to double as well
imshow(I)
imwrite(I,'sphere.bmp');
Rik on 30 Oct 2018
Did this suggestion solve your problem? If so, please consider marking it as accepted answer. It will make it easier for other people with the same question to find an answer. If this didn't solve your question, please comment with what problems you are still having.

tesarj13 on 26 Oct 2018
Edited: tesarj13 on 26 Oct 2018
imageSizeX = 100;
imageSizeY = 100;
imageSizeZ = 100;
[columnsInImage, rowsInImage,zInImage] =...
meshgrid(1:imageSizeX, 1:imageSizeY,1:imageSizeZ);
% Next create the circle in the image.
centerY = 50;
centerX = 50;
centerZ = 50;
circlePixels = (rowsInImage - centerY).^2 + (columnsInImage - centerX).^2 +...
% circlePixels is a 3D "logical" array. % Now, display it.
isosurface (circlePixels,1/2)
axis equal

Image Analyst on 30 Oct 2018
Try this:
[x,y,z] = sphere;
% Plot a sphere centered at the origin.
surf(x,y,z)
grid on;
axis equal