Ilastik Multicut for TXY images



Hi Constantin,

So about the multicut workflow to be used for TXY type of images where all the images are independent of each other, could you please share the well optimized workflow you were mentioning before with the code to do it? Institute Curie users would be so happy to be able to use it to get the superpixel segmentation :slight_smile:.



I don’t have a well optimized workflow myself, but I think it would be fairly easy to implement to this using the ilastik headless mode.

I have coded a quick example (see below).
Of course, this needs to be adapted for real world use.
The probability maps need to be generated somehow (in the example, I just use the raw data again, which will yield bad results).
If you are using ilastik pixel classification, you could also use headless mode for this and keep everything in the same script.
Also, to speed things up, you could parallelize the execution of ilastik using multiprocessing.

Unfortunately, the example I posted is currently not working due to this ilastik issue, but that should be easy to fix. This was due to an error in the command to call ilastik headless, fixed now. Thought the error message is very confusing.

import os
from shutil import rmtree
from subprocess import call

import numpy as np
import imageio

# this assumes that the images come as tiff stack
# and that time corresponds to the first dimension
# if any of this is not the case, you will need to adapt this code
def split_images(path, tmp_path):
    stack = imageio.volread(path)
    for t, im in enumerate(stack):
        imageio.imwrite(os.path.join(tmp_path, 'im_%04i.tif' % t), im)

# we use the raw data as probability map here as dummy input
# for a real workflow this needs to be changed
def run_headless(input_folder, output_folder, ilastik_bin, ilastik_project):
    input_images = os.listdir(input_folder)

    # headless multicut workflow command
    # general options we will need for each image
    cmd = [ilastik_bin, '--project', ilastik_project,
           '--readonly', '1',
           '--output_format', 'tif']

    for in_file in input_images:
        in_path = os.path.join(input_folder, in_file)
        out_path = os.path.join(output_folder, in_file)
        # file input output options for each image
        file_cmd = ['--output_filename_format', out_path,
                    '--raw_data', in_path,
                    '--probabilities', in_path]
        this_cmd = cmd + file_cmd

# merge the output images to get a stacked segmentation
def merge_segmentation(tmp_folder, out_path):
    input_images = os.listdir(tmp_folder)
    n_ims = len(input_images)
    out = []

    # we need to offset the segmentation to get independent ids over time
    max_id = 0
    for in_file in input_images:
        im = imageio.imread(os.path.join(tmp_folder, in_file))
        im += max_id
        max_id = int(im.max())
    out = np.array(out)
    imageio.volwrite(out_path, out)

if __name__ == '__main__':
    path = '/home/cpape/Work/data/carving/Helmstaedter_et_al_SUPPLinformation3c.tif'
    tmp_folder = '/home/cpape/Work/data/mc_ilastik/data'
    print("Splitting images ...")
    split_images(path, tmp_folder)

    print("Process headless ...")
    ilastik_bin = '/home/cpape/Work/software/src/ilastik-1.3.2rc2-Linux/'
    ilastik_project = '/home/cpape/Work/data/mc_ilastik/mc_test.ilp'
    tmp_out = '/home/cpape/Work/data/mc_ilastik/out'
    run_headless(tmp_folder, tmp_out, ilastik_bin, ilastik_project)

    print("Merge segmentation ...")
    out_path = '/home/cpape/Work/data/mc_ilastik/test_seg.tif'
    merge_segmentation(tmp_folder, out_path)

    print("Clean up ...")


Hi Constantin,

Thanks a lot for this, I will try it out and then report back, sorry for replying late I was on a short vacation.

Thanks again.