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

first versionn of fixOversegmentation(), doc + test missing

parent 6c12280d
Pipeline #72456 passed with stages
in 13 minutes and 52 seconds
......@@ -351,7 +351,7 @@ def labelledContacts(lab, maximumCoordinationNumber=20):
return [ contacts, Z, contactTable, contactingLabels[1:contacts.max()+1,:] ]
def fetchTwoGrains(volLab,volGrey,labels,boundingBoxes=None,padding=0,size_exclude=5):
def fetchTwoGrains(volLab,labels,volGrey=None,boundingBoxes=None,padding=0,size_exclude=5):
"""
Fetches the sub-volume of two grains from a labelled image
......@@ -360,11 +360,11 @@ def fetchTwoGrains(volLab,volGrey,labels,boundingBoxes=None,padding=0,size_exclu
volLab : 3D array of integers
Labelled volume, with lab.max() labels
volGrey : 3D array
Grey-scale volume
labels : 1x2 array of integers
the two labels that should be contained in the subvolume
volGrey : 3D array
Grey-scale volume
boundingBoxes : lab.max()x6 array of ints, optional
Bounding boxes in format returned by ``boundingBoxes``.
......@@ -394,6 +394,8 @@ def fetchTwoGrains(volLab,volGrey,labels,boundingBoxes=None,padding=0,size_exclu
#else:
#print "bounding boxes are given"
lab1, lab2 = labels
# Define output dictionary since we'll add different things to it
output = {}
# get coordinates of the big bounding box
startZ = min( boundingBoxes[lab1,0], boundingBoxes[lab2,0] ) - padding
stopZ = max( boundingBoxes[lab1,1], boundingBoxes[lab2,1] ) + padding
......@@ -401,8 +403,12 @@ def fetchTwoGrains(volLab,volGrey,labels,boundingBoxes=None,padding=0,size_exclu
stopY = max( boundingBoxes[lab1,3], boundingBoxes[lab2,3] ) + padding
startX = min( boundingBoxes[lab1,4], boundingBoxes[lab2,4] ) - padding
stopX = max( boundingBoxes[lab1,5], boundingBoxes[lab2,5] ) + padding
subVolLab = volLab[startZ:stopZ+1, startY:stopY+1, startX:stopX+1]
output['slice'] = (slice(startZ, stopZ+1),
slice(startY, stopY+1),
slice(startX, stopX+1))
subVolLab = volLab[output['slice'][0].start:output['slice'][0].stop,
output['slice'][1].start:output['slice'][1].stop,
output['slice'][2].start:output['slice'][2].stop]
subVolLab_A = numpy.where( subVolLab == lab1, lab1, 0 )
subVolLab_B = numpy.where( subVolLab == lab2, lab2, 0 )
......@@ -414,13 +420,17 @@ def fetchTwoGrains(volLab,volGrey,labels,boundingBoxes=None,padding=0,size_exclu
struc[0,1,1]=1
struc[2,1,1]=1
subVolLab = spam.label.filterIsolatedCells(subVolLab, struc, size_exclude)
output['subVolLab'] = subVolLab
subVolBin = numpy.where( subVolLab != 0, 1, 0 )
subVolGrey = volGrey[startZ:stopZ+1, startY:stopY+1, startX:stopX+1]
subVolGrey = subVolGrey * subVolBin
return subVolLab, subVolBin, subVolGrey
output['subVolBin'] = subVolBin
if volGrey is not None:
subVolGrey = volGrey[output['slice'][0].start:output['slice'][0].stop,
output['slice'][1].start:output['slice'][1].stop,
output['slice'][2].start:output['slice'][2].stop]
subVolGrey = subVolGrey * subVolBin
output['subVolGrey'] = subVolGrey
return output
def localDetection(subVolGrey,localThreshold,radiusThresh=None):
......@@ -927,8 +937,8 @@ def localDetectionAssembly(volLab, volGrey, contactList, localThreshold, boundin
def funLocalDetectionAssembly(job):
grainA, grainB = contactList[job].astype('int')
labels = [grainA, grainB]
subVolLab, subVolBin, subVolGrey = fetchTwoGrains(volLab,volGrey,labels,boundingBoxes)
contact = localDetection(subVolGrey,localThreshold,radiusThresh)
subset = fetchTwoGrains(volLab,labels,volGrey,boundingBoxes)
contact = localDetection(subset['subVolGrey'],localThreshold,radiusThresh)
if contact == True:
#print ("we are in contact!")
return job, grainA, grainB
......@@ -1027,8 +1037,12 @@ def contactOrientationsAssembly(volLab, volGrey, contactList, watershed="ITK", p
def funContactOrientationsAssembly(job):
grainA, grainB = contactList[job,0:2].astype('int')
labels = [grainA, grainB]
subVolLab, subVolBin, subVolGrey = fetchTwoGrains(volLab, volGrey, labels, boundingBoxes)
contactNormal, intervox, NotTreatedContact = spam.label.contactOrientations(subVolBin, subVolLab, watershed, peakDistance=peakDistance, verbose=verbose)
subset = fetchTwoGrains(volLab, labels, volGrey, boundingBoxes)
contactNormal, intervox, NotTreatedContact = spam.label.contactOrientations(subset['subVolBin'],
subset['subVolLab'],
watershed,
peakDistance=peakDistance,
verbose=verbose)
#TODO work on not treated contacts -- output them!
return grainA, grainB, contactNormal[0], contactNormal[1], contactNormal[2], intervox
......
......@@ -1982,6 +1982,7 @@ def detectOverSegmentation(lab):
for label in labels:
if label == 0:
overSegCoeff.append(0)
sharedLabel.append(0)
else:
# Check if there are contacting areas and volumes
if len(contactLabels[1][label]) > 0 and vol[label] > 0 :
......@@ -2146,3 +2147,77 @@ def fixUndersegmentation(lab, imGrey, targetLabels, underSegCoeff, boundingBoxes
pbar.finish()
print('\n spam.label.fixUndersegmentation(): From {} target labels, {} were modified'.format( len(targetLabels), successCounter))
return lab
def fixOversegmentation(lab, targetLabels, sharedLabel, verbose=True, imShowProgress=False):
# TODO: Missing DOC
# TODO: Missing test
# TODO: Missing usual checks on the inputs
# Start counters
labelDummy = numpy.zeros(lab.shape)
finishedLabelsCounter = 0
finishedLabels = []
if verbose:
widgets = [progressbar.FormatLabel(''), ' ', progressbar.Bar(), ' ', progressbar.AdaptiveETA()]
pbar = progressbar.ProgressBar(widgets=widgets, maxval=len(targetLabels))
pbar.start()
# Main loop
for labelA in targetLabels:
# Verify that the label is not on the finished list
if labelA in finishedLabels:
# It is already on the list, move on
pass
else:
# Get the touching label
labelB = sharedLabel[labelA]
# Add then to the list
finishedLabels.append(labelA)
finishedLabels.append(labelB)
# Get the subset of the two labels
subset = spam.label.fetchTwoGrains(lab, [labelA,labelB])
# Change the labelB by labelA in the subset
subVolLabNew = numpy.where(subset['subVolLab'] == labelB, labelA, subset['subVolLab'])
# Create a disposable dummy sample to allocate the grains
labelDummyUnit = numpy.zeros(lab.shape)
#Alocate the grains
labelDummyUnit[subset['slice'][0].start:subset['slice'][0].stop,
subset['slice'][1].start:subset['slice'][1].stop,
subset['slice'][2].start:subset['slice'][2].stop] = subVolLabNew
# Add the grains
labelDummy = labelDummy + labelDummyUnit
# Remove the label from the original label image
lab = spam.label.removeLabels(lab, [labelA, labelB])
# Enter graphical mode
if imShowProgress:
# Change the labels to show different colourss
subVolLabNorm = numpy.where(subset['subVolLab'] == labelA, 1, subset['subVolLab'])
subVolLabNorm = numpy.where(subset['subVolLab'] == labelB, 2, subVolLabNorm)
fig=plt.figure()
plt.subplot(3,2,1)
plt.gca().set_title('Before')
plt.imshow( subVolLabNorm[subset['subVolLab'].shape[0]//2, :, :], cmap="cubehelix" )
plt.subplot(3,2,2)
plt.gca().set_title('After')
plt.imshow( subVolLabNew[subVolLabNew.shape[0]//2, :, :], cmap="cubehelix" )
plt.subplot(3,2,3)
plt.imshow( subVolLabNorm[:, subset['subVolLab'].shape[1]//2,:], cmap="cubehelix" )
plt.subplot(3,2,4)
plt.imshow( subVolLabNew[:, subVolLabNew.shape[1]//2, :], cmap="cubehelix" )
plt.subplot(3,2,5)
plt.imshow( subVolLabNorm[:, :, subset['subVolLab'].shape[2]//2], cmap="cubehelix" )
plt.subplot(3,2,6)
plt.imshow( subVolLabNew[:, :, subVolLabNew.shape[2]//2], cmap="cubehelix" )
fig.suptitle('Label {} and {}'.format(labelA, labelB))
plt.show()
if verbose:
finishedLabelsCounter += 1
pbar.update(finishedLabelsCounter)
# We finish, lets add the new grains to the labelled image
lab = lab + labelDummy
# Update the labels
lab = spam.label.makeLabelsSequential(lab)
if verbose:
pbar.finish()
return lab
......@@ -72,9 +72,9 @@ class RandomTestCase(unittest.TestCase):
# to test
labels = [1,2]
subVolLab, subVolBin, subVolGrey = con.fetchTwoGrains(volLab,Box,labels)
subset = con.fetchTwoGrains(volLab,labels,Box)
self.assertEqual(numpy.unique(subVolLab).sum(),3)
self.assertEqual(numpy.unique(subset['subVolLab']).sum(),3)
def test_contactingLabelsAndPoints(self):
box = numpy.zeros((50, 50, 100), dtype='<f8')
......
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