MorpholibJ and labelToRGB inside python script

morpholibj
python
Tags: #<Tag:0x00007fb8905658d8> #<Tag:0x00007fb890565360>

#1

Hi,

can somebody help me to convert that macro line into a ypthon script using the fantastic MorphoLibJ API

run("Labels To RGB", "colormap=[Golden angle] background=Black shuffle");

I do not want to use the IJ.run method, but I struggle to understand the part of the API to achive the same. Can anyone point me into the right direction?


#2

Hello @sebi06,

The interesting lines of code in the original Java plugin are these ones.

This jython script should be equivalent:

# Inverse ROI: replaces the current ROI by its inverse.
from ij import IJ
from java.awt import Color
from inra.ijpb.label import LabelImages
from inra.ijpb.util.ColorMaps import CommonLabelMaps
from inra.ijpb.util import CommonColors

# Get current ImagePlus
imagePlus = IJ.getImage()

maxLabel = 255;
bgColor = Color.BLACK
shuffleLut = True
lutName = CommonLabelMaps.GOLDEN_ANGLE.getLabel()

# Create a new LUT from info in dialog
lut = CommonLabelMaps.fromLabel(lutName).computeLut(maxLabel, shuffleLut)
    	
# Create a new RGB image from index image and LUT options
resPlus = LabelImages.labelToRgb(imagePlus, lut, bgColor)
resPlus.show()

#3

Hi @iarganda,

perfect. This was what I was looking for. Here is my final script (maybe it helps somebody else …)

# @File(label = "Image File", persist=True) FILENAME
# @String(label = "Label Connectivity", choices={"6", "26"}, style="listBox", value="6", persist=True) LABEL_CONNECT
# @Integer(label = "MinVoxelSize", value=200, persist=True) MINVOXSIZE
# @Boolean(label = "Colorize Labels", value=True, persist=True) LABEL_COLORIZE
# @Boolean(label = "Save Particle Stack", value=True, persist=True) PASAVE
# @String(label = "Choose Save Format", choices={"ome.tiff", "png", "jpeg", "tiff"}, style="listBox", value="ome.tiff", persist=True) SAVEFORMAT
# @Boolean(label = "Save Result File as TXT", value=True, persist=True) RESULTSAVE
# @Boolean(label = "Run in headless mode", value=False, persist=False) HEADLESS
# @OUTPUT String FILENAME
# @OUTPUT String LABEL_CONNECT
# @OUTPUT Integer MINVOXSIZE
# @OUTPUT Boolean LABEL_COLORIZE
# @OUTPUT Boolean PASAVE
# @OUTPUT String SAVEFORMAT
# @OUTPUT Boolean RESULTSAVE
# @OUTPUT Boolean HEADLESS

#@UIService uiService
#@LogService log


"""
File: 3d_analytics.py
Author: Sebastian Rhode
Date: 2017_12_11
Version: 0.3
"""

# append path
import os
import sys
scriptdir = os.path.join(os.getcwd(), 'Scripts')
sys.path.append(scriptdir)
log.info('Script Directory: ' + scriptdir)

from fijipytools import ExportTools, FilterTools, ImageTools, ImportTools
from fijipytools import AnalyzeTools, RoiTools, MiscTools, ThresholdTools
from java.lang import Double, Integer
from ij import IJ, ImagePlus, ImageStack, Prefs
from ij.process import ImageProcessor, ImageConverter, LUT, ColorProcessor
from ij.plugin import Thresholder, Duplicator
from ij.plugin.filter import GaussianBlur, RankFilters, BackgroundSubtracter, Binary
from ij.plugin.filter import ParticleAnalyzer as PA
from ij.plugin.frame import RoiManager
from ij.io import FileSaver
from ij.gui import Roi, Overlay
from ij.measure import ResultsTable

import json
import os
import urllib
from java.awt import Color
from java.util import Random
from jarray import zeros
from org.scijava.vecmath import Point3f, Color3f

# MorphoLibJ imports
from inra.ijpb.binary import BinaryImages
from inra.ijpb.morphology import MinimaAndMaxima3D, Morphology, Strel3D
from inra.ijpb.watershed import Watershed
from inra.ijpb.label import LabelImages
from inra.ijpb.plugins import ParticleAnalysis3DPlugin, BoundingBox3DPlugin, ExtendBordersPlugin
from inra.ijpb.data.border import BorderManager3D, ReplicatedBorder3D
from inra.ijpb.util.ColorMaps import CommonLabelMaps
from inra.ijpb.util import CommonColors

