@@ -524,9 +524,11 @@ def map_labels(
524
524
The maximum number of z steps
525
525
positions : array
526
526
Array of positions
527
+ target_labels : array
528
+ Array of target label index.
527
529
"""
528
530
if target_pixel_size >= current_pixel_size :
529
- return 1 , [position ]
531
+ return 1 , [position ], [ 0 ]
530
532
if overlap < 0 :
531
533
overlap = 0
532
534
if x_direction not in ["x" , "-x" , "y" , "-y" ]:
@@ -555,6 +557,12 @@ def map_labels(
555
557
regionprops = measure .regionprops (labeled_image )
556
558
z_range = 1
557
559
560
+ target_labels_index = []
561
+ start = - 1
562
+ end = - 1
563
+ start_weight = current_image_width + current_image_height
564
+ end_weight = 0
565
+
558
566
for i in range (target_num ):
559
567
min_z , min_y , min_x , max_z , max_y , max_x = regionprops [i ].bbox
560
568
@@ -565,6 +573,53 @@ def map_labels(
565
573
) < filter_pixel_number :
566
574
continue
567
575
576
+ target_labels_index .append (i )
577
+ # find the start and end position
578
+ centroid_z , centroid_y , centroid_x = regionprops [i ].centroid
579
+ if start < 0 :
580
+ start = len (target_labels_index ) - 1
581
+ start_weight = centroid_x + centroid_y
582
+ elif centroid_x + centroid_y < start_weight :
583
+ if end_weight < start_weight :
584
+ end = start
585
+ end_weight = start_weight
586
+ start = len (target_labels_index ) - 1
587
+ start_weight = centroid_x + centroid_y
588
+ elif centroid_x + centroid_y > end_weight :
589
+ end = len (target_labels_index ) - 1
590
+ end_weight = centroid_x + centroid_y
591
+
592
+ # build a graph
593
+ weight_graph = [[0 for i in range (len (target_labels_index ))] for _ in range (len (target_labels_index ))]
594
+ for i in range (len (target_labels_index )):
595
+ centroid_z_i , centroid_y_i , centroid_x_i = regionprops [target_labels_index [i ]].centroid
596
+ for j in range (len (target_labels_index )):
597
+ if i == j :
598
+ weight_graph [j ][i ] = weight_graph [i ][j ] = current_image_width + current_image_height
599
+ continue
600
+ centroid_z_j , centroid_y_j , centroid_x_j = regionprops [target_labels_index [j ]].centroid
601
+ weight_graph [i ][j ] = abs (centroid_x_i - centroid_x_j ) + abs (centroid_y_i - centroid_y_j )
602
+ weight_graph [j ][i ] = weight_graph [i ][j ]
603
+ # find the appoximation shortest path in (x, y).
604
+ visited_num = 1
605
+ target_labels = [target_labels_index [start ]]
606
+ pre = start
607
+ visited = [False ] * len (target_labels_index )
608
+ visited [start ] = True
609
+ while visited_num < len (target_labels_index ):
610
+ while True :
611
+ idx = weight_graph [pre ].index (min (weight_graph [pre ]))
612
+ weight_graph [pre ][idx ] = weight_graph [idx ][pre ] = current_image_width + current_image_height
613
+ if not visited [idx ]:
614
+ break
615
+ target_labels .append (target_labels_index [idx ])
616
+ pre = idx
617
+ visited [idx ] = True
618
+ visited_num += 1
619
+
620
+ for i in target_labels :
621
+ min_z , min_y , min_x , max_z , max_y , max_x = regionprops [i ].bbox
622
+
568
623
num_x = math .ceil ((max_x - min_x ) / (x_pixel * (1 - overlap )))
569
624
shift_x = (num_x * x_pixel - (max_x - min_x )) // 2
570
625
@@ -615,4 +670,4 @@ def map_labels(
615
670
for x_pos , y_pos in product (x_positions , y_positions )
616
671
]
617
672
618
- return z_range , position_table
673
+ return z_range , position_table , target_labels
0 commit comments