Vous avez reçu un message "Your GitLab account has been locked ..." ? Pas d'inquiétude : lisez cet article https://docs.gricad-pages.univ-grenoble-alpes.fr/help/unlock/

Commit 8d3e67ed authored by Edward Andò's avatar Edward Andò
Browse files

small safety improvement in slicePadded()

parent b294c250
Pipeline #49994 passed with stages
in 23 minutes and 8 seconds
......@@ -458,7 +458,7 @@ def _binarisation(im, threshold=0.0, boolean=False, op='>', mask=None):
def slicePadded(im, startStop, createMask = False):
"""
Extract padded slice from im, which is always of the dimensions asked (given from startStop)
Extract slice from im, padded with zeros, which is always of the dimensions asked (given from startStop)
Parameters
----------
......@@ -481,24 +481,35 @@ def slicePadded(im, startStop, createMask = False):
mask : 3D numpy array of bools
The 3D mask, only returned if createMask is True
"""
imSliced = numpy.zeros((startStop[1]-startStop[0], startStop[3]-startStop[2], startStop[5]-startStop[4]), dtype=im.dtype)
start = numpy.array([startStop[0], startStop[2], startStop[4]])
stop = numpy.array([startStop[1], startStop[3], startStop[5]])
imSliced = numpy.zeros((startStop[1]-startStop[0],
startStop[3]-startStop[2],
startStop[5]-startStop[4]), dtype=im.dtype)
start = numpy.array([startStop[0], startStop[2], startStop[4]])
stop = numpy.array([startStop[1], startStop[3], startStop[5]])
startOffset = numpy.array([max(0, start[0]), max(0, start[1]), max(0, start[2])])
stopOffset = numpy.array([min(im.shape[0], stop[0]), min(im.shape[1], stop[1]), min(im.shape[2], stop[2])])
startLocal = startOffset - start
stopLocal = startLocal + stopOffset - startOffset
imSliced[startLocal[0]:stopLocal[0], startLocal[1]:stopLocal[1], startLocal[2]:stopLocal[2]] = im[startOffset[0]:stopOffset[0], startOffset[1]:stopOffset[1], startOffset[2]:stopOffset[2]]
startLocal = startOffset - start
stopLocal = startLocal + stopOffset - startOffset
if imSliced.sum() == 0:
# Check condition that we're asking for a slice of data wholly outside im
# This means either that the stop values are all smaller than 0
# OR the start are all bigger than im.shape
if numpy.all(stop < numpy.array([0, 0, 0])) or numpy.all(start >= numpy.array(im.shape)):
print("spam.helpers.slicePadded(): The extracted padded slice doesn't not touch the image!")
imSliced *= numpy.nan
if createMask:
return imSliced, numpy.zeros_like(imSliced, dtype=bool)
if createMask:
mask = numpy.zeros_like(imSliced, dtype=bool)
mask[startLocal[0]:stopLocal[0], startLocal[1]:stopLocal[1], startLocal[2]:stopLocal[2]] = 1
return imSliced, mask
else:
imSliced[startLocal[0]:stopLocal[0],
startLocal[1]:stopLocal[1],
startLocal[2]:stopLocal[2]] = im[startOffset[0]:stopOffset[0],
startOffset[1]:stopOffset[1],
startOffset[2]:stopOffset[2]]
if createMask:
mask = numpy.zeros_like(imSliced, dtype=bool)
mask[startLocal[0]:stopLocal[0], startLocal[1]:stopLocal[1], startLocal[2]:stopLocal[2]] = 1
return imSliced, mask
return imSliced
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment