segment_images
Script to segment objects from images.
For fiber-like objects, binarize and skeletonize the image, then use skan
to extract
branches coordinates.
For polygon-like objects, binarize the image and detect objects and extract contours
coordinates.
For points, treat that as polygons then extract the centroids instead of contours.
Finally, export the coordinates as collections in geojson files, importable in QuPath.
Supports any number of channel of interest within the same image. One file output file
per channel will be created.
This script uses cuisto.seg
. It is designed to work on probability maps generated
from a pixel classifier in QuPath, but might work on raw images.
Usage : fill-in the Parameters section of the script and run it.
A "geojson" folder will be created in the parent directory of IMAGES_DIR
.
To exclude objects near the edges of an ROI, specify the path to masks stored as images
with the same names as probabilities images (without their suffix).
author : Guillaume Le Goc (g.legoc@posteo.org) @ NeuroPSI version : 2024.12.10
CHANNELS_PARAMS = [{'name': 'cy5', 'target_channel': 0, 'proba_threshold': 0.85, 'qp_class': 'Fibers: Cy5', 'qp_color': [164, 250, 120]}, {'name': 'dsred', 'target_channel': 1, 'proba_threshold': 0.65, 'qp_class': 'Fibers: DsRed', 'qp_color': [224, 153, 18]}, {'name': 'egfp', 'target_channel': 2, 'proba_threshold': 0.85, 'qp_class': 'Fibers: EGFP', 'qp_color': [135, 11, 191]}]
module-attribute
#
This should be a list of dictionary (one per channel) with keys :
- name: str, used as suffix for output geojson files, not used if only one channel
- target_channel: int, index of the segmented channel of the image, 0-based
- proba_threshold: float < 1, probability cut-off for that channel
- qp_class: str, name of QuPath classification
- qp_color: list of RGB values, associated color
EDGE_DIST = 0
module-attribute
#
Distance to brain edge to ignore, in µm. 0 to disable.
FILTERS = {'length_low': 1.5, 'area_low': 10, 'area_high': 1000, 'ecc_low': 0.0, 'ecc_high': 0.9, 'dist_thresh': 30}
module-attribute
#
Dictionary with keys :
- length_low: minimal length in microns - for lines
- area_low: minimal area in µm² - for polygons and points
- area_high: maximal area in µm² - for polygons and points
- ecc_low: minimal eccentricity - for polygons and points (0 = circle)
- ecc_high: maximal eccentricity - for polygons and points (1 = line)
- dist_thresh: maximal inter-point distance in µm - for points
IMAGES_DIR = '/path/to/images'
module-attribute
#
Full path to the images to segment.
IMG_SUFFIX = '_Probabilities.tiff'
module-attribute
#
Images suffix, including extension. Masks must be the same name without the suffix.
MASKS_DIR = 'path/to/corresponding/masks'
module-attribute
#
Full path to the masks, to exclude objects near the brain edges (set to None or empty string to disable this feature).
MASKS_EXT = 'tiff'
module-attribute
#
Masks files extension.
MAX_PIX_VALUE = 255
module-attribute
#
Maximum pixel possible value to adjust proba_threshold
.
ORIGINAL_PIXELSIZE = 0.45
module-attribute
#
Original images pixel size in microns. This is in case the pixel classifier uses a lower resolution, yielding smaller probability maps, so output objects coordinates need to be rescaled to the full size images. The pixel size is written in the "Image" tab in QuPath.
QUPATH_TYPE = 'detection'
module-attribute
#
QuPath object type.
SEGTYPE = 'boutons'
module-attribute
#
Type of segmentation.
get_geojson_dir(images_dir)
#
Get the directory of geojson files, which will be in the parent directory
of images_dir
.
If the directory does not exist, create it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
images_dir
|
str
|
|
required |
Returns:
Name | Type | Description |
---|---|---|
geojson_dir |
str
|
|
Source code in scripts/segmentation/segment_images.py
get_geojson_properties(name, color, objtype='detection')
#
Return geojson objects properties as a dictionnary, ready to be used in geojson.Feature.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Classification name. |
required |
color
|
tuple or list
|
Classification color in RGB (3-elements vector). |
required |
objtype
|
str
|
Object type ("detection" or "annotation"). Default is "detection". |
'detection'
|
Returns:
Name | Type | Description |
---|---|---|
props |
dict
|
|
Source code in scripts/segmentation/segment_images.py
get_seg_method(segtype)
#
Determine what kind of segmentation is performed.
Segmentation kind are, for now, lines, polygons or points. We detect that based on hardcoded keywords.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
segtype
|
str
|
|
required |
Returns:
Name | Type | Description |
---|---|---|
seg_method |
str
|
|
Source code in scripts/segmentation/segment_images.py
parameters_as_dict(images_dir, masks_dir, segtype, name, proba_threshold, edge_dist)
#
Get information as a dictionnary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
images_dir
|
str
|
Path to images to be segmented. |
required |
masks_dir
|
str
|
Path to images masks. |
required |
segtype
|
str
|
Segmentation type (eg. "fibers"). |
required |
name
|
str
|
Name of the segmentation (eg. "green"). |
required |
proba_threshold
|
float < 1
|
Probability threshold. |
required |
edge_dist
|
float
|
Distance in µm to the brain edge that is ignored. |
required |
Returns:
Name | Type | Description |
---|---|---|
params |
dict
|
|
Source code in scripts/segmentation/segment_images.py
process_directory(images_dir, img_suffix='', segtype='', original_pixelsize=1.0, target_channel=0, proba_threshold=0.0, qupath_class='Object', qupath_color=[0, 0, 0], channel_suffix='', edge_dist=0.0, filters={}, masks_dir='', masks_ext='')
#
Main function, processes the .ome.tiff files in the input directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
images_dir
|
str
|
Animal ID to process. |
required |
img_suffix
|
str
|
Images suffix, including extension. |
''
|
segtype
|
str
|
Segmentation type. |
''
|
original_pixelsize
|
float
|
Original images pixel size in microns. |
1.0
|
target_channel
|
int
|
Index of the channel containning the objects of interest (eg. not the background), in the probability map (not the original images channels). |
0
|
proba_threshold
|
float < 1
|
Probability below this value will be discarded (multiplied by |
0.0
|
qupath_class
|
str
|
Name of the QuPath classification. |
'Object'
|
qupath_color
|
list of three elements
|
Color associated to that classification in RGB. |
[0, 0, 0]
|
channel_suffix
|
str
|
Channel name, will be used as a suffix in output geojson files. |
''
|
edge_dist
|
float
|
Distance to the edge of the brain masks that will be ignored, in microns. Set to 0 to disable this feature. |
0.0
|
filters
|
dict
|
Filters values to include or excludes objects. See the top of the script. |
{}
|
masks_dir
|
str
|
Path to images masks, to exclude objects found near the edges. The masks must be with the same name as the corresponding image to be segmented, without its suffix. Default is "", which disables this feature. |
''
|
masks_ext
|
str
|
Masks files extension, without leading ".". Default is "" |
''
|
Source code in scripts/segmentation/segment_images.py
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 |
|
write_parameters(outfile, parameters, filters, original_pixelsize)
#
Write parameters to outfile
.
A timestamp will be added. Parameters are written as key = value, and a [filters] is added before filters parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
outfile
|
str
|
Full path to the output file. |
required |
parameters
|
dict
|
General parameters. |
required |
filters
|
dict
|
Filters parameters. |
required |
original_pixelsize
|
float
|
Size of pixels in original image. |
required |