How can I create 3D pixels image

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;
radius = 25;
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?

1 Commento

Rik
Rik il 26 Ott 2018
Today I formatted your code, next time, use the {}Code button. See here for a GIF showing how to do it.

Accedi per commentare.

Risposte (3)

Rik
Rik il 26 Ott 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;
radius = 25;
circlePixels = (Z-centerZ).^2 + (Y-centerY).^2 + (X-centerX).^2 <= radius.^2;

7 Commenti

Thanks, how can I create the file or visualize the image?
As tesarj13 suggests in their answer, you can use the isosurface to convert your binary image to a patch object.
The best method to show the result and to convert your object to a 2D image depends on what you want to do next. A 2D image of a sphere look the same as a 2D image of a circle. Since you asked how to extend it to 3D, I doubt that is what you want.
So what would be your next step? That might give us an idea about what would be the best way to visualize it in a figure and/or export to a flat image.
If you plan on using isosurface, you can also use the data directly:
V=(Z-centerZ).^2 + (Y-centerY).^2 + (X-centerX).^2;
isosurface(X,Y,Z,V,radius^2)
Marcelo hernandez
Marcelo hernandez il 26 Ott 2018
Modificato: Marcelo hernandez il 26 Ott 2018
If I wanted to see a slice of the sphere of size LxLx1 pixel (2D image), for any position on the z axis, is this possible?
I would like to reconstruct the sphere of radius R by adding 2R slides.
the variable is of logical type, it is not treated as that type of variables, it only has values 0.
Rik
Rik il 27 Ott 2018
Modificato: Rik il 27 Ott 2018
I don't know if this is what you mean, but the montage function might be what you want.
montage(permute(circlePixels,[1 2 4 3]))
Marcelo hernandez
Marcelo hernandez il 27 Ott 2018
Modificato: Marcelo hernandez il 27 Ott 2018
Do you know how I can show each circle in a square of 100x100 pixels?.
I would like to reconstruct the sphere as a superposition of those circles
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
Rik il 30 Ott 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.

Accedi per commentare.

tesarj13
tesarj13 il 26 Ott 2018
Modificato: tesarj13 il 26 Ott 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;
radius = 25;
circlePixels = (rowsInImage - centerY).^2 + (columnsInImage - centerX).^2 +...
(zInImage - centerZ).^2 <= radius.^2;
% circlePixels is a 3D "logical" array. % Now, display it.
isosurface (circlePixels,1/2)
axis equal
Try this:
[x,y,z] = sphere;
% Plot a sphere centered at the origin.
% Make radius 50
radius = 50;
x = radius * x;
y = radius * y;
z = radius * z;
surf(x,y,z)
grid on;
axis equal

Tag

Richiesto:

il 26 Ott 2018

Risposto:

il 30 Ott 2018

Community Treasure Hunt

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

Start Hunting!

Translated by