############################################################################

def run(imagefile):

    # Opening the image
    log.info('Opening Image: ' + imagefile)

    # open image file and get a specific series
    imps, MetaInfo = ImportTools.openfile(imagefile)

    log.info('File Extension   : ' + MetaInfo['Extension'])
    if 'ResolutionCount' in MetaInfo:
        log.info('Resolution Count : ' + str(MetaInfo['ResolutionCount']))
    if 'SeriesCount' in MetaInfo:
        log.info('SeriesCount      : ' + str(MetaInfo['SeriesCount']))

    # get sprecifies image series
    log.info('Getting Image Series : ' + str(IMAGESERIES))
    imp = ImageTools.getseries(imps, series=IMAGESERIES)

    # do the processing
    log.info('Start Processing ...')

    # extend borders
    log.info('Border Extension ...')
    # create BorderManager and add Zeros in all dimensions
    bmType = BorderManager3D.Type.fromLabel("BLACK")
    bm = bmType.createBorderManager(imp.getStack())
    BorderExt = ExtendBordersPlugin()
    # extend border by always exb
    imp = BorderExt.process(imp.getStack(), EXB, EXB, EXB, EXB, EXB, EXB, bm)
    # convert back to ImgPlus
    pastack = ImagePlus('Particles', imp)

    # check for pixel in 3d by size
    log.info('Filtering VoxelSize - Minimum : ' + str(MINVOXSIZE))
    pastack = BinaryImages.volumeOpening(pastack.getStack(), MINVOXSIZE)
    imp = ImagePlus('Particles Filtered', pastack)
    pastack = BinaryImages.componentsLabeling(imp, LABEL_CONNECT, LABEL_BITDEPTH)

    # get the labels
    labels = LabelImages.findAllLabels(pastack)
    log.info('Labels Filtered : ' + str(len(labels)))

    # run 3D particle analysis
    log.info('3D Particle Analysis ...')
    PA3d = ParticleAnalysis3DPlugin()
    results = PA3d.process(pastack)
    
    # colorize the labels
    if LABEL_COLORIZE:
    	log.info('Colorize Lables ...')
        maxLabel = 255
        bgColor = Color.BLACK
        shuffleLut = True
        lutName = CommonLabelMaps.GOLDEN_ANGLE.getLabel()

        # Create a new LUT from info in dialog
        lut = CommonLabelMaps.fromLabel(lutName).computeLut(maxLabel, shuffleLut)
    	
        #  Create a new RGB image from index image and LUT options
        pastack = LabelImages.labelToRgb(pastack, lut, bgColor)

    return pastack, results, labels


################################################################################

# clear the console automatically
uiService.getDefaultUI().getConsolePane().clear()

###### Define various parameters ######
MAXSIZE = Double.POSITIVE_INFINITY
SUFFIX_PA = '_PA'
SUFFIX_RT = '_RESULTS'
SAVEFORMAT_RT = 'txt'
IMAGESERIES = 0
LABEL_BITDEPTH = 16
LABEL_CONNECT = Integer.valueOf(LABEL_CONNECT)
EXB = 1


# get the FILENAME as string
imagefile = FILENAME.toString()

log.info('Starting ...')
log.info('Filename                      : ' + imagefile)
log.info('Label Connectivity            : ' + str(LABEL_CONNECT))
log.info('Label output BitDepth         : ' + str(LABEL_BITDEPTH))
log.info('Colorize Labels               : ' + str(LABEL_COLORIZE))
log.info('Minimun Voxel Size            : ' + str(MINVOXSIZE))
log.info('Headless Mode                 : ' + str(HEADLESS))
log.info('Starting Image Analysis ...')


# run image analysis pipeline
objstack, results, labels = run(imagefile)


# show objects
log.info('Show Objects inside stack ...')
objstack.show()

log.info('Show ResultsTable ...')
results.show('3D Objects')

if PASAVE:
    savepath_objstack = ExportTools.savedata(objstack, imagefile,
                                            suffix=SUFFIX_PA,
                                            extension=SAVEFORMAT)
    log.info('Saving Processed Image to : ' + savepath_objstack)

# save the result file
if RESULTSAVE:
    # save the result table
    rtsavelocation = AnalyzeTools.create_resultfilename(imagefile,
                                                        suffix=SUFFIX_RT,
                                                        extension=SAVEFORMAT_RT)
    results.saveAs(rtsavelocation)
    log.info('Save Results to: ' + rtsavelocation)

# finish
log.info('Done.')

Python script example for 3D particle analysis