How can I improve the speed of this function?
def foo(mri_data, radius):
mask = mri_data.copy()
ny = len(mri_data[0,:])
nx = len(mri_data[:])
for y in xrange(0, ny):
for x in xrange(0, nx):
if (mri_data[x-radius:x+radius,y-radius:y+radius] != 1.0).all():
mask[x,y] = 0.0
return mask.copy()
It takes in image slices in the form of a numpy array. Iterates through each pixels and tests a bounding box around that pixel. If no values in the box are equal to 1 than we discard that pixel by setting it to 0.
I’ve been told I can use numpy.convolve but I am uncertain how this relates.
EDIT: The images values are in binary range so lowest value is 0.0 and max value is 1.0. With values in between ex: 0.767.
One of the cases where you can abuse convolution. I wouldn’t use it, but the boundaries are otherwise tedious…
Should do the same thing really…