File size: 9,958 Bytes
69591a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
import numpy as np
import cv2
from typing import List, Tuple
from dnafiber.postprocess.skan import find_endpoints, compute_points_angle
from scipy.spatial.distance import cdist

from scipy.sparse.csgraph import connected_components
from scipy.sparse import csr_array
from skimage.morphology import skeletonize
from dnafiber.postprocess.skan import find_line_intersection
from dnafiber.postprocess.fiber import Fiber, FiberProps, Bbox
from itertools import compress
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap

cmlabel = ListedColormap(["black", "red", "green"])

MIN_ANGLE = 20
MIN_BRANCH_LENGTH = 10
MIN_BRANCH_DISTANCE = 30


def handle_multiple_fiber_in_cc(fiber, junctions_fiber, coordinates):
    for y, x in junctions_fiber:
        fiber[y - 1 : y + 2, x - 1 : x + 2] = 0

    endpoints = find_endpoints(fiber > 0)
    endpoints = np.asarray(endpoints)
    # We only keep the endpoints that are close to the junction
    # We compute the distance between the endpoints and the junctions
    distances = np.linalg.norm(
        np.expand_dims(endpoints, axis=1) - np.expand_dims(junctions_fiber, axis=0),
        axis=2,
    )
    # We only keep the endpoints that are close to the junctions
    distances = distances < 5
    endpoints = endpoints[distances.any(axis=1)]

    retval, branches, branches_stats, _ = cv2.connectedComponentsWithStatsWithAlgorithm(
        fiber, connectivity=8, ccltype=cv2.CCL_BOLELLI, ltype=cv2.CV_16U
    )
    branches_bboxes = branches_stats[
        :,
        [cv2.CC_STAT_LEFT, cv2.CC_STAT_TOP, cv2.CC_STAT_WIDTH, cv2.CC_STAT_HEIGHT],
    ]

    num_branches = branches_bboxes.shape[0] - 1
    # We associate the endpoints to the branches
    endpoints_ids = np.zeros((len(endpoints),), dtype=np.uint16)
    endpoints_color = np.zeros((len(endpoints),), dtype=np.uint8)
    for i, endpoint in enumerate(endpoints):
        # Get the branch id
        branch_id = branches[endpoint[0], endpoint[1]]
        # Check if the branch id is not 0
        if branch_id != 0:
            endpoints_ids[i] = branch_id
            endpoints_color[i] = fiber[endpoint[0], endpoint[1]]

    # We remove the small branches
    kept_branches = set()
    for i in range(1, num_branches + 1):
        # Get the branch
        branch = branches == i
        # Compute the area of the branch
        area = np.sum(branch.astype(np.uint8))
        # If the area is less than 10 pixels, remove the branch
        if area < MIN_BRANCH_LENGTH:
            branches[branch] = 0
        else:
            kept_branches.add(i)

    # We remove the endpoints that are in the filtered branches
    remaining_idxs = np.isin(endpoints_ids, np.asarray(list(kept_branches)))
    if remaining_idxs.sum() == 0:
        return []
    endpoints = endpoints[remaining_idxs]

    endpoints_color = endpoints_color[remaining_idxs]
    endpoints_ids = endpoints_ids[remaining_idxs]

    # We compute the angles of the endpoints
    angles = compute_points_angle(fiber, endpoints, steps=15)
    angles = np.rad2deg(angles)
    # We compute the difference of angles between all the endpoints
    endpoints_angles_diff = cdist(angles[:, None], angles[:, None], metric="cityblock")

    # Put inf to the diagonal
    endpoints_angles_diff[range(len(endpoints)), range(len(endpoints))] = np.inf
    endpoints_distances = cdist(endpoints, endpoints, metric="euclidean")

    endpoints_distances[range(len(endpoints)), range(len(endpoints))] = np.inf

    # We sort by the distance
    endpoints_distances[endpoints_distances > MIN_BRANCH_DISTANCE] = np.inf
    endpoints_distances[endpoints_angles_diff > MIN_ANGLE] = np.inf

    matchB = np.argmin(endpoints_distances, axis=1)
    values = np.take_along_axis(endpoints_distances, matchB[:, None], axis=1)

    added_edges = dict()
    N = len(endpoints)
    A = np.eye(N, dtype=np.uint8)
    for i in range(N):
        for j in range(N):
            if i == j:
                continue
            if endpoints_ids[i] == endpoints_ids[j]:
                A[i, j] = 1
                A[j, i] = 1

            if matchB[i] == j and values[i, 0] < np.inf:
                added_edges[i] = j
                A[i, j] = 1
                A[j, i] = 1

    A = csr_array(A)
    n, ccs = connected_components(A, directed=False, return_labels=True)
    unique_clusters = np.unique(ccs)
    results = []
    for c in unique_clusters:
        idx = np.where(ccs == c)[0]
        branches_ids = np.unique(endpoints_ids[idx])

        unique_branches = np.logical_or.reduce(
            [branches == i for i in branches_ids], axis=0
        )

        commons_bboxes = branches_bboxes[branches_ids]
        # Compute the union of the bboxes
        min_x = np.min(commons_bboxes[:, 0])
        min_y = np.min(commons_bboxes[:, 1])
        max_x = np.max(commons_bboxes[:, 0] + commons_bboxes[:, 2])
        max_y = np.max(commons_bboxes[:, 1] + commons_bboxes[:, 3])

        new_fiber = fiber[min_y:max_y, min_x:max_x]
        new_fiber = unique_branches[min_y:max_y, min_x:max_x] * new_fiber
        for cidx in idx:
            if cidx not in added_edges:
                continue
            pointA = endpoints[cidx]
            pointB = endpoints[added_edges[cidx]]
            pointA = (
                pointA[1] - min_x,
                pointA[0] - min_y,
            )
            pointB = (
                pointB[1] - min_x,
                pointB[0] - min_y,
            )
            colA = endpoints_color[cidx]
            colB = endpoints_color[added_edges[cidx]]
            new_fiber = cv2.line(
                new_fiber,
                pointA,
                pointB,
                color=2 if colA != colB else int(colA),
                thickness=1,
            )
        # We express the bbox in the original image
        bbox = (
            coordinates[0] + min_x,
            coordinates[1] + min_y,
            max_x - min_x,
            max_y - min_y,
        )
        bbox = Bbox(x=bbox[0], y=bbox[1], width=bbox[2], height=bbox[3])
        result = Fiber(bbox=bbox, data=new_fiber)
        results.append(result)
    return results


