mask_to_objects.m 3.97 KB
Newer Older
1
function [ objects, junction_graph ] = mask_to_objects( I, downsample )
2
3
4
5
6
7
8
9
10
11
12
13
14
15
%% MASK_TO_CELL Returns the cells from a BW image with ridges.
% This function takes as input an image that was generated e.g. with the
% morphological segmentation technique, where each object is white and
% separated from its neighbor by a black ridge. It then returns the a
% struct array, 1 element per object, containing the object boundary, the
% junction id this object touches and its center.
% As a second output it returns the junction graph.
%
% This function only works for object in white with a ridge in black. The
% ridge must be connected using 8-connectivity.
%
% Jean-Yves Tinevez - Institut Pasteur - Nov 2019.


16
17
18
19
if nargin < 2
    downsample = true;
end

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
%% Create mask.

% Boundary mask.
M = I == 0;

%% Get dilated bounds.
% Bounds like we would not have the black ridge in between cells.

CC = bwconncomp( ~M, 4 );
n_cells = CC.NumObjects;
B = cell( n_cells, 1 );

% 4-connectivity strel.
se = strel( 'diamond', 1 );

for i = 1 : n_cells
    temp = false( CC.ImageSize );
    temp( CC.PixelIdxList{ i } ) = true;
    temp = imdilate( temp, se, 'same' );
Jean-Yves TINEVEZ's avatar
Jean-Yves TINEVEZ committed
39
    bounds = bwboundaries( temp, 8, 'noholes' );
40
41
42
43
44
    if downsample
        b = reducepoly( bounds{ 1 }, 0.005 );
    else
        b = bounds{ 1 };
    end
Jean-Yves TINEVEZ's avatar
Jean-Yves TINEVEZ committed
45
    B{ i } = b;
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
end

%% Remove cells touching the border.

[ width, height ] = size( M );
touching_border = cellfun( @(x) ...
    any( x(:) == 1 ) ...
    ||  any(x(:,1) == width ) ...
    ||  any(x(:,2) == height ),...
    B );
B( touching_border ) = [];
n_cells = numel( B );

%% Get the position of junction points.

% Define a function that only returns true for middle pixel of a 3x3
% neighborhood is >0 and with number of white neighbors >= 4.
f = @(x) (x(5) > 0 & sum(x(:)) >= 4);

% Make it a binary LUT for 3x3 neighborhood:
lut = makelut(f, 3);

% Apply the LUT to the mask image:
J = bwlookup( M, lut );

% Make label image with the junctions.
LJ = bwlabel( J );

% Measure junction centroids.
junction_table = regionprops('table', LJ, 'Centroid');
n_junctions = size(junction_table, 1 );

% Add junction id.
junction_table.ID = ( 1 : n_junctions )';

%% Link junctions together and create the output struct.

% Struct, one per cell.

% Junction graph.
junction_graph = graph( false( n_junctions, n_junctions ), junction_table );


% Loop over cells.
for i = n_cells : -1 : 1
    
    boundary = B{ i };
    n_pixels = size( boundary, 1 );
    previous_junction_id = -1;
    
    visited_junctions = [];
    
    for k = 1 : n_pixels
        
        x = boundary( k , 1 );
        y = boundary( k , 2 );
        junction_id = LJ( x, y );
        
        if  junction_id > 0
            
            visited_junctions = [
                visited_junctions
                junction_id ]; %#ok<AGROW>
            
            if previous_junction_id < 0
                first_junction_id = junction_id;
            end
            
            if previous_junction_id > 0 && previous_junction_id ~= junction_id
                % Link this junction to the previous one.
                idx = findedge( junction_graph, previous_junction_id, junction_id );
                
                if numel( idx ) <= 1 && idx <= 0
                    junction_graph = addedge( junction_graph, previous_junction_id, junction_id );
                end
            end
            
            previous_junction_id = junction_id;
        end
        
    end
    
    % Loop.
    if previous_junction_id > 0 && previous_junction_id ~= first_junction_id
        % Link this junction to the previous one.
        idx = findedge( junction_graph, previous_junction_id, first_junction_id );
        
        if numel( idx ) <= 1 && idx <= 0
            junction_graph = addedge( junction_graph, previous_junction_id, first_junction_id );
        end
    end
    
    objects( i ).junctions = unique( visited_junctions );
Jean-Yves TINEVEZ's avatar
Jean-Yves TINEVEZ committed
139
140
141
    % Permute X and Y.
    objects( i ).boundary = [ boundary(:,2) boundary(:,1) ];
    objects( i ).center = mean( objects( i ).boundary );
142
143
144
145
    
end

end