quantumjot / btrack

Bayesian multi-object tracking
https://btrack.readthedocs.io
MIT License
310 stars 50 forks source link

`btrack.utils.segmentation_to_objects` fails if you are trying to measure an `intensity_image` from a single segment #361

Open nthndy opened 11 months ago

nthndy commented 11 months ago

I have some images where the associated segmentation may only feature one single segment/object in a frame. I need to extract the intensity_image as a part of the properties parameter of segmentation_to_objects so that I can do some extra measurements before removing this property from all the objects prior to tracking. However, when iterating over all my frames I noticed that segmentation_to_objects fails on the frames where there is just a single mask/segment. Here's the full error message:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [37], in <cell line: 11>()
      2 props = ('axis_major_length',
      3          'axis_minor_length',
      4          'eccentricity',
   (...)
      7          'mean_intensity',
      8          'intensity_image')
     10 # localise all cells in image stack
---> 11 objects = btrack.utils.segmentation_to_objects(
     12                                                segmentation = mask_stack, # set the masks here 
     13                                                intensity_image = image, # provide the image so that the mean intensity can be measured
     14                                                properties = props, # provide the cell properties to improve tracker 
     15                                                use_weighted_centroid = False, 
     16 #                                                    assign_class_ID=True,
     17                                                )

File ~/analysis/btrack/btrack/io/_localization.py:290, in segmentation_to_objects(segmentation, intensity_image, properties, extra_properties, scale, use_weighted_centroid, assign_class_ID, num_workers)
    288     for data in tqdm(container, total=len(container), position=0):
    289         _nodes = processor(data)
--> 290         nodes = _concat_nodes(nodes, _nodes)
    291 else:
    292     logger.info(f"Processing using {num_workers} workers.")

File ~/analysis/btrack/btrack/io/_localization.py:34, in _concat_nodes(nodes, new_nodes)
     31 """Concatentate centroid dictionaries."""
     32 for key, values in new_nodes.items():
     33     nodes[key] = (
---> 34         np.concatenate([nodes[key], values]) if key in nodes else values
     35     )
     36 return nodes

File <__array_function__ internals>:180, in concatenate(*args, **kwargs)

ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 4 dimension(s) and the array at index 1 has 1 dimension(s)

I think it might be important to note that this is specific to the intensity_image parameter and that if I remove that property it can still manage the non-scalar mean_intensity parameter.

Setup: btrack.SystemInformation() output:

btrack_version: 0.6.1.dev30
system_platform: Linux-5.15.0-76-generic-x86_64-with-glibc2.31
system_python: 3.9.12