cuisto.segmentation
segmentation module, part of cuisto.
Functions for segmentating probability map stored as an image.
convert_to_pixels(filters, pixelsize)
#
Convert some values in filters
in pixels.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filters
|
dict
|
Must contain the keys used below. |
required |
pixelsize
|
float
|
Pixel size in microns. |
required |
Returns:
Name | Type | Description |
---|---|---|
filters |
dict
|
Same as input, with values in pixels. |
Source code in cuisto/segmentation.py
erode_mask(mask, edge_dist)
#
Erode the mask outline so that it is edge_dist
smaller from the border.
This allows discarding the edges.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mask
|
ndarray
|
|
required |
edge_dist
|
float
|
Distance to edges, in pixels. |
required |
Returns:
Name | Type | Description |
---|---|---|
eroded_mask |
ndarray of bool
|
|
Source code in cuisto/segmentation.py
get_collection_from_points(coords, properties, rescale_factor=1.0, offset=0.5)
#
Gather coordinates from coords
and put them in GeoJSON format.
An entry in coords
are pairs of (x, y) coordinates defining the point.
properties
is a dictionnary with QuPath properties of each detections.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
coords
|
list
|
|
required |
properties
|
dict
|
|
required |
rescale_factor
|
float
|
Rescale output coordinates by this factor. |
1.0
|
Returns:
Name | Type | Description |
---|---|---|
collection |
FeatureCollection
|
|
Source code in cuisto/segmentation.py
get_collection_from_poly(contours, properties, rescale_factor=1.0, offset=0.5)
#
Gather coordinates in the list and put them in GeoJSON format as Polygons.
An entry in contours
must define a closed polygon. properties
is a dictionnary
with QuPath properties of each detections.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
contours
|
list
|
|
required |
properties
|
dict
|
QuPatj objects' properties. |
required |
rescale_factor
|
float
|
Rescale output coordinates by this factor. |
1.0
|
offset
|
float
|
Shift coordinates by this amount, typically to get pixel centers or edges. Default is 0.5. |
0.5
|
Returns:
Name | Type | Description |
---|---|---|
collection |
FeatureCollection
|
A FeatureCollection ready to be written as geojson. |
Source code in cuisto/segmentation.py
get_collection_from_skel(skeleton, properties, rescale_factor=1.0, offset=0.5)
#
Get the coordinates of each skeleton path as a GeoJSON Features in a
FeatureCollection.
properties
is a dictionnary with QuPath properties of each detections.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
skeleton
|
Skeleton
|
|
required |
properties
|
dict
|
QuPath objects' properties. |
required |
rescale_factor
|
float
|
Rescale output coordinates by this factor. |
1.0
|
offset
|
float
|
Shift coordinates by this amount, typically to get pixel centers or edges. Default is 0.5. |
0.5
|
Returns:
Name | Type | Description |
---|---|---|
collection |
FeatureCollection
|
A FeatureCollection ready to be written as geojson. |
Source code in cuisto/segmentation.py
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 cuisto/segmentation.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 cuisto/segmentation.py
get_image_skeleton(img, minsize=0)
#
Get the image skeleton.
Computes the image skeleton and removes objects smaller than minsize
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
img
|
ndarray of bool
|
|
required |
minsize
|
number
|
Min. size the object can have, as a number of pixels. Default is 0. |
0
|
Returns:
Name | Type | Description |
---|---|---|
skel |
ndarray of bool
|
Binary image with 1-pixel wide skeleton. |
Source code in cuisto/segmentation.py
get_pixelsize(image_name)
#
Get pixel size recorded in image_name
TIFF metadata.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image_name
|
str
|
Full path to image. |
required |
Returns:
Name | Type | Description |
---|---|---|
pixelsize |
float
|
Pixel size in microns. |
Source code in cuisto/segmentation.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 cuisto/segmentation.py
pad_image(img, finalsize)
#
Pad image with zeroes to match expected final size.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
img
|
ndarray
|
|
required |
finalsize
|
tuple or list
|
nrows, ncolumns |
required |
Returns:
Name | Type | Description |
---|---|---|
imgpad |
ndarray
|
img with black borders. |
Source code in cuisto/segmentation.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 cuisto/segmentation.py
process_directory(images_dir, img_suffix='', segtype='', original_pixelsize=1.0, target_channel=0, proba_threshold=0.0, max_pixel_value=255, qupath_class='Object', qupath_color=[0, 0, 0], qupath_type='detection', channel_suffix='', edge_dist=0.0, filters={}, masks_dir='', masks_ext='')
#
Segment 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
|
max_pixel_value
|
float
|
Maximum pixel value in the segmented image, to rescale them to a probability between 0 and 1. |
255
|
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]
|
qupath_type
|
(detection, annotation)
|
QuPath type of object. |
"detection"
|
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 cuisto/segmentation.py
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 |
|
segment_lines(img, geojson_props, minsize=0.0, rescale_factor=1.0)
#
Wraps skeleton analysis to get paths coordinates.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
img
|
ndarray of bool
|
Binary image to segment as lines. |
required |
geojson_props
|
dict
|
GeoJSON properties of objects. |
required |
minsize
|
float
|
Minimum size in pixels for an object. |
0.0
|
rescale_factor
|
float
|
Rescale output coordinates by this factor. |
1.0
|
Returns:
Name | Type | Description |
---|---|---|
collection |
FeatureCollection
|
A FeatureCollection ready to be written as geojson. |
Source code in cuisto/segmentation.py
segment_points(img, geojson_props, area_min=0.0, area_max=np.inf, ecc_min=0, ecc_max=1, dist_thresh=0, rescale_factor=1)
#
Point segmentation.
First, segment polygons to apply shape filters, then extract their centroids,
and remove isolated points as defined by dist_thresh
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
img
|
ndarray of bool
|
Binary image to segment as points. |
required |
geojson_props
|
dict
|
GeoJSON properties of objects. |
required |
area_min
|
float
|
Minimum and maximum area in pixels for an object. |
0.0
|
area_max
|
float
|
Minimum and maximum area in pixels for an object. |
0.0
|
ecc_min
|
float
|
Minimum and maximum eccentricity for an object. |
0
|
ecc_max
|
float
|
Minimum and maximum eccentricity for an object. |
0
|
dist_thresh
|
float
|
Maximal distance in pixels between objects before considering them as isolated and remove them. 0 disables it. |
0
|
rescale_factor
|
float
|
Rescale output coordinates by this factor. |
1
|
Returns:
Name | Type | Description |
---|---|---|
collection |
FeatureCollection
|
A FeatureCollection ready to be written as geojson. |
Source code in cuisto/segmentation.py
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 441 442 443 444 445 446 447 448 449 450 451 |
|
segment_polygons(img, geojson_props, area_min=0.0, area_max=np.inf, ecc_min=0.0, ecc_max=1.0, rescale_factor=1.0)
#
Polygon segmentation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
img
|
ndarray of bool
|
Binary image to segment as polygons. |
required |
geojson_props
|
dict
|
GeoJSON properties of objects. |
required |
area_min
|
float
|
Minimum and maximum area in pixels for an object. |
0.0
|
area_max
|
float
|
Minimum and maximum area in pixels for an object. |
0.0
|
ecc_min
|
float
|
Minimum and maximum eccentricity for an object. |
0.0
|
ecc_max
|
float
|
Minimum and maximum eccentricity for an object. |
0.0
|
rescale_factor
|
float
|
Rescale output coordinates by this factor. |
1.0
|
Returns:
Name | Type | Description |
---|---|---|
collection |
FeatureCollection
|
A FeatureCollection ready to be written as geojson. |
Source code in cuisto/segmentation.py
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 |