napari_cellseg3d.code_models.instance_segmentation
Instance segmentation methods for 3D images.
Classes:
| Name | Description |
|---|---|
ConnectedComponents |
Widget class for Connected Components instance segmentation. Requires 2 parameters, see binary_connected. |
ImageStats |
Dataclass containing various statistics from instance labels. |
InstanceMethod |
Base class for instance segmentation methods. Contains the method name, the function to use, and the corresponding UI elements. |
InstanceWidgets |
Base widget with several sliders, for use in instance segmentation parameters. |
VoronoiOtsu |
Widget class for Voronoi-Otsu labeling from pyclesperanto. Requires 2 parameter, see voronoi_otsu. |
Watershed |
Widget class for Watershed segmentation. Requires 4 parameters, see binary_watershed. |
Functions:
| Name | Description |
|---|---|
binary_connected |
Convert binary foreground probability maps to instance masks via connected-component labeling. |
binary_watershed |
Convert binary foreground probability maps to instance masks via watershed segmentation algorithm. |
clear_large_objects |
Uses watershed to label all obejcts, and removes the ones with a volume larger than the specified threshold. |
clear_small_objects |
Calls skimage.remove_small_objects to remove small fragments that might be artifacts. |
threshold |
Remove all values smaller than the specified threshold in the volume. |
to_semantic |
Converts a ground-truth label to semantic (binary 0/1) labels. |
volume_stats |
Computes various statistics from instance labels and returns them in a dict. |
voronoi_otsu |
Voronoi-Otsu labeling from pyclesperanto. |
Attributes:
| Name | Type | Description |
|---|---|---|
USE_SLIDING_WINDOW |
If True, uses a sliding window to perform instance segmentation to avoid memory issues. |
USE_SLIDING_WINDOW
module-attribute
USE_SLIDING_WINDOW = True
If True, uses a sliding window to perform instance segmentation to avoid memory issues.
ConnectedComponents
Bases: InstanceMethod
Widget class for Connected Components instance segmentation. Requires 2 parameters, see binary_connected.
Methods:
| Name | Description |
|---|---|
__init__ |
Creates a ConnectedComponents widget. |
run_method |
Runs the method on the image with the parameters set in the widget. |
Attributes:
| Name | Type | Description |
|---|---|---|
foreground_threshold |
Returns the value of the foreground threshold slider. |
|
small_object_removal |
Returns the value of the small object removal counter. |
Source code in napari_cellseg3d/code_models/instance_segmentation.py
616 617 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 | |
foreground_threshold
property
writable
foreground_threshold
Returns the value of the foreground threshold slider.
small_object_removal
property
writable
small_object_removal
Returns the value of the small object removal counter.
__init__
__init__(widget_parent=None)
Creates a ConnectedComponents widget.
Source code in napari_cellseg3d/code_models/instance_segmentation.py
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 | |
run_method
run_method(image)
Runs the method on the image with the parameters set in the widget.
If USE_SLIDING_WINDOW is True, uses a sliding window to perform instance segmentation to avoid memory issues.
Source code in napari_cellseg3d/code_models/instance_segmentation.py
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 | |
ImageStats
dataclass
Dataclass containing various statistics from instance labels.
Methods:
| Name | Description |
|---|---|
get_dict |
Returns a dict containing the statistics. |
Source code in napari_cellseg3d/code_models/instance_segmentation.py
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | |
get_dict
get_dict()
Returns a dict containing the statistics.
Source code in napari_cellseg3d/code_models/instance_segmentation.py
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | |
InstanceMethod
Base class for instance segmentation methods. Contains the method name, the function to use, and the corresponding UI elements.
Methods:
| Name | Description |
|---|---|
__init__ |
Methods for instance segmentation. |
record_parameters |
Records all the parameters of the instance segmentation method from the current values of the widgets. |
run_method |
Runs the method on the image with the parameters set in the widget. |
run_method_from_params |
Runs the method on the image with the RECORDED parameters set in the widget. |
run_method_on_channels |
Runs the method on each channel of the image with the parameters set in the widget. |
run_method_on_channels_from_params |
Runs the method on each channel of the image with the RECORDED parameters set in the widget. |
sliding_window |
Given a volume of dimensions HxWxD, runs the provided function segmentation on the volume using a sliding window of size patch_size. |
Attributes:
| Name | Type | Description |
|---|---|---|
recorded_parameters |
Stores the parameters when calling self.record_parameters() |
Source code in napari_cellseg3d/code_models/instance_segmentation.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | |
recorded_parameters
instance-attribute
recorded_parameters = {}
Stores the parameters when calling self.record_parameters()
__init__
__init__(name: str, function: callable, num_sliders: int, num_counters: int, widget_parent: QWidget = None)
Methods for instance segmentation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the instance segmentation method (for UI) |
required |
function
|
callable
|
Function to use for instance segmentation |
required |
num_sliders
|
int
|
Number of Slider UI elements needed to set the parameters of the function |
required |
num_counters
|
int
|
Number of DoubleIncrementCounter UI elements needed to set the parameters of the function |
required |
widget_parent
|
QWidget
|
parent for the declared widgets |
None
|
Source code in napari_cellseg3d/code_models/instance_segmentation.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | |
record_parameters
record_parameters()
Records all the parameters of the instance segmentation method from the current values of the widgets.
Source code in napari_cellseg3d/code_models/instance_segmentation.py
124 125 126 127 128 129 130 131 | |
run_method
abstractmethod
run_method(image)
Runs the method on the image with the parameters set in the widget.
Source code in napari_cellseg3d/code_models/instance_segmentation.py
102 103 104 105 | |
run_method_from_params
run_method_from_params(image)
Runs the method on the image with the RECORDED parameters set in the widget.
See self.record_parameters() and self.run_method()
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
image data to run method on |
required |
Returns: processed image from self._method
Source code in napari_cellseg3d/code_models/instance_segmentation.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | |
run_method_on_channels
run_method_on_channels(image)
Runs the method on each channel of the image with the parameters set in the widget.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
image data to run method on |
required |
Returns: processed image from self._method
Source code in napari_cellseg3d/code_models/instance_segmentation.py
157 158 159 160 161 162 163 164 165 166 167 | |
run_method_on_channels_from_params
run_method_on_channels_from_params(image)
Runs the method on each channel of the image with the RECORDED parameters set in the widget.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
image data to run method on |
required |
Returns: processed image from self._method
Source code in napari_cellseg3d/code_models/instance_segmentation.py
169 170 171 172 173 174 175 176 177 178 179 | |
sliding_window
staticmethod
sliding_window(volume, func, patch_size=512, increment_labels=True)
Given a volume of dimensions HxWxD, runs the provided function segmentation on the volume using a sliding window of size patch_size.
If the edge has been reached, the patch size is reduced to fit the remaining space. The result is a segmentation of the same size as the input volume.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
volume
|
array
|
The volume to segment |
required |
func
|
callable
|
Function to use for instance segmentation. Should be a partial function with the parameters already set. |
required |
patch_size
|
int
|
The size of the sliding window. |
512
|
increment_labels
|
bool
|
If True, increments the labels of each patch by the maximum label of the previous patch. |
True
|
Returns:
| Type | Description |
|---|---|
|
np.array: Instance segmentation labels from |
Source code in napari_cellseg3d/code_models/instance_segmentation.py
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | |
InstanceWidgets
Bases: QWidget
Base widget with several sliders, for use in instance segmentation parameters.
Methods:
| Name | Description |
|---|---|
__init__ |
Creates an InstanceWidgets widget. |
run_method |
Calls instance function with chosen parameters. |
Attributes:
| Name | Type | Description |
|---|---|---|
instance_widgets |
Contains the lists of widgets for each methods, to show/hide |
|
methods |
Contains the instance of the method, with its name as key |
Source code in napari_cellseg3d/code_models/instance_segmentation.py
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 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 | |
instance_widgets
instance-attribute
instance_widgets = {}
Contains the lists of widgets for each methods, to show/hide
methods
instance-attribute
methods = {}
Contains the instance of the method, with its name as key
__init__
__init__(parent=None)
Creates an InstanceWidgets widget.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parent
|
parent widget |
None
|
Source code in napari_cellseg3d/code_models/instance_segmentation.py
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 | |
run_method
run_method(volume)
Calls instance function with chosen parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
volume
|
image data to run method on |
required |
Returns: processed image from self._method
Source code in napari_cellseg3d/code_models/instance_segmentation.py
815 816 817 818 819 820 821 822 823 824 825 | |
VoronoiOtsu
Bases: InstanceMethod
Widget class for Voronoi-Otsu labeling from pyclesperanto. Requires 2 parameter, see voronoi_otsu.
Methods:
| Name | Description |
|---|---|
__init__ |
Creates a VoronoiOtsu widget. |
run_method |
Runs the method on the image with the parameters set in the widget. |
Attributes:
| Name | Type | Description |
|---|---|---|
outline_sigma |
Returns the value of the outline sigma counter. |
|
small_object_removal |
Returns the value of the small object removal counter. |
|
spot_sigma |
Returns the value of the spot sigma counter. |
Source code in napari_cellseg3d/code_models/instance_segmentation.py
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 | |
outline_sigma
property
writable
outline_sigma
Returns the value of the outline sigma counter.
small_object_removal
property
writable
small_object_removal
Returns the value of the small object removal counter.
spot_sigma
property
writable
spot_sigma
Returns the value of the spot sigma counter.
__init__
__init__(widget_parent=None)
Creates a VoronoiOtsu widget.
Source code in napari_cellseg3d/code_models/instance_segmentation.py
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 | |
run_method
run_method(image)
Runs the method on the image with the parameters set in the widget.
If USE_SLIDING_WINDOW is True, uses a sliding window to perform instance segmentation to avoid memory issues.
Source code in napari_cellseg3d/code_models/instance_segmentation.py
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 | |
Watershed
Bases: InstanceMethod
Widget class for Watershed segmentation. Requires 4 parameters, see binary_watershed.
Methods:
| Name | Description |
|---|---|
__init__ |
Creates a Watershed widget. |
run_method |
Runs the method on the image with the parameters set in the widget. |
Attributes:
| Name | Type | Description |
|---|---|---|
foreground_threshold |
Returns the value of the foreground threshold slider. |
|
seed_threshold |
Returns the value of the seed threshold slider. |
|
small_object_removal |
Returns the value of the small object removal counter. |
|
small_seed_removal |
Returns the value of the small seed removal counter. |
Source code in napari_cellseg3d/code_models/instance_segmentation.py
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 | |
foreground_threshold
property
writable
foreground_threshold
Returns the value of the foreground threshold slider.
seed_threshold
property
writable
seed_threshold
Returns the value of the seed threshold slider.
small_object_removal
property
writable
small_object_removal
Returns the value of the small object removal counter.
small_seed_removal
property
writable
small_seed_removal
Returns the value of the small seed removal counter.
__init__
__init__(widget_parent=None)
Creates a Watershed widget.
Source code in napari_cellseg3d/code_models/instance_segmentation.py
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 | |
run_method
run_method(image)
Runs the method on the image with the parameters set in the widget.
If USE_SLIDING_WINDOW is True, uses a sliding window to perform instance segmentation to avoid memory issues.
Source code in napari_cellseg3d/code_models/instance_segmentation.py
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 | |
binary_connected
binary_connected(volume: array, thres=0.5, thres_small=3)
Convert binary foreground probability maps to instance masks via connected-component labeling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
volume
|
ndarray
|
foreground probability of shape :math: |
required |
thres
|
float
|
threshold of foreground. Default: 0.8 |
0.5
|
thres_small
|
int
|
size threshold of small objects to remove. Default: 128 |
3
|
Source code in napari_cellseg3d/code_models/instance_segmentation.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | |
binary_watershed
binary_watershed(volume, thres_objects=0.3, thres_seeding=0.9, thres_small=10, rem_seed_thres=3)
Convert binary foreground probability maps to instance masks via watershed segmentation algorithm.
Note
This function uses the skimage.segmentation.watershed <https://github.com/scikit-image/scikit-image/blob/master/skimage/segmentation/_watershed.py#L89>_
function that converts the input image into np.float64 data type for processing. Therefore, please make sure enough memory is allocated when handling large arrays.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
volume
|
ndarray
|
foreground probability of shape :math: |
required |
thres_objects
|
float
|
threshold for foreground objects. Default: 0.3 |
0.3
|
thres_seeding
|
float
|
threshold for seeding. Default: 0.9 |
0.9
|
thres_small
|
int
|
size threshold of small objects removal. Default: 10 |
10
|
rem_seed_thres
|
int
|
threshold for small seeds removal. Default : 3 |
3
|
Source code in napari_cellseg3d/code_models/instance_segmentation.py
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 | |
clear_large_objects
clear_large_objects(image, large_label_size=200, use_window=True)
Uses watershed to label all obejcts, and removes the ones with a volume larger than the specified threshold.
This is intended for artifact removal, and should not be used for instance segmentation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
array containing the image |
required | |
large_label_size
|
size threshold for removal of objects in pixels. E.g. if 10, all objects larger than 10 pixels as a whole will be removed. |
200
|
|
use_window
|
if True, will use a sliding window to perform instance segmentation to avoid memory issues. Default : True |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
array |
The image with large objects removed |
Source code in napari_cellseg3d/code_models/instance_segmentation.py
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 | |
clear_small_objects
clear_small_objects(image, threshold, is_file_path=False)
Calls skimage.remove_small_objects to remove small fragments that might be artifacts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
array containing the image |
required | |
threshold
|
size threshold for removal of objects in pixels. E.g. if 10, all objects smaller than 10 pixels as a whole will be removed. |
required | |
is_file_path
|
if True, will load the image from a file path directly. Default : False |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
array |
The image with small objects removed |
Source code in napari_cellseg3d/code_models/instance_segmentation.py
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 | |
threshold
threshold(volume, thresh)
Remove all values smaller than the specified threshold in the volume.
Source code in napari_cellseg3d/code_models/instance_segmentation.py
258 259 260 261 262 | |
to_semantic
to_semantic(image, is_file_path=False)
Converts a ground-truth label to semantic (binary 0/1) labels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
image or path to image |
required | |
is_file_path
|
if True, will consider |
False
|
Returns: resulting converted labels
Source code in napari_cellseg3d/code_models/instance_segmentation.py
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 | |
volume_stats
volume_stats(volume_image)
Computes various statistics from instance labels and returns them in a dict.
Currently provided :
* "Volume": volume of each object
* "Centroid": x,y,z centroid coordinates for each object
* "Sphericity (axes)": sphericity computed from semi-minor and semi-major axes
* "Image size": size of the image
* "Total image volume": volume in pixels of the whole image
* "Total object volume (pixels)": total labeled volume in pixels
* "Filling ratio": ratio of labeled over total pixel volume
* "Number objects": total number of unique labeled objects
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
volume_image
|
instance labels image |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Statistics described above |
Source code in napari_cellseg3d/code_models/instance_segmentation.py
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 | |
voronoi_otsu
voronoi_otsu(volume: ndarray, spot_sigma: float, outline_sigma: float, remove_small_size: float = None)
Voronoi-Otsu labeling from pyclesperanto.
BASED ON CODE FROM : napari_pyclesperanto_assistant by Robert Haase https://github.com/clEsperanto/napari_pyclesperanto_assistant Original code at : https://github.com/clEsperanto/pyclesperanto/blob/d1990e28b1da44a7921890b7bd809d522d3198b8/pyclesperanto/_tier7.py#L409-L448.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
volume
|
ndarray
|
volume to segment |
required |
spot_sigma
|
float
|
parameter determining how close detected objects can be |
required |
outline_sigma
|
float
|
determines the smoothness of the segmentation |
required |
remove_small_size
|
float
|
remove all objects smaller than the specified size in pixel |
None
|
Returns: Instance segmentation labels from Voronoi-Otsu method
Source code in napari_cellseg3d/code_models/instance_segmentation.py
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | |