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 9d07e56c authored by Gustavo Pinzon's avatar Gustavo Pinzon
Browse files

moveLabels function - all the test are missing

parent c8b133d1
Pipeline #50287 passed with stages
in 12 minutes and 36 seconds
......@@ -1651,3 +1651,177 @@ def convexVolume(lab, boundingBoxes = None, centresOfMass = None, volumes = None
return convexVolume
def moveLabels(lab, PhiField, boundingBoxes = None, centresOfMass = None, margin = 3, PhiCOM = True, threshold = 0.5, labelDilate = 0, NumberOfThreads = 1):
This function applies a discrete Phi field (from DDIC?) over a labelled image.
lab : 3D numpy array
Labelled image
PhiField : (multidimensional x 4 x 4 numpy array of floats)
Spatial field of Phis
boundingBoxes : lab.max()x6 array of ints, optional
Bounding boxes in format returned by ``boundingBoxes``.
If not defined (Default = None), it is recomputed by running ``boundingBoxes``
centresOfMass : lab.max()x3 array of floats, optional
Centres of mass in format returned by ``centresOfMass``.
If not defined (Default = None), it is recomputed by running ``centresOfMass``
margin : int, optional
Margin, in pixels, to take in each label.
Default = 3
PhiCOM : bool, optional
Apply Phi to centre of mass of particle?, otherwise it will be applied in the middle of the particle\'s bounding box.
Default = True
threshold : float, optional
Threshold to keep interpolated voxels in the binary image.
Default = 0.5
labelDilate : int, optional
Number of times label should be dilated/eroded before returning it.
If ``labelDilate > 0`` a dilated label is returned, while ``labelDilate < 0`` returns an eroded label.
Default = 0
NumberOfThreads : int, optional
Number of Threads for multiprocessing
Default = 1
labOut : 3D numpy array
New labelled image with the labels moved by the deformations established by the PhiField.
# Define worker for multiprocessing
def work_on_one_job(worker_number, q_jobs, q_results):
while True:
job = q_jobs.get()
if job == "STOP":
label = job
getLabelReturn = spam.label.getLabel(lab,
# Check that the label exist
if getLabelReturn is not None:
# Get Phi field
Phi = PhiField[label].copy()
# Phi will be split into a local part and a part of floored displacements
disp = numpy.floor(Phi[0:3,-1]).astype(int)
Phi[0:3,-1] -= disp
# Check that the displacement exist
if numpy.isfinite(disp).sum() == 3:
# Just move binary label
# Need to do backtracking here to avoid holes in the NN interpolation
# Here we will cheat and do order 1 and re-threshold full pixels
if PhiCOM:
labSubvolDefInterp = spam.DIC.applyPhi(getLabelReturn['subvol'],
labSubvolDefInterp = spam.DIC.applyPhi(getLabelReturn['subvol'],
# "death mask"
labSubvolDefMask = labSubvolDefInterp >= threshold
del labSubvolDefInterp
# Get the boundary of the cube
topOfSlice = numpy.array([getLabelReturn['boundingBoxCube'][0] + disp[0],
getLabelReturn['boundingBoxCube'][2] + disp[1],
getLabelReturn['boundingBoxCube'][4] + disp[2]])
botOfSlice = numpy.array([getLabelReturn['boundingBoxCube'][1] + disp[0],
getLabelReturn['boundingBoxCube'][3] + disp[1],
getLabelReturn['boundingBoxCube'][5] + disp[2]])
topOfSliceCrop = numpy.array([max(topOfSlice[0], 0),
max(topOfSlice[1], 0),
max(topOfSlice[2], 0)])
botOfSliceCrop = numpy.array([min(botOfSlice[0], lab.shape[0]),
min(botOfSlice[1], lab.shape[1]),
min(botOfSlice[2], lab.shape[2])])
# Update grainSlice with disp
grainSlice = (slice(topOfSliceCrop[0], botOfSliceCrop[0]),
slice(topOfSliceCrop[1], botOfSliceCrop[1]),
slice(topOfSliceCrop[2], botOfSliceCrop[2]))
# Update labSubvolDefMask
labSubvolDefMaskCrop = labSubvolDefMask[topOfSliceCrop[0]-topOfSlice[0] : labSubvolDefMask.shape[0]-1 + (botOfSliceCrop[0]-botOfSlice[0]),
topOfSliceCrop[1]-topOfSlice[1] : labSubvolDefMask.shape[1]-1 + (botOfSliceCrop[1]-botOfSlice[1]),
topOfSliceCrop[2]-topOfSlice[2] : labSubvolDefMask.shape[2]-1 + (botOfSliceCrop[2]-botOfSlice[2])]
#print(labSubvolDefMaskCrop.shape, botOfSliceCrop-topOfSliceCrop)
q_results.put([worker_number, grainSlice, labSubvolDefMaskCrop, label])
# Nan displacement, run away
q_results.put([ worker_number, 0, 0, -1 ])
# Got None from getLabel()
q_results.put([ worker_number, 0, 0, -1 ])
#Check for boundingBoxes
if boundingBoxes is None:
boundingBoxes = spam.label.boundingBoxes(lab)
#Check for centresOfMass
if centresOfMass is None:
centresOfMass = spam.label.centresOfMass(lab)
#Create output label image
labOut = numpy.zeros_like(lab, dtype=spam.label.labelType)
#Get number of labels
numberOfLabels = min(lab.max(), PhiField.shape[0])
#Setting up queues
q_jobs = multiprocessing.Queue()
q_results = multiprocessing.Queue()
#Adding jobs to queues
for label in range(1, numberOfLabels+1):
for i in range(NumberOfThreads): q_jobs.put("STOP")
#Launching workers
for i in range(NumberOfThreads):
p = multiprocessing.Process( target=work_on_one_job, args=(i, q_jobs, q_results,))
finished_threads = 0
nodes_processed = 0
#Waiting for results
widgets = [progressbar.FormatLabel(''), ' ', progressbar.Bar(), ' ', progressbar.AdaptiveETA()]
pbar = progressbar.ProgressBar(widgets=widgets, maxval=numberOfLabels)
while finished_threads < NumberOfThreads:
result = q_results.get()
if result == "STOP":
finished_threads += 1
# Set voxels in slice to the value of the label if not in greyscale mode
if result[3] > 0:
labOut[result[1]][result[2]] = result[3]
nodes_processed += 1
widgets[0] = progressbar.FormatLabel("{}/{} ".format(nodes_processed, numberOfLabels))
return labOut
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