def handle_ccs_with_junctions(

    ccs: List[np.ndarray],

    junctions: List[List[Tuple[int, int]]],

    coordinates: List[Tuple[int, int]],

):
    """

    Handle the connected components with junctions.

    The function takes a list of connected components, a list of list of junctions and a list of coordinates.

    The junctions

    The coordinates corresponds to the top left corner of the connected component.

    """
    jncts_fibers = []
    for fiber, junction, coordinate in zip(ccs, junctions, coordinates):
        jncts_fibers += handle_multiple_fiber_in_cc(fiber, junction, coordinate)

    return jncts_fibers


def refine_segmentation(segmentation, fix_junctions=True, show=False):
    skeleton = skeletonize(segmentation > 0, method="lee").astype(np.uint8)
    skeleton_gt = skeleton * segmentation
    retval, labels, stats, centroids = cv2.connectedComponentsWithStatsWithAlgorithm(
        skeleton, connectivity=8, ccltype=cv2.CCL_BOLELLI, ltype=cv2.CV_16U
    )

    bboxes = stats[
        :,
        [
            cv2.CC_STAT_LEFT,
            cv2.CC_STAT_TOP,
            cv2.CC_STAT_WIDTH,
            cv2.CC_STAT_HEIGHT,
        ],
    ]

    local_fibers = []
    coordinates = []
    junctions = []
    for i in range(1, retval):
        bbox = bboxes[i]
        x1, y1, w, h = bbox
        local_gt = skeleton_gt[y1 : y1 + h, x1 : x1 + w]
        local_label = (labels[y1 : y1 + h, x1 : x1 + w] == i).astype(np.uint8)
        local_fiber = local_gt * local_label
        local_fibers.append(local_fiber)
        coordinates.append(np.asarray([x1, y1, w, h]))
        local_junctions = find_line_intersection(local_fiber > 0)
        local_junctions = np.where(local_junctions)
        local_junctions = np.array(local_junctions).transpose()
        junctions.append(local_junctions)
    if show:
        for bbox, junction in zip(coordinates, junctions):
            x, y, w, h = bbox
            junction_to_global = np.array(junction) + np.array([y, x])

            plt.scatter(
                junction_to_global[:, 1],
                junction_to_global[:, 0],
                color="white",
                s=30,
                alpha=0.35,
            )

        plt.imshow(skeleton_gt, cmap=cmlabel, interpolation="nearest")
        plt.axis("off")
        plt.xticks([])
        plt.yticks([])
        plt.subplots_adjust(left=0, right=1, top=1, bottom=0)
        plt.show()

    fibers = []
    if fix_junctions:
        has_junctions = [len(j) > 0 for j in junctions]
        for fiber, coordinate in zip(
            compress(local_fibers, np.logical_not(has_junctions)),
            compress(coordinates, np.logical_not(has_junctions)),
        ):
            bbox = Bbox(
                x=coordinate[0],
                y=coordinate[1],
                width=coordinate[2],
                height=coordinate[3],
            )
            fibers.append(Fiber(bbox=bbox, data=fiber))

        fibers += handle_ccs_with_junctions(
            compress(local_fibers, has_junctions),
            compress(junctions, has_junctions),
            compress(coordinates, has_junctions),
        )
    else:
        for fiber, coordinate in zip(local_fibers, coordinates):
            bbox = Bbox(
                x=coordinate[0],
                y=coordinate[1],
                width=coordinate[2],
                height=coordinate[3],
            )
            fibers.append(Fiber(bbox=bbox, data=fiber))

    fiberprops = [FiberProps(fiber=f, fiber_id=i) for i, f in enumerate(fibers)]

    return fiberprops