cellects.image_analysis.progressively_add_distant_shapes
cellects.image_analysis.progressively_add_distant_shapes
Progressively Add Distant Shapes Module
This module contains the ProgressivelyAddDistantShapes class which is designed to analyze
and connect shapes in binary images based on their size and distance from a main shape. It can
progressively grow bridges between shapes in binary video sequences, with growth speeds that depend on neighboring growth speed.
The module provides functionality to: - Check and adjust main shape labels - Consider shapes based on size criteria - Connect shapes that meet distance and size requirements - Expand small shapes toward the main shape - Modify past analysis by progressively filling pixels based on shape growth patterns
Classes: ProgressivelyAddDistantShapes: Main class for analyzing and connecting shapes in binary images.
Functions: make_gravity_field: Creates a gravity field around the main shape. CompareNeighborsWithValue: Compares neighbor values in an array. get_radius_distance_against_time: Calculates the relationship between distance and time for shape expansion.
This module is particularly useful in image analysis tasks where shapes need to be tracked and connected over time based on spatial relationships.
ProgressivelyAddDistantShapes
This class checks new potential shapes sizes and distance to a main shape.
If these sizes and distance match requirements, create a bridge between
these and the main shape. Then, the modify_past_analysis method progressively grows that bridge
in a binary video. Bridge growth speed depends on neighboring growth speed.
Attributes:
| Name | Type | Description |
|---|---|---|
new_order |
ndarray
|
A binary image of all shapes detected at t. |
main_shape |
ndarray
|
A binary image of the main shape (1) at t - 1. |
stats |
ndarray
|
Statistics about the connected components found in |
max_distance |
int
|
The maximal distance for a shape from new_potentials to get bridged. |
gravity_field |
ndarray
|
The gravity field used for connecting shapes. |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_potentials
|
ndarray
|
A binary image of all shapes detected at t. |
required |
previous_shape
|
ndarray
|
A binary image of the main shape (1) at t - 1. |
required |
max_distance
|
int
|
The maximal distance for a shape from new_potentials to get bridged. |
required |
Methods:
| Name | Description |
|---|---|
check_main_shape_label |
Check if the main shape label is correctly set. |
consider_shapes_sizes |
Consider shapes sizes and eliminate too small or large ones. |
connect_shapes |
Connect shapes that are within the maximal distance and of appropriate size. |
_expand_smalls_toward_main |
Expand small shapes toward the main shape. |
Example
new_potentials = np.array([[0, 1, 0], [1, 0, 1], [0, 1, 0]]) previous_shape = np.array([[0, 1, 0], [1, 0, 0], [0, 1, 0]]) max_distance = 2 bridge_shapes = ProgressivelyAddDistantShapes(new_potentials, previous_shape, max_distance) bridge_shapes.consider_shapes_sizes(min_shape_size=2, max_shape_size=10) bridge_shapes.connect_shapes(only_keep_connected_shapes=True, rank_connecting_pixels=False) print(bridge_shapes.expanded_shape) [[0 1 0] [1 1 1] [0 1 0]]
Source code in src/cellects/image_analysis/progressively_add_distant_shapes.py
35 36 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 224 225 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 256 257 258 259 260 261 262 263 264 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 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 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 | |
__init__(new_potentials, previous_shape, max_distance)
Find connected components and update order.
This class processes new potentials and previous shape to find connected components and updates the main shape based on a maximum distance threshold.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_potentials
|
ndarray of uint8
|
The new potential values to process. |
required |
previous_shape
|
ndarray of uint8
|
The previous shape information. |
required |
max_distance
|
The maximum distance threshold for processing. |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
new_order |
ndarray of uint8
|
The result after applying logical OR on |
stats |
ndarray of int64
|
Statistics of the connected components. |
centers |
ndarray of float64
|
Centers of the connected components. |
main_shape |
ndarray of uint8
|
The main shape array initialized to zeros. |
max_distance |
int
|
The maximum distance threshold for processing. |
Examples:
>>> new_potentials = np.array([[0, 1, 2], [3, 4, 5]])
>>> previous_shape = np.array([[0, 1, 0], [1, 0, 1]])
>>> max_distance = 2
>>> obj = ClassName(new_potentials, previous_shape, max_distance)
>>> print(obj.new_order)
[[1 1 2]
[1 1 1]]
Source code in src/cellects/image_analysis/progressively_add_distant_shapes.py
connect_shapes(only_keep_connected_shapes, rank_connecting_pixels, intensity_valley=None)
Connects small shapes to a main shape using gravity field expansion and filtering based on distance and intensity conditions.
Extended Description
When distant shapes of sufficient size are present, this method generates a gravity field around the main shape. It then expands smaller shapes toward the main one according to gradient values. If shapes fall within the gravity field range:
- Shapes not connected to the main one (via only_keep_connected_shapes) are filtered out.
- Connecting pixels between small and main shapes (via rank_connecting_pixels) receive distance-based ranking.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
only_keep_connected_shapes
|
bool
|
If True, filters expanded shapes to retain only those connected directly to the main shape. |
required |
rank_connecting_pixels
|
bool
|
If True, ranks connecting pixel extensions based on distance between small/main shapes. |
required |
intensity_valley
|
array - like
|
Optional intensity values defining a valley region for gravity field calculation. Default is None. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
gravity_field |
ndarray or array - like
|
Stores the computed gravity field used to guide shape expansion. |
expanded_shape |
ndarray of dtype uint8
|
Final combined shape after processing; contains main and connected small shapes. |
Examples:
>>> new_potentials = np.array([[1, 1, 0], [0, 0, 0], [0, 1, 1]])
>>> previous_shape = np.array([[0, 0, 0], [0, 0, 0], [0, 1, 1]])
>>> max_distance = 3
>>> pads = ProgressivelyAddDistantShapes(new_potentials, previous_shape, max_distance)
>>> pads.consider_shapes_sizes(min_shape_size=2, max_shape_size=10)
>>> pads.gravity_field = make_gravity_field(pads.main_shape, max_distance=pads.max_distance, with_erosion=0)
>>> pads.connect_shapes(only_keep_connected_shapes=False, rank_connecting_pixels=True)
>>> expanded_main, max_field_feelings = pads._expand_smalls_toward_main()
>>> print(expanded_main)
[[1 1 0]
[0 1 1]
[0 1 1]]
Source code in src/cellects/image_analysis/progressively_add_distant_shapes.py
consider_shapes_sizes(min_shape_size=None, max_shape_size=None)
Filter shapes based on minimum and maximum size thresholds.
This method adjusts new_order by excluding indices of shapes that are either
smaller than min_shape_size or larger than max_shape_size. The main shape index
(1) is preserved even if it meets the filtering criteria. When no constraints apply,
the expanded shape defaults to the main shape.
Parameters
min_shape_size : int, optional
Minimum allowed size for shapes (compared against 4th column of self.stats).
max_shape_size : int, optional
Maximum allowed size for shapes (compared against 4th column of self.stats).
Examples
new_potentials = np.array([[1, 1, 0], [0, 0, 0], [0, 1, 1]]) previous_shape = np.array([[0, 0, 0], [0, 0, 0], [0, 1, 1]]) max_distance = 2 pads = ProgressivelyAddDistantShapes(new_potentials, previous_shape, max_distance) pads.consider_shapes_sizes(min_shape_size=2, max_shape_size=10) pads.new_order array([[2, 2, 0], [0, 0, 0], [0, 1, 1]], dtype=np.uint8)
Source code in src/cellects/image_analysis/progressively_add_distant_shapes.py
distance_ranking_of_connecting_pixels()
Calculate the distance ranking of connecting pixels.
This function computes a ranked extension map based on the difference between
main_shape and expanded_shape, modifies it using a gravity field, and then
updates the expanded_shape with this ranked extension.
Source code in src/cellects/image_analysis/progressively_add_distant_shapes.py
find_expansion_timings()
Find the expansion timings of a shape in binary video.
This method calculates the time at which an expanded shape reaches the main shape, as well as the distance and time relationship during expansion.
Returns:
| Name | Type | Description |
|---|---|---|
distance_against_time |
ndarray of float64
|
Array representing the distance against time. |
time_start |
int
|
The start time of expansion in frames. |
time_end |
int
|
The end time of expansion in frames. |
Raises:
| Type | Description |
|---|---|
AttributeError
|
If 'binary_video', 'expanded_shape' or 'main_shape' are not defined. |
Source code in src/cellects/image_analysis/progressively_add_distant_shapes.py
modify_past_analysis(binary_video, draft_seg)
Modify past analysis based on binary video and draft segmentation.
This method modifies the past analysis by updating binary_video with
information from draft_seg, and then iteratively filling pixels based on
expansion timings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
binary_video
|
ndarray of uint8
|
Input binary video to be modified. |
required |
draft_seg
|
ndarray of uint8
|
Draft segmentation used for expanding the shape. |
required |
Returns:
| Type | Description |
|---|---|
ndarray of uint8
|
Modified binary video after past analysis. |