|
| 1 | +function map = chamferDistanceMap(obj, varargin) |
| 2 | +% Distance map of a binary image computed using chamfer mask. |
| 3 | +% |
| 4 | +% DISTMAP = chamferDistanceMap(IMG) |
| 5 | +% DISTMAP = chamferDistanceMap(IMG, WEIGHTS) |
| 6 | +% Computes the distance map of the input image using chamfer weights. The |
| 7 | +% aim of this function is similar to that of the "distanceMap" one, with |
| 8 | +% the following specificities: |
| 9 | +% * possibility to use 5-by-5 chamfer masks |
| 10 | +% * possibility to compute distance maps for label images with touching |
| 11 | +% regions. |
| 12 | +% |
| 13 | +% Example |
| 14 | +% chamferDistanceMap |
| 15 | +% |
| 16 | +% See also |
| 17 | +% distanceMap, geodesicDistanceMap |
| 18 | +% |
| 19 | + |
| 20 | +% ------ |
| 21 | +% Author: David Legland |
| 22 | +% e-mail: david.legland@inrae.fr |
| 23 | +% INRAE - BIA Research Unit - BIBS Platform (Nantes) |
| 24 | +% Created: 2021-11-18, using Matlab 9.10.0.1684407 (R2021a) Update 3 |
| 25 | +% Copyright 2021 INRAE. |
| 26 | + |
| 27 | +%% Process input arguments |
| 28 | + |
| 29 | +% default weights for orthogonal or diagonal |
| 30 | +weights = [5 7 11]; |
| 31 | + |
| 32 | +normalize = true; |
| 33 | + |
| 34 | +% extract user-specified weights |
| 35 | +if ~isempty(varargin) |
| 36 | + weights = varargin{1}; |
| 37 | + varargin(1) = []; |
| 38 | +end |
| 39 | + |
| 40 | +% extract verbosity option |
| 41 | +verbose = false; |
| 42 | +if length(varargin) > 1 |
| 43 | + varName = varargin{1}; |
| 44 | + if ~ischar(varName) |
| 45 | + error('Require options as name-value pairs'); |
| 46 | + end |
| 47 | + |
| 48 | + if strcmpi(varName, 'normalize') |
| 49 | + normalize = varargin{2}; |
| 50 | + elseif strcmpi(varName, 'verbose') |
| 51 | + verbose = varargin{2}; |
| 52 | + else |
| 53 | + error(['unknown option: ' varName]); |
| 54 | + end |
| 55 | +end |
| 56 | + |
| 57 | + |
| 58 | +%% Initialisations |
| 59 | + |
| 60 | +% determines type of output from type of weights |
| 61 | +outputType = class(weights); |
| 62 | + |
| 63 | +% small check up to avoid degenerate cases |
| 64 | +w1 = weights(1); |
| 65 | +w2 = weights(2); |
| 66 | +if w2 < w1 |
| 67 | + w2 = 2 * w1; |
| 68 | +end |
| 69 | + |
| 70 | +% shifts in directions i and j for (1) forward and (2) backward iterations |
| 71 | +if length(weights) == 2 |
| 72 | + nShifts = 4; |
| 73 | + di1 = [-1 -1 -1 0]; |
| 74 | + dj1 = [-1 0 1 -1]; |
| 75 | + di2 = [+1 +1 +1 0]; |
| 76 | + dj2 = [-1 0 1 +1]; |
| 77 | + ws = [w2 w1 w2 w1]; |
| 78 | + |
| 79 | +elseif length(weights) == 3 |
| 80 | + nShifts = 8; |
| 81 | + w3 = weights(3); |
| 82 | + di1 = [-2 -2 -1 -1 -1 -1 -1 0]; |
| 83 | + dj1 = [-1 +1 -2 -1 0 1 +2 -1]; |
| 84 | + di2 = [+2 +2 +1 +1 +1 +1 +1 0]; |
| 85 | + dj2 = [-1 +1 +2 +1 0 -1 -2 +1]; |
| 86 | + ws = [w3 w3 w3 w2 w1 w2 w3 w1]; |
| 87 | +end |
| 88 | + |
| 89 | +% allocate memory for result |
| 90 | +dist = ones(size(obj.Data), outputType); |
| 91 | + |
| 92 | +% init result: either max value, or 0 for marker pixels |
| 93 | +if isinteger(w1) |
| 94 | + dist(:) = intmax(outputType); |
| 95 | +else |
| 96 | + dist(:) = inf; |
| 97 | +end |
| 98 | +dist(obj.Data == 0) = 0; |
| 99 | + |
| 100 | +% size of image |
| 101 | +[D1, D2] = size(obj.Data); |
| 102 | + |
| 103 | + |
| 104 | +%% Forward iteration |
| 105 | + |
| 106 | +if verbose |
| 107 | + disp('Forward iteration %d'); |
| 108 | +end |
| 109 | + |
| 110 | +for i = 1:D1 |
| 111 | + for j = 1:D2 |
| 112 | + % computes only for pixels within a region |
| 113 | + if obj.Data(i, j) == 0 |
| 114 | + continue; |
| 115 | + end |
| 116 | + |
| 117 | + % compute minimal propagated distance |
| 118 | + newVal = dist(i, j); |
| 119 | + for k = 1:nShifts |
| 120 | + % coordinate of neighbor |
| 121 | + i2 = i + di1(k); |
| 122 | + j2 = j + dj1(k); |
| 123 | + |
| 124 | + % check bounds |
| 125 | + if i2 < 1 || i2 > D1 || j2 < 1 || j2 > D2 |
| 126 | + continue; |
| 127 | + end |
| 128 | + |
| 129 | + % compute new value |
| 130 | + if obj.Data(i2, j2) == obj.Data(i, j) |
| 131 | + % neighbor in same region |
| 132 | + % -> add offset weight to neighbor distance |
| 133 | + newVal = min(newVal, dist(i2, j2) + ws(k)); |
| 134 | + else |
| 135 | + % neighbor in another region |
| 136 | + % -> initialize with the offset weight |
| 137 | + newVal = min(newVal, ws(k)); |
| 138 | + end |
| 139 | + end |
| 140 | + |
| 141 | + % if distance was changed, update result |
| 142 | + dist(i,j) = newVal; |
| 143 | + end |
| 144 | + |
| 145 | +end % iteration on lines |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | +%% Backward iteration |
| 150 | + |
| 151 | +if verbose |
| 152 | + disp('Backward iteration'); |
| 153 | +end |
| 154 | + |
| 155 | +for i = D1:-1:1 |
| 156 | + for j = D2:-1:1 |
| 157 | + % computes only for foreground pixels |
| 158 | + if obj.Data(i, j) == 0 |
| 159 | + continue; |
| 160 | + end |
| 161 | + |
| 162 | + % compute minimal propagated distance |
| 163 | + newVal = dist(i, j); |
| 164 | + for k = 1:nShifts |
| 165 | + % coordinate of neighbor |
| 166 | + i2 = i + di2(k); |
| 167 | + j2 = j + dj2(k); |
| 168 | + |
| 169 | + % check bounds |
| 170 | + if i2 < 1 || i2 > D1 || j2 < 1 || j2 > D2 |
| 171 | + continue; |
| 172 | + end |
| 173 | + |
| 174 | + % compute new value |
| 175 | + if obj.Data(i2, j2) == obj.Data(i, j) |
| 176 | + % neighbor in same region |
| 177 | + % -> add offset weight to neighbor distance |
| 178 | + newVal = min(newVal, dist(i2, j2) + ws(k)); |
| 179 | + else |
| 180 | + % neighbor in another region |
| 181 | + % -> initialize with the offset weight |
| 182 | + newVal = min(newVal, ws(k)); |
| 183 | + end |
| 184 | + end |
| 185 | + |
| 186 | + % if distance was changed, update result |
| 187 | + dist(i,j) = newVal; |
| 188 | + end |
| 189 | + |
| 190 | +end % line iteration |
| 191 | + |
| 192 | +if normalize |
| 193 | + dist(obj.Data>0) = dist(obj.Data>0) / w1; |
| 194 | +end |
| 195 | + |
| 196 | +newName = createNewName(obj, '%s-distMap'); |
| 197 | + |
| 198 | +% create new image |
| 199 | +map = Image('Data', dist, ... |
| 200 | + 'Parent', obj, ... |
| 201 | + 'Name', newName, ... |
| 202 | + 'Type', 'intensity', ... |
| 203 | + 'ChannelNames', {'distance'}); |
0 commit comments