cellects.image_analysis.shape_descriptors
cellects.image_analysis.shape_descriptors
Module for computing shape descriptors from binary images.
This module provides a framework for calculating various geometric and statistical
descriptors of shapes in binary images through configurable dictionaries and a core class.
Supported metrics include area, perimeter, axis lengths, orientation, and more.
Descriptor computation is controlled via category dictionaries (e.g., descriptors_categories)
and implemented as methods in the ShapeDescriptors class.
Classes:
| Name | Description |
|---|---|
ShapeDescriptors : Class to compute various descriptors for a binary image |
|
Notes
Relies on OpenCV and NumPy for image processing operations. Shape descriptors: The following names, lists and computes all the variables describing a shape in a binary image. If you want to allow the software to compute another variable: 1) In the following dicts and list, you need to: add the variable name and whether to compute it (True/False) by default 2) In the ShapeDescriptors class: add a method to compute that variable 3) In the init method of the ShapeDescriptors class attribute a None value to the variable that store it add a if condition in the for loop to compute that variable when its name appear in the wanted_descriptors_list
ShapeDescriptors
This class takes :
- a binary image of 0 and 1 drawing one shape
- a list of descriptors to calculate from that image
["area", "perimeter", "circularity", "rectangularity", "total_hole_area", "solidity", "convexity",
"eccentricity", "euler_number",
"standard_deviation_y", "standard_deviation_x", "skewness_y", "skewness_x", "kurtosis_y", "kurtosis_x",
"major_axis_len", "minor_axis_len", "axes_orientation",
"mo", "contours", "min_bounding_rectangle", "convex_hull"]
Be careful! mo, contours, min_bounding_rectangle, convex_hull,
standard_deviations, skewness and kurtosis are not atomics
https://www.researchgate.net/publication/27343879_Estimators_for_Orientation_and_Anisotropy_in_Digitized_Images
Source code in src/cellects/image_analysis/shape_descriptors.py
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 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 515 516 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 614 615 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 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 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 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 | |
__init__(binary_image, wanted_descriptors_list)
Class to compute various descriptors for a binary image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
binary_image
|
ndarray
|
Binary image used to compute the descriptors. |
required |
wanted_descriptors_list
|
list
|
List of strings with the names of the wanted descriptors. |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
binary_image |
ndarray
|
The binary image. |
descriptors |
dict
|
Dictionary containing the computed descriptors. |
mo |
(float or None, optional)
|
Moment of inertia (default is |
area |
(int or None, optional)
|
Area of the object (default is |
contours |
(ndarray or None, optional)
|
Contours of the object (default is |
min_bounding_rectangle |
(tuple or None, optional)
|
Minimum bounding rectangle of the object (default is |
convex_hull |
(ndarray or None, optional)
|
Convex hull of the object (default is |
major_axis_len |
(float or None, optional)
|
Major axis length of the object (default is |
minor_axis_len |
(float or None, optional)
|
Minor axis length of the object (default is |
axes_orientation |
(float or None, optional)
|
Orientation of the axes (default is |
sx |
(float or None, optional)
|
Standard deviation in x-axis (default is |
kx |
(float or None, optional)
|
Kurtosis in x-axis (default is |
skx |
(float or None, optional)
|
Skewness in x-axis (default is |
perimeter |
(float or None, optional)
|
Perimeter of the object (default is |
convexity |
(float or None, optional)
|
Convexity of the object (default is |
Examples:
>>> binary_image = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]], dtype=np.uint8)
>>> wanted_descriptors_list = ["area", "perimeter"]
>>> SD = ShapeDescriptors(binary_image, wanted_descriptors_list)
>>> SD.descriptors
{'area': np.uint64(9), 'perimeter': 8.0}
Source code in src/cellects/image_analysis/shape_descriptors.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 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 | |
get_area()
Calculate the area of a binary image by summing its pixel values.
This function computes the area covered by white pixels (value 1) in a binary image, which is equivalent to counting the number of 'on' pixels.
Notes
Sums values in self.binary_image and stores the result in self.area.
Returns:
| Type | Description |
|---|---|
None
|
|
Examples:
>>> SD = ShapeDescriptors(np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]], dtype=np.uint8), ["area"])
>>> print(SD.area)
9.0
Source code in src/cellects/image_analysis/shape_descriptors.py
get_axes_orientation()
Ensure the axes orientation angle is computed and stored in self.axes_orientation.
Returns:
| Type | Description |
|---|---|
None
|
|
Notes
Calls get_inertia_axes() if orientation is not yet computed.
Examples:
>>> SD = ShapeDescriptors(np.array([[0, 1, 0], [0, 1, 0], [0, 1, 0]], dtype=np.uint8), ["axes_orientation"])
>>> print(SD.axes_orientation)
1.5707963267948966
Source code in src/cellects/image_analysis/shape_descriptors.py
get_circularity()
Compute and store circularity: 4πA / P².
Notes
Uses self.area and self.perimeter; stores result in self.circularity.
Returns:
| Type | Description |
|---|---|
None
|
|
Examples:
SD = ShapeDescriptors(np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]], dtype=np.uint8), ["circularity"])
Source code in src/cellects/image_analysis/shape_descriptors.py
get_contours()
Find and process the largest contour in a binary image.
Retrieves contours from a binary image, calculates the Euler number, and identifies the largest contour based on its length.
Notes
This function modifies the internal state of the self object to store
the largest contour and Euler number.
Returns:
| Type | Description |
|---|---|
None
|
|
Examples:
>>> SD = ShapeDescriptors(np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]], dtype=np.uint8), ["euler_number"])
>>> print(len(SD.contours))
8
Source code in src/cellects/image_analysis/shape_descriptors.py
get_convex_hull()
Compute and store the convex hull of the object's contour.
Notes
Stores the result in self.convex_hull. Computes contours if needed.
Returns:
| Type | Description |
|---|---|
None
|
|
Examples:
>>> SD = ShapeDescriptors(np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]], dtype=np.uint8), ["solidity"])
>>> print(len(SD.convex_hull))
4
Source code in src/cellects/image_analysis/shape_descriptors.py
get_convexity()
Compute and store convexity: convex hull perimeter / contour perimeter.
Notes
Requires self.perimeter and self.convex_hull.
Returns:
| Type | Description |
|---|---|
None
|
|
Examples:
>>> SD = ShapeDescriptors(np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]], dtype=np.uint8), ["convexity"])
>>> print(SD.convexity)
1.0
Source code in src/cellects/image_analysis/shape_descriptors.py
get_eccentricity()
Compute and store eccentricity from major and minor axis lengths.
Notes
Calls get_inertia_axes() if needed and stores result in self.eccentricity.
Returns:
| Type | Description |
|---|---|
None
|
|
Examples:
>>> SD = ShapeDescriptors(np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]], dtype=np.uint8), ["eccentricity"])
>>> print(SD.eccentricity)
0.0
Source code in src/cellects/image_analysis/shape_descriptors.py
get_euler_number()
Ensure contours are computed; stores Euler number in self.euler_number via get_contours().
Returns:
| Type | Description |
|---|---|
None
|
|
Notes
Euler number is computed in get_contours() as (components - 1) - len(contours).
Source code in src/cellects/image_analysis/shape_descriptors.py
get_inertia_axes()
Calculate and set the moments of inertia properties of an object.
This function computes the centroid, major axis length,
minor axis length, and axes orientation for an object. It
first ensures that the moments of inertia (mo) attribute is available,
computing them if necessary, before using the get_inertia_axes function.
Returns:
| Type | Description |
|---|---|
None
|
This method sets the following attributes:
- |
Raises:
| Type | Description |
|---|---|
ValueError
|
If there is an issue with the moments of inertia computation. |
Notes
This function modifies in-place the object's attributes related to its geometry.
Examples:
>>> SD = ShapeDescriptors(np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]], dtype=np.uint8), ["major_axis_len"])
>>> print(SD.axes_orientation)
0.0
Source code in src/cellects/image_analysis/shape_descriptors.py
get_kurtosis()
Calculates the kurtosis of the image moments.
Kurtosis is a statistical measure that describes the shape of a distribution's tails in relation to its overall shape. It is used here in the context of image moments analysis.
Notes
This function first checks if the kurtosis values have already been calculated.
If not, it calculates them using the get_kurtosis function.
Examples:
>>> SD = ShapeDescriptors(np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]], dtype=np.uint8), ["kurtosis_x", "kurtosis_y"])
>>> print(SD.kx, SD.ky)
1.5 1.5
Source code in src/cellects/image_analysis/shape_descriptors.py
get_major_axis_len()
Ensure the major axis length is computed and stored in self.major_axis_len.
Returns:
| Type | Description |
|---|---|
None
|
|
Notes
Triggers get_inertia_axes() if needed.
Examples:
>>> SD = ShapeDescriptors(np.array([[0, 1, 0], [0, 1, 0], [0, 1, 0]], dtype=np.uint8), ["major_axis_len"])
>>> print(SD.major_axis_len)
2.8284271247461907
Source code in src/cellects/image_analysis/shape_descriptors.py
get_min_bounding_rectangle()
Retrieve the minimum bounding rectangle from the contours of an image.
This method calculates the smallest area rectangle that can enclose the object outlines present in the image, which is useful for object detection and analysis tasks.
Notes
- The bounding rectangle is calculated only if contours are available. If not, they will be retrieved first before calculating the rectangle.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the contours are not available and cannot be retrieved, indicating a problem with the image or preprocessing steps. |
Returns:
| Type | Description |
|---|---|
None
|
|
Examples:
>>> SD = ShapeDescriptors(np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]], dtype=np.uint8), ["rectangularity"])
>>> print(len(SD.min_bounding_rectangle))
3
Source code in src/cellects/image_analysis/shape_descriptors.py
get_minor_axis_len()
Ensure the minor axis length is computed and stored in self.minor_axis_len.
Returns:
| Type | Description |
|---|---|
None
|
|
Notes
Triggers get_inertia_axes() if needed.
Examples:
>>> SD = ShapeDescriptors(np.array([[0, 1, 0], [0, 1, 0], [0, 1, 0]], dtype=np.uint8), ["minor_axis_len"])
>>> print(SD.minor_axis_len)
0.0
Source code in src/cellects/image_analysis/shape_descriptors.py
get_mo()
Get moments of a binary image.
Calculate the image moments for a given binary image using OpenCV's
cv2.moments function and then translate these moments into a formatted
dictionary.
Notes
This function assumes the binary image has already been processed and is in a suitable format for moment calculation.
Returns
None
Examples
SD = ShapeDescriptors(np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]], dtype=np.uint8), ["mo"]) print(SD.mo["m00"]) 9.0
Source code in src/cellects/image_analysis/shape_descriptors.py
get_perimeter()
Compute and store the contour perimeter length.
Notes
Computes contours if needed and stores the length in self.perimeter.
Returns:
| Type | Description |
|---|---|
None
|
|
Examples:
>>> SD = ShapeDescriptors(np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]], dtype=np.uint8), ["perimeter"])
>>> print(SD.perimeter)
8.0
Source code in src/cellects/image_analysis/shape_descriptors.py
get_rectangularity()
Compute and store rectangularity: area / bounding-rectangle-area.
Notes
Uses self.binary_image and self.min_bounding_rectangle. Computes the MBR if needed.
Returns:
| Type | Description |
|---|---|
None
|
|
Examples:
>>> SD = ShapeDescriptors(np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]], dtype=np.uint8), ["rectangularity"])
>>> print(SD.rectangularity)
2.25
Source code in src/cellects/image_analysis/shape_descriptors.py
get_skewness()
Calculate and store skewness along x and y (skx, sky).
This function computes the skewness about the x-axis and y-axis of an image. Skewness is a measure of the asymmetry of the probability distribution of values in an image.
Notes
Requires standard deviations; values are stored in self.skx and self.sky.
Returns:
| Type | Description |
|---|---|
None
|
|
Examples:
>>> SD = ShapeDescriptors(np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]], dtype=np.uint8), ["skewness_x", "skewness_y"])
>>> print(SD.skx, SD.sky)
0.0 0.0
Source code in src/cellects/image_analysis/shape_descriptors.py
get_solidity()
Compute and store solidity: contour area / convex hull area.
Extended Summary
The solidity is a dimensionless measure that compares the area of a shape to its convex hull. A solidity of 1 means the contour is fully convex, while a value less than 1 indicates concavities.
Notes
If the convex hull area is 0 or absent, solidity is set to 0.
Returns:
| Type | Description |
|---|---|
None
|
|
Examples:
>>> SD = ShapeDescriptors(np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]], dtype=np.uint8), ["solidity"])
>>> print(SD.solidity)
1.0
Source code in src/cellects/image_analysis/shape_descriptors.py
get_standard_deviations()
Calculate and store standard deviations along x and y (sx, sy).
Notes
Requires centroid and moments; values are stored in self.sx and self.sy.
Returns
None
Examples
SD = ShapeDescriptors(np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]], dtype=np.uint8), ["standard_deviation_x", "standard_deviation_y"]) print(SD.sx, SD.sy) 0.816496580927726 0.816496580927726
Source code in src/cellects/image_analysis/shape_descriptors.py
get_total_hole_area()
Calculate the total area of holes in a binary image.
This function uses connected component labeling to detect and measure the area of holes in a binary image.
Returns:
| Type | Description |
|---|---|
float
|
The total area of all detected holes in the binary image. |
Notes
This function assumes that the binary image has been pre-processed and that holes are represented as connected components of zero pixels within the foreground
Examples:
>>> SD = ShapeDescriptors(np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]], dtype=np.uint8), ["total_hole_area"])
>>> print(SD.total_hole_area)
0
Source code in src/cellects/image_analysis/shape_descriptors.py
compute_one_descriptor_per_frame(binary_vid, arena_label, timings, descriptors_dict, output_in_mm, pixel_size, do_fading, save_coord_specimen)
Computes descriptors for each frame in a binary video and returns them as a DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
binary_vid
|
NDArray[uint8]
|
The binary video data where each frame is a 2D array. |
required |
arena_label
|
int
|
Label for the arena in the video. |
required |
timings
|
NDArray
|
Array of timestamps corresponding to each frame. |
required |
descriptors_dict
|
dict
|
Dictionary containing the descriptors to be computed. |
required |
output_in_mm
|
bool
|
Flag indicating if output should be in millimeters. Default is False. |
required |
pixel_size
|
float
|
Size of a pixel in the video when |
required |
do_fading
|
bool
|
Flag indicating if the fading effect should be applied. Default is False. |
required |
save_coord_specimen
|
bool
|
Flag indicating if the coordinates of specimens should be saved. Default is False. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame containing the descriptors for each frame in the video. |
Notes
For large inputs, consider pre-allocating memory for efficiency.
The save_coord_specimen flag will save coordinate data to a file.
Examples:
>>> binary_vid = np.ones((10, 640, 480), dtype=np.uint8)
>>> timings = np.arange(10)
>>> descriptors_dict = {'area': True, 'perimeter': True}
>>> result = compute_one_descriptor_per_frame(binary_vid, 1, timings, descriptors_dict)
>>> print(result.head())
arena time area perimeter
0 1 0 0 0
1 1 1 0 0
2 1 2 0 0
3 1 3 0 0
4 1 4 0 0
>>> binary_vid = np.ones((5, 640, 480), dtype=np.uint8)
>>> timings = np.arange(5)
>>> descriptors_dict = {'area': True, 'perimeter': True}
>>> result = compute_one_descriptor_per_frame(binary_vid, 2, timings,
... descriptors_dict,
... output_in_mm=True,
... pixel_size=0.1)
>>> print(result.head())
arena time area perimeter
0 2 0 0 0.0
1 2 1 0 0.0
2 2 2 0 0.0
3 2 3 0 0.0
4 2 4 0 0.0
Source code in src/cellects/image_analysis/shape_descriptors.py
initialize_descriptor_computation(descriptors_dict)
Initialize descriptor computation based on available and requested descriptors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
descriptors_dict
|
dict
|
A dictionary where keys are descriptor names and values are booleans indicating whether to compute the corresponding descriptor. |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
A tuple containing four lists: - all_descriptors: List of all requested descriptor names. - to_compute_from_sd: Array of descriptor names that need to be computed from the shape descriptors class. - length_measures: Array of descriptor names that are length measures and need to be computed. - area_measures: Array of descriptor names that are area measures and need to be computed. |
Examples:
>>> descriptors_dict = {'perimeter': True, 'area': False}
>>> all_descriptors, to_compute_from_sd, length_measures, area_measures = initialize_descriptor_computation(descriptors_dict)
>>> print(all_descriptors, to_compute_from_sd, length_measures, area_measures)
['length'] ['length'] ['length'] []
Source code in src/cellects/image_analysis/shape_descriptors.py
scale_descriptors(descriptors_dict, pixel_size, length_measures=None, area_measures=None)
Scale the spatial descriptors in a dictionary based on pixel size.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
descriptors_dict
|
dict
|
Dictionary containing spatial descriptors. |
required |
pixel_size
|
float
|
Pixel size used for scaling. |
required |
length_measures
|
ndarray
|
Array of descriptors that represent lengths. If not provided, they will be initialized. |
None
|
area_measures
|
ndarray
|
Array of descriptors that represent areas. If not provided, they will be initialized. |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary with scaled spatial descriptors. |
Examples:
>>> from numpy import array as ndarray
>>> descriptors_dict = {'length': ndarray([1, 2]), 'area': ndarray([3, 4])}
>>> pixel_size = 0.5
>>> scaled_dict = scale_descriptors(descriptors_dict, pixel_size)
>>> print(scaled_dict)
{'length': array([0.5, 1.]), 'area': array([1.58421369, 2.])}