How to create sphere inside a ndarray python?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have a ndarray size of 32x32x32
. I want to create a sphere inside the array with center at (x,y) and radius 4 pixels. The value of sphere is 1 while value of array is 0. How could I do it in python? Thanks so much
This is code to generate the array
import numpy as np
A = np.zeros((32,32,32))
print (A)
python numpy
|
show 3 more comments
I have a ndarray size of 32x32x32
. I want to create a sphere inside the array with center at (x,y) and radius 4 pixels. The value of sphere is 1 while value of array is 0. How could I do it in python? Thanks so much
This is code to generate the array
import numpy as np
A = np.zeros((32,32,32))
print (A)
python numpy
What do you mean by "cube (circle)"? Cubes aren't circles.
– user2357112
Nov 15 '18 at 19:25
Do you want a circle (2D) or a sphere (3D)?
– Siddharth Satpathy
Nov 15 '18 at 19:26
Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here. For instance, give an example of what you want, and post your coding attempt.
– Prune
Nov 15 '18 at 19:27
1
How about trying something? You only need to iterate over the 8x8x8 cube centred on (x,y,z) rather than the whole array...
– Mark Setchell
Nov 15 '18 at 21:42
1
Possible duplicate of How to generate a sphere in 3D Numpy array
– norok2
Feb 8 at 10:54
|
show 3 more comments
I have a ndarray size of 32x32x32
. I want to create a sphere inside the array with center at (x,y) and radius 4 pixels. The value of sphere is 1 while value of array is 0. How could I do it in python? Thanks so much
This is code to generate the array
import numpy as np
A = np.zeros((32,32,32))
print (A)
python numpy
I have a ndarray size of 32x32x32
. I want to create a sphere inside the array with center at (x,y) and radius 4 pixels. The value of sphere is 1 while value of array is 0. How could I do it in python? Thanks so much
This is code to generate the array
import numpy as np
A = np.zeros((32,32,32))
print (A)
python numpy
python numpy
edited Nov 16 '18 at 1:19
Siddharth Satpathy
6491817
6491817
asked Nov 15 '18 at 19:21
Moon LeeMoon Lee
84112
84112
What do you mean by "cube (circle)"? Cubes aren't circles.
– user2357112
Nov 15 '18 at 19:25
Do you want a circle (2D) or a sphere (3D)?
– Siddharth Satpathy
Nov 15 '18 at 19:26
Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here. For instance, give an example of what you want, and post your coding attempt.
– Prune
Nov 15 '18 at 19:27
1
How about trying something? You only need to iterate over the 8x8x8 cube centred on (x,y,z) rather than the whole array...
– Mark Setchell
Nov 15 '18 at 21:42
1
Possible duplicate of How to generate a sphere in 3D Numpy array
– norok2
Feb 8 at 10:54
|
show 3 more comments
What do you mean by "cube (circle)"? Cubes aren't circles.
– user2357112
Nov 15 '18 at 19:25
Do you want a circle (2D) or a sphere (3D)?
– Siddharth Satpathy
Nov 15 '18 at 19:26
Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here. For instance, give an example of what you want, and post your coding attempt.
– Prune
Nov 15 '18 at 19:27
1
How about trying something? You only need to iterate over the 8x8x8 cube centred on (x,y,z) rather than the whole array...
– Mark Setchell
Nov 15 '18 at 21:42
1
Possible duplicate of How to generate a sphere in 3D Numpy array
– norok2
Feb 8 at 10:54
What do you mean by "cube (circle)"? Cubes aren't circles.
– user2357112
Nov 15 '18 at 19:25
What do you mean by "cube (circle)"? Cubes aren't circles.
– user2357112
Nov 15 '18 at 19:25
Do you want a circle (2D) or a sphere (3D)?
– Siddharth Satpathy
Nov 15 '18 at 19:26
Do you want a circle (2D) or a sphere (3D)?
– Siddharth Satpathy
Nov 15 '18 at 19:26
Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here. For instance, give an example of what you want, and post your coding attempt.
– Prune
Nov 15 '18 at 19:27
Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here. For instance, give an example of what you want, and post your coding attempt.
– Prune
Nov 15 '18 at 19:27
1
1
How about trying something? You only need to iterate over the 8x8x8 cube centred on (x,y,z) rather than the whole array...
– Mark Setchell
Nov 15 '18 at 21:42
How about trying something? You only need to iterate over the 8x8x8 cube centred on (x,y,z) rather than the whole array...
– Mark Setchell
Nov 15 '18 at 21:42
1
1
Possible duplicate of How to generate a sphere in 3D Numpy array
– norok2
Feb 8 at 10:54
Possible duplicate of How to generate a sphere in 3D Numpy array
– norok2
Feb 8 at 10:54
|
show 3 more comments
3 Answers
3
active
oldest
votes
Since array indexes only have a certain level of specificity (i.e. you can only subdivide down to the width, in this case 32), there's no one perfect way to represent a sphere in an array. Instead, we can treat each array index as a space of cubic area, where the [x][y][z]
indices of the index represent the the cubic area's center coordinates. To create the sphere, we evaluate whether the sphere's presence in that area of space meets certain criteria.
We start with the equation for a sphere. From Wikipedia:
In analytic geometry, a sphere with center (x0, y0, z0) and radius r is the locus of all points (x, y, z) such that
(x - x0)^2 + (y - y0)^2 + (z - z0)^2 <= r^2.
For an array of dimensions N, the center will have the coordinate (N - 1) / 2 for all dimensions. (because for an even-numbered dimension, the center should be between the two center points, and for an odd-numbered dimension, the center should be an integer.) The magnitude of the radius can vary depending on where you decide the boundaries of the sphere relative to our imagined cubic array representation; re-reading the question, I notice you already gave the desired radius: 4.
There are two evaluation criteria I can think of:
Simple approach
In this approach, we will simply use a test of whether the array index's cubic area's center lies within the circle equation.
You can see Siddharth Satpathy's answer for some code using this approach.
Sophisticated approach
Ideally for me, the equation would decide whether an index lies within the sphere by assessing whether the proportion of sphere for that cubic area is greater than 50%. However, this approach unfortunately goes beyond my current working mathematical knowledge.
In regards to a discussion I had in the comments, neither approach is better than the other since they represent different perspectives: I personally imagine the array as being actually representative of the cubic area for each index, while others may imagine the indexes being the center points of these cubic areas.
".. whether the proportion of sphere for that cubic area is greater than 50%." An interesting idea. If you consider each data point a cube, then you can test if its center is inside the sphere. That ought to do it – at least for spheres larger than a single cube.
– usr2564301
Nov 15 '18 at 21:53
1
@usr2564301 The center of a point's cube being inside the sphere doesn't guarantee that the area occupied inside that cube is greater than 50%. While the first approach is a good approximation, I think a 50% comparison is more satisfying in certain ways. But I think the difference lies in perspective of what the array indexes represent: cubic area measurements, or the center points of these cubic areas. Your argument seems to be based on the array indexes being the centers' coordinates, while I am thinking of them as proportions of cubic area.
– Graham
Nov 15 '18 at 22:02
add a comment |
Very good question. You can try the following code. In the below mentioned code AA
is the matrix that you want. =)
import numpy as np
from copy import deepcopy
''' size : size of original 3D numpy matrix A.
radius : radius of circle inside A which will be filled with ones.
'''
size, radius = 5, 2
''' A : numpy.ndarray of shape size*size*size. '''
A = np.zeros((size,size, size))
''' AA : copy of A (you don't want the original copy of A to be overwritten.) '''
AA = deepcopy(A)
''' (x0, y0, z0) : coordinates of center of circle inside A. '''
x0, y0, z0 = int(np.floor(A.shape[0]/2)),
int(np.floor(A.shape[1]/2)), int(np.floor(A.shape[2]/2))
for x in range(x0-radius, x0+radius+1):
for y in range(y0-radius, y0+radius+1):
for z in range(z0-radius, z0+radius+1):
''' deb: measures how far a coordinate in A is far from the center.
deb>=0: inside the sphere.
deb<0: outside the sphere.'''
deb = radius - abs(x0-x) - abs(y0-y) - abs(z0-z)
if (deb)>=0: AA[x,y,z] = 1
Following is an example of the output for size=5
and radius=2
(a sphere of radius 2
pixels inside a numpy array of shape 5*5*5
):
[[[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 1. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]]
[[0. 0. 0. 0. 0.]
[0. 0. 1. 0. 0.]
[0. 1. 1. 1. 0.]
[0. 0. 1. 0. 0.]
[0. 0. 0. 0. 0.]]
[[0. 0. 1. 0. 0.]
[0. 1. 1. 1. 0.]
[1. 1. 1. 1. 1.]
[0. 1. 1. 1. 0.]
[0. 0. 1. 0. 0.]]
[[0. 0. 0. 0. 0.]
[0. 0. 1. 0. 0.]
[0. 1. 1. 1. 0.]
[0. 0. 1. 0. 0.]
[0. 0. 0. 0. 0.]]
[[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 1. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]]]
I haven't printed the output for the size and radius that you had asked for (size=32
and radius=4
), as the output will be very long.
add a comment |
Using a combination of indexing, distance calculation and masking (all with numpy
):
import numpy as np
center = (31/2, 31/2, 31/2) # if it is centered
size = (32, 32, 32)
max_dist = 4
distance = np.linalg.norm(np.subtract(np.indices(size).T, np.asarray(center)), axis=2)
#print(distance)
mask = np.ones(size) * (distance < max_dist)
print(mask)
np.indices
creates an index in the form [[[(i, j, k)]]]
, np.substract
calculates the vector difference to your center, and np.linalg.norm
calculates the vector norm. The rest is just using a mask operation on the distance array.
Does that work?
EDIT: an example with (3,3,3) for clarity purposes
center = (1, 1, 1)
size = (3, 3, 3)
distance = np.linalg.norm(np.subtract(np.indices(size).T,np.asarray(center)), axis=len(center))
mask = np.ones(size) * (distance<=1)
print(mask)
>>[[[0. 0. 0.]
[0. 1. 0.]
[0. 0. 0.]]
[[0. 1. 0.]
[1. 1. 1.]
[0. 1. 0.]]
[[0. 0. 0.]
[0. 1. 0.]
[0. 0. 0.]]]
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326570%2fhow-to-create-sphere-inside-a-ndarray-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Since array indexes only have a certain level of specificity (i.e. you can only subdivide down to the width, in this case 32), there's no one perfect way to represent a sphere in an array. Instead, we can treat each array index as a space of cubic area, where the [x][y][z]
indices of the index represent the the cubic area's center coordinates. To create the sphere, we evaluate whether the sphere's presence in that area of space meets certain criteria.
We start with the equation for a sphere. From Wikipedia:
In analytic geometry, a sphere with center (x0, y0, z0) and radius r is the locus of all points (x, y, z) such that
(x - x0)^2 + (y - y0)^2 + (z - z0)^2 <= r^2.
For an array of dimensions N, the center will have the coordinate (N - 1) / 2 for all dimensions. (because for an even-numbered dimension, the center should be between the two center points, and for an odd-numbered dimension, the center should be an integer.) The magnitude of the radius can vary depending on where you decide the boundaries of the sphere relative to our imagined cubic array representation; re-reading the question, I notice you already gave the desired radius: 4.
There are two evaluation criteria I can think of:
Simple approach
In this approach, we will simply use a test of whether the array index's cubic area's center lies within the circle equation.
You can see Siddharth Satpathy's answer for some code using this approach.
Sophisticated approach
Ideally for me, the equation would decide whether an index lies within the sphere by assessing whether the proportion of sphere for that cubic area is greater than 50%. However, this approach unfortunately goes beyond my current working mathematical knowledge.
In regards to a discussion I had in the comments, neither approach is better than the other since they represent different perspectives: I personally imagine the array as being actually representative of the cubic area for each index, while others may imagine the indexes being the center points of these cubic areas.
".. whether the proportion of sphere for that cubic area is greater than 50%." An interesting idea. If you consider each data point a cube, then you can test if its center is inside the sphere. That ought to do it – at least for spheres larger than a single cube.
– usr2564301
Nov 15 '18 at 21:53
1
@usr2564301 The center of a point's cube being inside the sphere doesn't guarantee that the area occupied inside that cube is greater than 50%. While the first approach is a good approximation, I think a 50% comparison is more satisfying in certain ways. But I think the difference lies in perspective of what the array indexes represent: cubic area measurements, or the center points of these cubic areas. Your argument seems to be based on the array indexes being the centers' coordinates, while I am thinking of them as proportions of cubic area.
– Graham
Nov 15 '18 at 22:02
add a comment |
Since array indexes only have a certain level of specificity (i.e. you can only subdivide down to the width, in this case 32), there's no one perfect way to represent a sphere in an array. Instead, we can treat each array index as a space of cubic area, where the [x][y][z]
indices of the index represent the the cubic area's center coordinates. To create the sphere, we evaluate whether the sphere's presence in that area of space meets certain criteria.
We start with the equation for a sphere. From Wikipedia:
In analytic geometry, a sphere with center (x0, y0, z0) and radius r is the locus of all points (x, y, z) such that
(x - x0)^2 + (y - y0)^2 + (z - z0)^2 <= r^2.
For an array of dimensions N, the center will have the coordinate (N - 1) / 2 for all dimensions. (because for an even-numbered dimension, the center should be between the two center points, and for an odd-numbered dimension, the center should be an integer.) The magnitude of the radius can vary depending on where you decide the boundaries of the sphere relative to our imagined cubic array representation; re-reading the question, I notice you already gave the desired radius: 4.
There are two evaluation criteria I can think of:
Simple approach
In this approach, we will simply use a test of whether the array index's cubic area's center lies within the circle equation.
You can see Siddharth Satpathy's answer for some code using this approach.
Sophisticated approach
Ideally for me, the equation would decide whether an index lies within the sphere by assessing whether the proportion of sphere for that cubic area is greater than 50%. However, this approach unfortunately goes beyond my current working mathematical knowledge.
In regards to a discussion I had in the comments, neither approach is better than the other since they represent different perspectives: I personally imagine the array as being actually representative of the cubic area for each index, while others may imagine the indexes being the center points of these cubic areas.
".. whether the proportion of sphere for that cubic area is greater than 50%." An interesting idea. If you consider each data point a cube, then you can test if its center is inside the sphere. That ought to do it – at least for spheres larger than a single cube.
– usr2564301
Nov 15 '18 at 21:53
1
@usr2564301 The center of a point's cube being inside the sphere doesn't guarantee that the area occupied inside that cube is greater than 50%. While the first approach is a good approximation, I think a 50% comparison is more satisfying in certain ways. But I think the difference lies in perspective of what the array indexes represent: cubic area measurements, or the center points of these cubic areas. Your argument seems to be based on the array indexes being the centers' coordinates, while I am thinking of them as proportions of cubic area.
– Graham
Nov 15 '18 at 22:02
add a comment |
Since array indexes only have a certain level of specificity (i.e. you can only subdivide down to the width, in this case 32), there's no one perfect way to represent a sphere in an array. Instead, we can treat each array index as a space of cubic area, where the [x][y][z]
indices of the index represent the the cubic area's center coordinates. To create the sphere, we evaluate whether the sphere's presence in that area of space meets certain criteria.
We start with the equation for a sphere. From Wikipedia:
In analytic geometry, a sphere with center (x0, y0, z0) and radius r is the locus of all points (x, y, z) such that
(x - x0)^2 + (y - y0)^2 + (z - z0)^2 <= r^2.
For an array of dimensions N, the center will have the coordinate (N - 1) / 2 for all dimensions. (because for an even-numbered dimension, the center should be between the two center points, and for an odd-numbered dimension, the center should be an integer.) The magnitude of the radius can vary depending on where you decide the boundaries of the sphere relative to our imagined cubic array representation; re-reading the question, I notice you already gave the desired radius: 4.
There are two evaluation criteria I can think of:
Simple approach
In this approach, we will simply use a test of whether the array index's cubic area's center lies within the circle equation.
You can see Siddharth Satpathy's answer for some code using this approach.
Sophisticated approach
Ideally for me, the equation would decide whether an index lies within the sphere by assessing whether the proportion of sphere for that cubic area is greater than 50%. However, this approach unfortunately goes beyond my current working mathematical knowledge.
In regards to a discussion I had in the comments, neither approach is better than the other since they represent different perspectives: I personally imagine the array as being actually representative of the cubic area for each index, while others may imagine the indexes being the center points of these cubic areas.
Since array indexes only have a certain level of specificity (i.e. you can only subdivide down to the width, in this case 32), there's no one perfect way to represent a sphere in an array. Instead, we can treat each array index as a space of cubic area, where the [x][y][z]
indices of the index represent the the cubic area's center coordinates. To create the sphere, we evaluate whether the sphere's presence in that area of space meets certain criteria.
We start with the equation for a sphere. From Wikipedia:
In analytic geometry, a sphere with center (x0, y0, z0) and radius r is the locus of all points (x, y, z) such that
(x - x0)^2 + (y - y0)^2 + (z - z0)^2 <= r^2.
For an array of dimensions N, the center will have the coordinate (N - 1) / 2 for all dimensions. (because for an even-numbered dimension, the center should be between the two center points, and for an odd-numbered dimension, the center should be an integer.) The magnitude of the radius can vary depending on where you decide the boundaries of the sphere relative to our imagined cubic array representation; re-reading the question, I notice you already gave the desired radius: 4.
There are two evaluation criteria I can think of:
Simple approach
In this approach, we will simply use a test of whether the array index's cubic area's center lies within the circle equation.
You can see Siddharth Satpathy's answer for some code using this approach.
Sophisticated approach
Ideally for me, the equation would decide whether an index lies within the sphere by assessing whether the proportion of sphere for that cubic area is greater than 50%. However, this approach unfortunately goes beyond my current working mathematical knowledge.
In regards to a discussion I had in the comments, neither approach is better than the other since they represent different perspectives: I personally imagine the array as being actually representative of the cubic area for each index, while others may imagine the indexes being the center points of these cubic areas.
edited Nov 15 '18 at 22:20
answered Nov 15 '18 at 21:45
GrahamGraham
643519
643519
".. whether the proportion of sphere for that cubic area is greater than 50%." An interesting idea. If you consider each data point a cube, then you can test if its center is inside the sphere. That ought to do it – at least for spheres larger than a single cube.
– usr2564301
Nov 15 '18 at 21:53
1
@usr2564301 The center of a point's cube being inside the sphere doesn't guarantee that the area occupied inside that cube is greater than 50%. While the first approach is a good approximation, I think a 50% comparison is more satisfying in certain ways. But I think the difference lies in perspective of what the array indexes represent: cubic area measurements, or the center points of these cubic areas. Your argument seems to be based on the array indexes being the centers' coordinates, while I am thinking of them as proportions of cubic area.
– Graham
Nov 15 '18 at 22:02
add a comment |
".. whether the proportion of sphere for that cubic area is greater than 50%." An interesting idea. If you consider each data point a cube, then you can test if its center is inside the sphere. That ought to do it – at least for spheres larger than a single cube.
– usr2564301
Nov 15 '18 at 21:53
1
@usr2564301 The center of a point's cube being inside the sphere doesn't guarantee that the area occupied inside that cube is greater than 50%. While the first approach is a good approximation, I think a 50% comparison is more satisfying in certain ways. But I think the difference lies in perspective of what the array indexes represent: cubic area measurements, or the center points of these cubic areas. Your argument seems to be based on the array indexes being the centers' coordinates, while I am thinking of them as proportions of cubic area.
– Graham
Nov 15 '18 at 22:02
".. whether the proportion of sphere for that cubic area is greater than 50%." An interesting idea. If you consider each data point a cube, then you can test if its center is inside the sphere. That ought to do it – at least for spheres larger than a single cube.
– usr2564301
Nov 15 '18 at 21:53
".. whether the proportion of sphere for that cubic area is greater than 50%." An interesting idea. If you consider each data point a cube, then you can test if its center is inside the sphere. That ought to do it – at least for spheres larger than a single cube.
– usr2564301
Nov 15 '18 at 21:53
1
1
@usr2564301 The center of a point's cube being inside the sphere doesn't guarantee that the area occupied inside that cube is greater than 50%. While the first approach is a good approximation, I think a 50% comparison is more satisfying in certain ways. But I think the difference lies in perspective of what the array indexes represent: cubic area measurements, or the center points of these cubic areas. Your argument seems to be based on the array indexes being the centers' coordinates, while I am thinking of them as proportions of cubic area.
– Graham
Nov 15 '18 at 22:02
@usr2564301 The center of a point's cube being inside the sphere doesn't guarantee that the area occupied inside that cube is greater than 50%. While the first approach is a good approximation, I think a 50% comparison is more satisfying in certain ways. But I think the difference lies in perspective of what the array indexes represent: cubic area measurements, or the center points of these cubic areas. Your argument seems to be based on the array indexes being the centers' coordinates, while I am thinking of them as proportions of cubic area.
– Graham
Nov 15 '18 at 22:02
add a comment |
Very good question. You can try the following code. In the below mentioned code AA
is the matrix that you want. =)
import numpy as np
from copy import deepcopy
''' size : size of original 3D numpy matrix A.
radius : radius of circle inside A which will be filled with ones.
'''
size, radius = 5, 2
''' A : numpy.ndarray of shape size*size*size. '''
A = np.zeros((size,size, size))
''' AA : copy of A (you don't want the original copy of A to be overwritten.) '''
AA = deepcopy(A)
''' (x0, y0, z0) : coordinates of center of circle inside A. '''
x0, y0, z0 = int(np.floor(A.shape[0]/2)),
int(np.floor(A.shape[1]/2)), int(np.floor(A.shape[2]/2))
for x in range(x0-radius, x0+radius+1):
for y in range(y0-radius, y0+radius+1):
for z in range(z0-radius, z0+radius+1):
''' deb: measures how far a coordinate in A is far from the center.
deb>=0: inside the sphere.
deb<0: outside the sphere.'''
deb = radius - abs(x0-x) - abs(y0-y) - abs(z0-z)
if (deb)>=0: AA[x,y,z] = 1
Following is an example of the output for size=5
and radius=2
(a sphere of radius 2
pixels inside a numpy array of shape 5*5*5
):
[[[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 1. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]]
[[0. 0. 0. 0. 0.]
[0. 0. 1. 0. 0.]
[0. 1. 1. 1. 0.]
[0. 0. 1. 0. 0.]
[0. 0. 0. 0. 0.]]
[[0. 0. 1. 0. 0.]
[0. 1. 1. 1. 0.]
[1. 1. 1. 1. 1.]
[0. 1. 1. 1. 0.]
[0. 0. 1. 0. 0.]]
[[0. 0. 0. 0. 0.]
[0. 0. 1. 0. 0.]
[0. 1. 1. 1. 0.]
[0. 0. 1. 0. 0.]
[0. 0. 0. 0. 0.]]
[[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 1. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]]]
I haven't printed the output for the size and radius that you had asked for (size=32
and radius=4
), as the output will be very long.
add a comment |
Very good question. You can try the following code. In the below mentioned code AA
is the matrix that you want. =)
import numpy as np
from copy import deepcopy
''' size : size of original 3D numpy matrix A.
radius : radius of circle inside A which will be filled with ones.
'''
size, radius = 5, 2
''' A : numpy.ndarray of shape size*size*size. '''
A = np.zeros((size,size, size))
''' AA : copy of A (you don't want the original copy of A to be overwritten.) '''
AA = deepcopy(A)
''' (x0, y0, z0) : coordinates of center of circle inside A. '''
x0, y0, z0 = int(np.floor(A.shape[0]/2)),
int(np.floor(A.shape[1]/2)), int(np.floor(A.shape[2]/2))
for x in range(x0-radius, x0+radius+1):
for y in range(y0-radius, y0+radius+1):
for z in range(z0-radius, z0+radius+1):
''' deb: measures how far a coordinate in A is far from the center.
deb>=0: inside the sphere.
deb<0: outside the sphere.'''
deb = radius - abs(x0-x) - abs(y0-y) - abs(z0-z)
if (deb)>=0: AA[x,y,z] = 1
Following is an example of the output for size=5
and radius=2
(a sphere of radius 2
pixels inside a numpy array of shape 5*5*5
):
[[[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 1. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]]
[[0. 0. 0. 0. 0.]
[0. 0. 1. 0. 0.]
[0. 1. 1. 1. 0.]
[0. 0. 1. 0. 0.]
[0. 0. 0. 0. 0.]]
[[0. 0. 1. 0. 0.]
[0. 1. 1. 1. 0.]
[1. 1. 1. 1. 1.]
[0. 1. 1. 1. 0.]
[0. 0. 1. 0. 0.]]
[[0. 0. 0. 0. 0.]
[0. 0. 1. 0. 0.]
[0. 1. 1. 1. 0.]
[0. 0. 1. 0. 0.]
[0. 0. 0. 0. 0.]]
[[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 1. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]]]
I haven't printed the output for the size and radius that you had asked for (size=32
and radius=4
), as the output will be very long.
add a comment |
Very good question. You can try the following code. In the below mentioned code AA
is the matrix that you want. =)
import numpy as np
from copy import deepcopy
''' size : size of original 3D numpy matrix A.
radius : radius of circle inside A which will be filled with ones.
'''
size, radius = 5, 2
''' A : numpy.ndarray of shape size*size*size. '''
A = np.zeros((size,size, size))
''' AA : copy of A (you don't want the original copy of A to be overwritten.) '''
AA = deepcopy(A)
''' (x0, y0, z0) : coordinates of center of circle inside A. '''
x0, y0, z0 = int(np.floor(A.shape[0]/2)),
int(np.floor(A.shape[1]/2)), int(np.floor(A.shape[2]/2))
for x in range(x0-radius, x0+radius+1):
for y in range(y0-radius, y0+radius+1):
for z in range(z0-radius, z0+radius+1):
''' deb: measures how far a coordinate in A is far from the center.
deb>=0: inside the sphere.
deb<0: outside the sphere.'''
deb = radius - abs(x0-x) - abs(y0-y) - abs(z0-z)
if (deb)>=0: AA[x,y,z] = 1
Following is an example of the output for size=5
and radius=2
(a sphere of radius 2
pixels inside a numpy array of shape 5*5*5
):
[[[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 1. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]]
[[0. 0. 0. 0. 0.]
[0. 0. 1. 0. 0.]
[0. 1. 1. 1. 0.]
[0. 0. 1. 0. 0.]
[0. 0. 0. 0. 0.]]
[[0. 0. 1. 0. 0.]
[0. 1. 1. 1. 0.]
[1. 1. 1. 1. 1.]
[0. 1. 1. 1. 0.]
[0. 0. 1. 0. 0.]]
[[0. 0. 0. 0. 0.]
[0. 0. 1. 0. 0.]
[0. 1. 1. 1. 0.]
[0. 0. 1. 0. 0.]
[0. 0. 0. 0. 0.]]
[[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 1. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]]]
I haven't printed the output for the size and radius that you had asked for (size=32
and radius=4
), as the output will be very long.
Very good question. You can try the following code. In the below mentioned code AA
is the matrix that you want. =)
import numpy as np
from copy import deepcopy
''' size : size of original 3D numpy matrix A.
radius : radius of circle inside A which will be filled with ones.
'''
size, radius = 5, 2
''' A : numpy.ndarray of shape size*size*size. '''
A = np.zeros((size,size, size))
''' AA : copy of A (you don't want the original copy of A to be overwritten.) '''
AA = deepcopy(A)
''' (x0, y0, z0) : coordinates of center of circle inside A. '''
x0, y0, z0 = int(np.floor(A.shape[0]/2)),
int(np.floor(A.shape[1]/2)), int(np.floor(A.shape[2]/2))
for x in range(x0-radius, x0+radius+1):
for y in range(y0-radius, y0+radius+1):
for z in range(z0-radius, z0+radius+1):
''' deb: measures how far a coordinate in A is far from the center.
deb>=0: inside the sphere.
deb<0: outside the sphere.'''
deb = radius - abs(x0-x) - abs(y0-y) - abs(z0-z)
if (deb)>=0: AA[x,y,z] = 1
Following is an example of the output for size=5
and radius=2
(a sphere of radius 2
pixels inside a numpy array of shape 5*5*5
):
[[[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 1. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]]
[[0. 0. 0. 0. 0.]
[0. 0. 1. 0. 0.]
[0. 1. 1. 1. 0.]
[0. 0. 1. 0. 0.]
[0. 0. 0. 0. 0.]]
[[0. 0. 1. 0. 0.]
[0. 1. 1. 1. 0.]
[1. 1. 1. 1. 1.]
[0. 1. 1. 1. 0.]
[0. 0. 1. 0. 0.]]
[[0. 0. 0. 0. 0.]
[0. 0. 1. 0. 0.]
[0. 1. 1. 1. 0.]
[0. 0. 1. 0. 0.]
[0. 0. 0. 0. 0.]]
[[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 1. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]]]
I haven't printed the output for the size and radius that you had asked for (size=32
and radius=4
), as the output will be very long.
edited Nov 15 '18 at 22:47
answered Nov 15 '18 at 21:43
Siddharth SatpathySiddharth Satpathy
6491817
6491817
add a comment |
add a comment |
Using a combination of indexing, distance calculation and masking (all with numpy
):
import numpy as np
center = (31/2, 31/2, 31/2) # if it is centered
size = (32, 32, 32)
max_dist = 4
distance = np.linalg.norm(np.subtract(np.indices(size).T, np.asarray(center)), axis=2)
#print(distance)
mask = np.ones(size) * (distance < max_dist)
print(mask)
np.indices
creates an index in the form [[[(i, j, k)]]]
, np.substract
calculates the vector difference to your center, and np.linalg.norm
calculates the vector norm. The rest is just using a mask operation on the distance array.
Does that work?
EDIT: an example with (3,3,3) for clarity purposes
center = (1, 1, 1)
size = (3, 3, 3)
distance = np.linalg.norm(np.subtract(np.indices(size).T,np.asarray(center)), axis=len(center))
mask = np.ones(size) * (distance<=1)
print(mask)
>>[[[0. 0. 0.]
[0. 1. 0.]
[0. 0. 0.]]
[[0. 1. 0.]
[1. 1. 1.]
[0. 1. 0.]]
[[0. 0. 0.]
[0. 1. 0.]
[0. 0. 0.]]]
add a comment |
Using a combination of indexing, distance calculation and masking (all with numpy
):
import numpy as np
center = (31/2, 31/2, 31/2) # if it is centered
size = (32, 32, 32)
max_dist = 4
distance = np.linalg.norm(np.subtract(np.indices(size).T, np.asarray(center)), axis=2)
#print(distance)
mask = np.ones(size) * (distance < max_dist)
print(mask)
np.indices
creates an index in the form [[[(i, j, k)]]]
, np.substract
calculates the vector difference to your center, and np.linalg.norm
calculates the vector norm. The rest is just using a mask operation on the distance array.
Does that work?
EDIT: an example with (3,3,3) for clarity purposes
center = (1, 1, 1)
size = (3, 3, 3)
distance = np.linalg.norm(np.subtract(np.indices(size).T,np.asarray(center)), axis=len(center))
mask = np.ones(size) * (distance<=1)
print(mask)
>>[[[0. 0. 0.]
[0. 1. 0.]
[0. 0. 0.]]
[[0. 1. 0.]
[1. 1. 1.]
[0. 1. 0.]]
[[0. 0. 0.]
[0. 1. 0.]
[0. 0. 0.]]]
add a comment |
Using a combination of indexing, distance calculation and masking (all with numpy
):
import numpy as np
center = (31/2, 31/2, 31/2) # if it is centered
size = (32, 32, 32)
max_dist = 4
distance = np.linalg.norm(np.subtract(np.indices(size).T, np.asarray(center)), axis=2)
#print(distance)
mask = np.ones(size) * (distance < max_dist)
print(mask)
np.indices
creates an index in the form [[[(i, j, k)]]]
, np.substract
calculates the vector difference to your center, and np.linalg.norm
calculates the vector norm. The rest is just using a mask operation on the distance array.
Does that work?
EDIT: an example with (3,3,3) for clarity purposes
center = (1, 1, 1)
size = (3, 3, 3)
distance = np.linalg.norm(np.subtract(np.indices(size).T,np.asarray(center)), axis=len(center))
mask = np.ones(size) * (distance<=1)
print(mask)
>>[[[0. 0. 0.]
[0. 1. 0.]
[0. 0. 0.]]
[[0. 1. 0.]
[1. 1. 1.]
[0. 1. 0.]]
[[0. 0. 0.]
[0. 1. 0.]
[0. 0. 0.]]]
Using a combination of indexing, distance calculation and masking (all with numpy
):
import numpy as np
center = (31/2, 31/2, 31/2) # if it is centered
size = (32, 32, 32)
max_dist = 4
distance = np.linalg.norm(np.subtract(np.indices(size).T, np.asarray(center)), axis=2)
#print(distance)
mask = np.ones(size) * (distance < max_dist)
print(mask)
np.indices
creates an index in the form [[[(i, j, k)]]]
, np.substract
calculates the vector difference to your center, and np.linalg.norm
calculates the vector norm. The rest is just using a mask operation on the distance array.
Does that work?
EDIT: an example with (3,3,3) for clarity purposes
center = (1, 1, 1)
size = (3, 3, 3)
distance = np.linalg.norm(np.subtract(np.indices(size).T,np.asarray(center)), axis=len(center))
mask = np.ones(size) * (distance<=1)
print(mask)
>>[[[0. 0. 0.]
[0. 1. 0.]
[0. 0. 0.]]
[[0. 1. 0.]
[1. 1. 1.]
[0. 1. 0.]]
[[0. 0. 0.]
[0. 1. 0.]
[0. 0. 0.]]]
edited Nov 16 '18 at 14:30
answered Nov 16 '18 at 14:22
MstainoMstaino
2,1321413
2,1321413
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326570%2fhow-to-create-sphere-inside-a-ndarray-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What do you mean by "cube (circle)"? Cubes aren't circles.
– user2357112
Nov 15 '18 at 19:25
Do you want a circle (2D) or a sphere (3D)?
– Siddharth Satpathy
Nov 15 '18 at 19:26
Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here. For instance, give an example of what you want, and post your coding attempt.
– Prune
Nov 15 '18 at 19:27
1
How about trying something? You only need to iterate over the 8x8x8 cube centred on (x,y,z) rather than the whole array...
– Mark Setchell
Nov 15 '18 at 21:42
1
Possible duplicate of How to generate a sphere in 3D Numpy array
– norok2
Feb 8 at 10:54