Mercurial > hg > octave-image
annotate inst/regionprops.m @ 868:b42bfcfed81d
regionprops.m: Return a column vector for Matlab compatibility
author | Jordi Gutiérrez Hermoso <jordigh@octave.org> |
---|---|
date | Mon, 10 Feb 2014 10:41:30 -0500 |
parents | 6db5e3c6759b |
children | 5e598ccaf0c7 |
rev | line source |
---|---|
709 | 1 ## Copyright (C) 2010 Søren Hauberg <soren@hauberg.org> |
585 | 2 ## Copyright (C) 2012 Jordi Gutiérrez Hermoso <jordigh@octave.org> |
393 | 3 ## |
561
c45838839d86
maint: update license to GPLv3 and mention non GPL files
carandraug
parents:
396
diff
changeset
|
4 ## This program is free software; you can redistribute it and/or modify it under |
c45838839d86
maint: update license to GPLv3 and mention non GPL files
carandraug
parents:
396
diff
changeset
|
5 ## the terms of the GNU General Public License as published by the Free Software |
c45838839d86
maint: update license to GPLv3 and mention non GPL files
carandraug
parents:
396
diff
changeset
|
6 ## Foundation; either version 3 of the License, or (at your option) any later |
c45838839d86
maint: update license to GPLv3 and mention non GPL files
carandraug
parents:
396
diff
changeset
|
7 ## version. |
393 | 8 ## |
561
c45838839d86
maint: update license to GPLv3 and mention non GPL files
carandraug
parents:
396
diff
changeset
|
9 ## This program is distributed in the hope that it will be useful, but WITHOUT |
c45838839d86
maint: update license to GPLv3 and mention non GPL files
carandraug
parents:
396
diff
changeset
|
10 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
c45838839d86
maint: update license to GPLv3 and mention non GPL files
carandraug
parents:
396
diff
changeset
|
11 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
c45838839d86
maint: update license to GPLv3 and mention non GPL files
carandraug
parents:
396
diff
changeset
|
12 ## details. |
393 | 13 ## |
561
c45838839d86
maint: update license to GPLv3 and mention non GPL files
carandraug
parents:
396
diff
changeset
|
14 ## You should have received a copy of the GNU General Public License along with |
c45838839d86
maint: update license to GPLv3 and mention non GPL files
carandraug
parents:
396
diff
changeset
|
15 ## this program; if not, see <http://www.gnu.org/licenses/>. |
393 | 16 |
17 ## -*- texinfo -*- | |
18 ## @deftypefn {Function File} {@var{props} = } regionprops (@var{BW}) | |
19 ## @deftypefnx {Function File} {@var{props} = } regionprops (@var{BW}, @var{properties}, @dots{}) | |
782
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
20 ## @deftypefnx {Function File} {@var{props} = } regionprops (@var{L}, @var{properties}, @dots{}) |
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
21 ## @deftypefnx {Function File} {@var{props} = } regionprops (@dots{}, @var{I}, @var{properties}, @dots{}) |
393 | 22 ## Compute object properties in a binary image. |
23 ## | |
24 ## @code{regionprops} computes various properties of the individual objects (as | |
25 ## identified by @code{bwlabel}) in the binary image @var{BW}. The result is a | |
26 ## structure array containing an entry per property per object. | |
27 ## | |
782
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
28 ## The optional grayscale image @var{I} is used for pixel value measurements |
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
29 ## (MaxIntensity, MinIntensity, MeanIntensity, PixelValues and WeightedCentroid). |
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
30 ## |
593
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
31 ## The following properties can be computed: |
393 | 32 ## |
593
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
33 ## @table @asis |
393 | 34 ## @item "Area" |
35 ## The number of pixels in the object. | |
593
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
36 ## |
393 | 37 ## @item "BoundingBox" |
38 ## @itemx "bounding_box" | |
39 ## The bounding box of the object. This is represented as a 4-vector where the | |
40 ## first two entries are the @math{x} and @math{y} coordinates of the upper left | |
41 ## corner of the bounding box, and the two last entries are the width and the | |
42 ## height of the box. | |
593
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
43 ## |
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
44 ## @item "Centroid" |
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
45 ## The center coordinate of the object. |
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
46 ## |
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
47 ## @item "EulerNumber" |
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
48 ## @itemx "euler_number" |
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
49 ## The Euler number of the object (see @code{bweuler} for details). |
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
50 ## |
393 | 51 ## @item "Extent" |
52 ## The area of the object divided by the area of the bounding box. | |
593
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
53 ## |
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
54 ## @item "FilledArea" |
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
55 ## @itemx "filled_area" |
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
56 ## The area of the object including possible holes. |
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
57 ## |
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
58 ## @item "FilledImage" |
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
59 ## @itemx "filled_image" |
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
60 ## A binary image with the same size as the object's bounding box that contains |
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
61 ## the object with all holes removed. |
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
62 ## |
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
63 ## @item "Image" |
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
64 ## An image with the same size as the bounding box that contains the original pixels. |
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
65 ## |
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
66 ## @item "MaxIntensity" |
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
67 ## @itemx "max_intensity" |
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
68 ## The maximum intensity inside the object. |
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
69 ## |
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
70 ## @item "MeanIntensity" |
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
71 ## @itemx "mean_intensity" |
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
72 ## The mean intensity inside the object. |
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
73 ## |
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
74 ## @item "MinIntensity" |
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
75 ## @itemx "min_intensity" |
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
76 ## The minimum intensity inside the object. |
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
77 ## |
393 | 78 ## @item "Perimeter" |
79 ## The length of the boundary of the object. | |
593
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
80 ## |
393 | 81 ## @item "PixelIdxList" |
82 ## @itemx "pixel_idx_list" | |
83 ## The indices of the pixels in the object. | |
593
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
84 ## |
393 | 85 ## @item "PixelList" |
86 ## @itemx "pixel_list" | |
87 ## The actual pixel values inside the object. This is only useful for grey scale | |
88 ## images. | |
593
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
89 ## |
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
90 ## @item "PixelValues" |
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
91 ## @itemx "pixel_values" |
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
92 ## The pixel values inside the object represented as a vector. |
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
93 ## |
393 | 94 ## @item "WeightedCentroid" |
95 ## @itemx "weighted_centroid" | |
96 ## The centroid of the object where pixel values are used as weights. | |
97 ## @end table | |
98 ## | |
99 ## The requested properties can either be specified as several input arguments | |
100 ## or as a cell array of strings. As a short-hand it is also possible to give | |
101 ## the following strings as arguments. | |
102 ## | |
593
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
103 ## @table @asis |
393 | 104 ## @item "basic" |
593
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
105 ## The following properties are computed: @t{"Area"}, @t{"Centroid"} and |
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
106 ## @t{"BoundingBox"}. This is the default. |
f9673d75423d
regionprops: alphabetic order of options in help text
carandraug
parents:
585
diff
changeset
|
107 ## |
393 | 108 ## @item "all" |
109 ## All properties are computed. | |
110 ## @end table | |
111 ## | |
112 ## @seealso{bwlabel, bwperim, bweuler} | |
113 ## @end deftypefn | |
114 | |
115 function retval = regionprops (bw, varargin) | |
116 ## Check input | |
117 if (nargin < 1) | |
118 error ("regionprops: not enough input arguments"); | |
119 endif | |
120 | |
782
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
121 prop_start = 1; |
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
122 if (numel (varargin) >= 1 && isnumeric (varargin{1})) |
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
123 if (size_equal (bw, varargin{1})) |
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
124 I = varargin{1}; |
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
125 varargin(1) = []; |
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
126 else |
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
127 error ("regionprops: I must have the same size as BW"); |
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
128 endif |
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
129 else |
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
130 I = bw; |
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
131 endif |
393 | 132 if (numel (varargin) == 0) |
585 | 133 properties = {"basic"}; |
134 elseif (numel (varargin) == 1 && iscellstr (varargin{1})) | |
135 properties = varargin{1}; | |
393 | 136 elseif (iscellstr (varargin)) |
137 properties = varargin; | |
138 else | |
139 error ("regionprops: properties must be a cell array of strings"); | |
140 endif | |
585 | 141 |
142 properties = lower (properties); | |
143 | |
775
f4e0686fcf82
* __conditional_mark_patterns_lut_fun__.m, cp2tform.m, fchcode.m,
John W. Eaton <jwe@octave.org>
parents:
772
diff
changeset
|
144 all_props = {"Area", "EulerNumber", "BoundingBox", "Extent", "Perimeter",... |
f4e0686fcf82
* __conditional_mark_patterns_lut_fun__.m, cp2tform.m, fchcode.m,
John W. Eaton <jwe@octave.org>
parents:
772
diff
changeset
|
145 "Centroid", "PixelIdxList", "FilledArea", "PixelList",... |
f4e0686fcf82
* __conditional_mark_patterns_lut_fun__.m, cp2tform.m, fchcode.m,
John W. Eaton <jwe@octave.org>
parents:
772
diff
changeset
|
146 "FilledImage", "Image", "MaxIntensity", "MinIntensity",... |
f4e0686fcf82
* __conditional_mark_patterns_lut_fun__.m, cp2tform.m, fchcode.m,
John W. Eaton <jwe@octave.org>
parents:
772
diff
changeset
|
147 "WeightedCentroid", "MeanIntensity", "PixelValues",... |
585 | 148 "Orientation"}; |
782
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
149 |
585 | 150 if (ismember ("basic", properties)) |
151 properties = union (properties, {"Area", "Centroid", "BoundingBox"}); | |
152 properties = setdiff (properties, "basic"); | |
153 endif | |
154 | |
155 if (ismember ("all", properties)) | |
156 properties = all_props; | |
157 endif | |
782
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
158 |
585 | 159 if (!iscellstr (properties)) |
393 | 160 error ("%s %s", "regionprops: properties must be specified as a list of", |
161 "strings or a cell array of strings"); | |
162 endif | |
163 | |
585 | 164 ## Fix capitalisation, underscores of user-supplied properties... |
165 for k = 1:numel (properties) | |
166 property = lower (strrep(properties{k}, "_", "")); | |
167 [~, idx] = ismember (property, lower (all_props)); | |
168 if (!idx) | |
169 error ("regionprops: unsupported property: %s", property); | |
170 endif | |
171 properties(k) = all_props{idx}; | |
172 endfor | |
782
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
173 |
585 | 174 N = ndims (bw); |
175 | |
393 | 176 ## Get a labelled image |
177 if (!islogical (bw) && all (bw >= 0) && all (bw == round (bw))) | |
178 L = bw; # the image was already labelled | |
179 num_labels = max (L (:)); | |
585 | 180 elseif (N > 2) |
181 [L, num_labels] = bwlabeln (bw); | |
393 | 182 else |
396 | 183 [L, num_labels] = bwlabel (bw); |
393 | 184 endif |
782
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
185 |
585 | 186 ## Return an empty struct with specified properties if there are no labels |
187 if num_labels == 0 | |
188 retval = struct ([properties; repmat({{}}, size(properties))]{:}); | |
189 return; | |
190 endif | |
191 | |
393 | 192 ## Compute the properties |
193 retval = struct (); | |
585 | 194 for property = lower(properties) |
195 property = property{:}; | |
196 switch (property) | |
393 | 197 case "area" |
198 for k = 1:num_labels | |
199 retval (k).Area = local_area (L == k); | |
200 endfor | |
782
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
201 |
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
202 case "eulernumber" |
393 | 203 for k = 1:num_labels |
204 retval (k).EulerNumber = bweuler (L == k); | |
205 endfor | |
206 | |
782
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
207 case "boundingbox" |
393 | 208 for k = 1:num_labels |
209 retval (k).BoundingBox = local_boundingbox (L == k); | |
210 endfor | |
211 | |
212 case "extent" | |
213 for k = 1:num_labels | |
214 bb = local_boundingbox (L == k); | |
215 area = local_area (L == k); | |
585 | 216 idx = length (bb)/2 + 1; |
217 retval (k).Extent = area / prod (bb(idx:end)); | |
393 | 218 endfor |
219 | |
220 case "perimeter" | |
585 | 221 if (N > 2) |
222 warning ("regionprops: skipping perimeter for Nd image"); | |
223 else | |
224 for k = 1:num_labels | |
225 retval (k).Perimeter = sum (bwperim (L == k) (:)); | |
226 endfor | |
227 endif | |
393 | 228 |
229 case "centroid" | |
230 for k = 1:num_labels | |
738 | 231 C = all_coords (L == k, true); |
585 | 232 retval (k).Centroid = [mean(C)]; |
393 | 233 endfor |
234 | |
782
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
235 case "pixelidxlist" |
393 | 236 for k = 1:num_labels |
237 retval (k).PixelIdxList = find (L == k); | |
238 endfor | |
782
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
239 |
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
240 case "filledarea" |
393 | 241 for k = 1:num_labels |
242 retval (k).FilledArea = sum (bwfill (L == k, "holes") (:)); | |
243 endfor | |
244 | |
782
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
245 case "pixellist" |
393 | 246 for k = 1:num_labels |
585 | 247 C = all_coords (L == k, true, true); |
248 retval (k).PixelList = C; | |
393 | 249 endfor |
250 | |
782
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
251 case "filledimage" |
393 | 252 for k = 1:num_labels |
253 retval (k).FilledImage = bwfill (L == k, "holes"); | |
254 endfor | |
255 | |
256 case "image" | |
257 for k = 1:num_labels | |
258 tmp = (L == k); | |
585 | 259 C = all_coords (tmp, false); |
260 idx = arrayfun (@(x,y) x:y, min (C), max (C), "unif", 0); | |
261 idx = substruct ("()", idx); | |
262 retval (k).Image = subsref (tmp, idx); | |
393 | 263 endfor |
264 | |
782
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
265 case "maxintensity" |
393 | 266 for k = 1:num_labels |
782
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
267 retval (k).MaxIntensity = max (I(L == k)(:)); |
393 | 268 endfor |
782
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
269 |
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
270 case "minintensity" |
393 | 271 for k = 1:num_labels |
782
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
272 retval (k).MinIntensity = min (I(L == k)(:)); |
393 | 273 endfor |
782
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
274 |
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
275 case "weightedcentroid" |
393 | 276 for k = 1:num_labels |
585 | 277 C = all_coords (L == k, true, true); |
782
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
278 vals = I(L == k)(:); |
393 | 279 vals /= sum (vals); |
782
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
280 retval (k).WeightedCentroid = [dot(C, repmat(vals, 1, columns(C)), 1)]; |
393 | 281 endfor |
282 | |
782
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
283 case "meanintensity" |
393 | 284 for k = 1:num_labels |
782
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
285 retval (k).MeanIntensity = mean (I(L == k)(:)); |
393 | 286 endfor |
782
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
287 |
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
288 case "pixelvalues" |
393 | 289 for k = 1:num_labels |
782
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
290 retval (k).PixelValues = I(L == k)(:); |
393 | 291 endfor |
782
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
292 |
396 | 293 case "orientation" |
585 | 294 if (N > 2) |
295 warning ("regionprops: skipping orientation for Nd image"); | |
296 break | |
297 endif | |
298 | |
396 | 299 for k = 1:num_labels |
300 [Y, X] = find (L == k); | |
301 if (numel (Y) > 1) | |
302 C = cov ([X(:), Y(:)]); | |
303 [V, lambda] = eig (C); | |
304 [max_val, max_idx] = max (diag (lambda)); | |
305 v = V (:, max_idx); | |
306 retval (k).Orientation = 180 - 180 * atan2 (v (2), v (1)) / pi; | |
307 else | |
308 retval (k).Orientation = 0; # XXX: What does the other brand do? | |
309 endif | |
310 endfor | |
782
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
311 |
396 | 312 %{ |
313 case "majoraxislength" | |
314 for k = 1:num_labels | |
315 [Y, X] = find (L == k); | |
316 if (numel (Y) > 1) | |
317 C = cov ([X(:), Y(:)]); | |
318 lambda = eig (C); | |
319 retval (k).MajorAxisLength = (max (lambda)); | |
320 else | |
321 retval (k).MajorAxisLength = 1; | |
322 endif | |
323 endfor | |
782
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
324 |
396 | 325 case "minoraxislength" |
326 for k = 1:num_labels | |
327 [Y, X] = find (L == k); | |
328 if (numel (Y) > 1) | |
329 C = cov ([X(:), Y(:)]); | |
330 lambda = eig (C); | |
331 retval (k).MinorAxisLength = (min (lambda)); | |
332 else | |
333 retval (k).MinorAxisLength = 1; | |
334 endif | |
335 endfor | |
336 %} | |
782
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
337 |
393 | 338 #case "extrema" |
782
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
339 #case "convexarea" |
393 | 340 #case "convexhull" |
341 #case "solidity" | |
342 #case "conveximage" | |
343 #case "subarrayidx" | |
344 #case "eccentricity" | |
345 #case "equivdiameter" | |
346 | |
347 otherwise | |
585 | 348 error ("regionprops: unsupported property '%s'", property); |
393 | 349 endswitch |
350 endfor | |
868
b42bfcfed81d
regionprops.m: Return a column vector for Matlab compatibility
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
782
diff
changeset
|
351 ## Matlab returns a column vector struct array. |
b42bfcfed81d
regionprops.m: Return a column vector for Matlab compatibility
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
782
diff
changeset
|
352 retval = retval(:); |
393 | 353 endfunction |
354 | |
355 function retval = local_area (bw) | |
356 retval = sum (bw (:)); | |
357 endfunction | |
358 | |
359 function retval = local_boundingbox (bw) | |
585 | 360 C = all_coords (bw); |
361 retval = [min(C) - 0.5, max(C) - min(C) + 1]; | |
393 | 362 endfunction |
585 | 363 |
364 function C = all_coords (bw, flip = true, singleton = false) | |
365 N = ndims (bw); | |
366 idx = find (bw); | |
772
ab0ac77d350b
fixed regionprops 'centroid' (bug #39701)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
738
diff
changeset
|
367 C = cell2mat (nthargout (1:N, @ind2sub, size(bw), idx(:))); |
585 | 368 |
369 ## Coordinate convention for 2d images is to flip the X and Y axes | |
370 ## relative to matrix indexing. Nd images inherit this for the first | |
371 ## two dimensions. | |
372 if (flip) | |
772
ab0ac77d350b
fixed regionprops 'centroid' (bug #39701)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
738
diff
changeset
|
373 [C(:, 2), C(:, 1)] = deal (C(:, 1), C(:, 2)); |
585 | 374 endif |
375 | |
376 ## Some functions above expect to work columnwise, so don't return a | |
377 ## vector | |
378 if (rows (C) == 1 && !singleton) | |
379 C = [C; C]; | |
380 endif | |
381 endfunction | |
772
ab0ac77d350b
fixed regionprops 'centroid' (bug #39701)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
738
diff
changeset
|
382 |
ab0ac77d350b
fixed regionprops 'centroid' (bug #39701)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
738
diff
changeset
|
383 %!test |
ab0ac77d350b
fixed regionprops 'centroid' (bug #39701)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
738
diff
changeset
|
384 %! c = regionprops ([0 0 1], 'centroid'); |
ab0ac77d350b
fixed regionprops 'centroid' (bug #39701)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
738
diff
changeset
|
385 %! assert (c.Centroid, [3 1]) |
ab0ac77d350b
fixed regionprops 'centroid' (bug #39701)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
738
diff
changeset
|
386 |
ab0ac77d350b
fixed regionprops 'centroid' (bug #39701)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
738
diff
changeset
|
387 %!test |
ab0ac77d350b
fixed regionprops 'centroid' (bug #39701)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
738
diff
changeset
|
388 %! c = regionprops ([0 0 1; 0 0 0], 'centroid'); |
ab0ac77d350b
fixed regionprops 'centroid' (bug #39701)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
738
diff
changeset
|
389 %! assert (c.Centroid, [3 1]) |
ab0ac77d350b
fixed regionprops 'centroid' (bug #39701)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
738
diff
changeset
|
390 |
ab0ac77d350b
fixed regionprops 'centroid' (bug #39701)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
738
diff
changeset
|
391 %!test |
ab0ac77d350b
fixed regionprops 'centroid' (bug #39701)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
738
diff
changeset
|
392 %! c = regionprops ([0 1 1], 'centroid'); #bug 39701 |
ab0ac77d350b
fixed regionprops 'centroid' (bug #39701)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
738
diff
changeset
|
393 %! assert (c.Centroid, [2.5 1]) |
782
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
394 |
772
ab0ac77d350b
fixed regionprops 'centroid' (bug #39701)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
738
diff
changeset
|
395 %!test |
ab0ac77d350b
fixed regionprops 'centroid' (bug #39701)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
738
diff
changeset
|
396 %! c = regionprops([0 1 1; 0 0 0], 'centroid'); #bug 39701 |
ab0ac77d350b
fixed regionprops 'centroid' (bug #39701)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
738
diff
changeset
|
397 %! assert (c.Centroid, [2.5 1]) |
ab0ac77d350b
fixed regionprops 'centroid' (bug #39701)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
738
diff
changeset
|
398 |
ab0ac77d350b
fixed regionprops 'centroid' (bug #39701)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
738
diff
changeset
|
399 %!test |
ab0ac77d350b
fixed regionprops 'centroid' (bug #39701)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
738
diff
changeset
|
400 %! a = zeros (2, 3, 3); |
ab0ac77d350b
fixed regionprops 'centroid' (bug #39701)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
738
diff
changeset
|
401 %! a(:, :, 1) = [0 1 0; 0 0 0]; |
ab0ac77d350b
fixed regionprops 'centroid' (bug #39701)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
738
diff
changeset
|
402 %! a(:, :, 3) = a(:, :, 1); |
ab0ac77d350b
fixed regionprops 'centroid' (bug #39701)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
738
diff
changeset
|
403 %! c = regionprops (a, 'centroid'); |
ab0ac77d350b
fixed regionprops 'centroid' (bug #39701)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
738
diff
changeset
|
404 %! assert (c.Centroid, [2 1 2]) |
ab0ac77d350b
fixed regionprops 'centroid' (bug #39701)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
738
diff
changeset
|
405 |
ab0ac77d350b
fixed regionprops 'centroid' (bug #39701)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
738
diff
changeset
|
406 %!test |
ab0ac77d350b
fixed regionprops 'centroid' (bug #39701)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
738
diff
changeset
|
407 %! d1=2; d2=4; d3=6; |
ab0ac77d350b
fixed regionprops 'centroid' (bug #39701)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
738
diff
changeset
|
408 %! a = ones (d1, d2, d3); |
ab0ac77d350b
fixed regionprops 'centroid' (bug #39701)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
738
diff
changeset
|
409 %! c = regionprops (a, 'centroid'); |
ab0ac77d350b
fixed regionprops 'centroid' (bug #39701)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
738
diff
changeset
|
410 %! assert (c.Centroid, [mean(1:d2), mean(1:d1), mean(1:d3)], eps) |
ab0ac77d350b
fixed regionprops 'centroid' (bug #39701)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
738
diff
changeset
|
411 |
ab0ac77d350b
fixed regionprops 'centroid' (bug #39701)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
738
diff
changeset
|
412 %!test |
ab0ac77d350b
fixed regionprops 'centroid' (bug #39701)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
738
diff
changeset
|
413 %! a = [0 0 2 2; 3 3 0 0; 0 1 0 1]; |
ab0ac77d350b
fixed regionprops 'centroid' (bug #39701)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
738
diff
changeset
|
414 %! c = regionprops (a, 'centroid'); |
ab0ac77d350b
fixed regionprops 'centroid' (bug #39701)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
738
diff
changeset
|
415 %! assert (c(1).Centroid, [3 3], eps) |
ab0ac77d350b
fixed regionprops 'centroid' (bug #39701)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
738
diff
changeset
|
416 %! assert (c(2).Centroid, [3.5 1], eps) |
ab0ac77d350b
fixed regionprops 'centroid' (bug #39701)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
738
diff
changeset
|
417 %! assert (c(3).Centroid, [1.5 2], eps) |
782
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
418 |
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
419 %!test |
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
420 %! img = zeros (3, 9); |
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
421 %! img(2, 1:9) = 0:0.1:0.8; |
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
422 %! bw = im2bw (img, 0.5); |
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
423 %! props = regionprops(bw, img, "WeightedCentroid"); |
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
424 %! ix = 7:9; |
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
425 %! x = sum (img(2,ix) .* (ix)) / sum (img(2,ix)); |
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
426 %! assert (props(1).WeightedCentroid(1), x, 10*eps) |
6db5e3c6759b
inst/regionprops.m: Added handling of labeled region in image I and cleanup
Andreas Weber <andy.weber.aw@gmail.com>
parents:
775
diff
changeset
|
427 %! assert (props(1).WeightedCentroid(2), 2, 10*eps) |