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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
709
c6be7812523a maint: fix Søren's name
carandraug
parents: 593
diff changeset
1 ## Copyright (C) 2010 Søren Hauberg <soren@hauberg.org>
585
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
2 ## Copyright (C) 2012 Jordi Gutiérrez Hermoso <jordigh@octave.org>
393
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
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
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
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
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
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
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
16
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
17 ## -*- texinfo -*-
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
18 ## @deftypefn {Function File} {@var{props} = } regionprops (@var{BW})
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
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
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
22 ## Compute object properties in a binary image.
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
23 ##
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
24 ## @code{regionprops} computes various properties of the individual objects (as
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
25 ## identified by @code{bwlabel}) in the binary image @var{BW}. The result is a
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
26 ## structure array containing an entry per property per object.
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
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
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
32 ##
593
f9673d75423d regionprops: alphabetic order of options in help text
carandraug
parents: 585
diff changeset
33 ## @table @asis
393
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
34 ## @item "Area"
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
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
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
37 ## @item "BoundingBox"
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
38 ## @itemx "bounding_box"
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
39 ## The bounding box of the object. This is represented as a 4-vector where the
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
40 ## first two entries are the @math{x} and @math{y} coordinates of the upper left
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
41 ## corner of the bounding box, and the two last entries are the width and the
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
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
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
51 ## @item "Extent"
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
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
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
78 ## @item "Perimeter"
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
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
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
81 ## @item "PixelIdxList"
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
82 ## @itemx "pixel_idx_list"
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
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
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
85 ## @item "PixelList"
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
86 ## @itemx "pixel_list"
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
87 ## The actual pixel values inside the object. This is only useful for grey scale
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
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
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
94 ## @item "WeightedCentroid"
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
95 ## @itemx "weighted_centroid"
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
96 ## The centroid of the object where pixel values are used as weights.
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
97 ## @end table
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
98 ##
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
99 ## The requested properties can either be specified as several input arguments
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
100 ## or as a cell array of strings. As a short-hand it is also possible to give
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
101 ## the following strings as arguments.
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
102 ##
593
f9673d75423d regionprops: alphabetic order of options in help text
carandraug
parents: 585
diff changeset
103 ## @table @asis
393
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
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
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
108 ## @item "all"
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
109 ## All properties are computed.
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
110 ## @end table
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
111 ##
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
112 ## @seealso{bwlabel, bwperim, bweuler}
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
113 ## @end deftypefn
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
114
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
115 function retval = regionprops (bw, varargin)
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
116 ## Check input
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
117 if (nargin < 1)
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
118 error ("regionprops: not enough input arguments");
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
119 endif
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
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
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
132 if (numel (varargin) == 0)
585
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
133 properties = {"basic"};
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
134 elseif (numel (varargin) == 1 && iscellstr (varargin{1}))
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
135 properties = varargin{1};
393
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
136 elseif (iscellstr (varargin))
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
137 properties = varargin;
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
138 else
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
139 error ("regionprops: properties must be a cell array of strings");
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
140 endif
585
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
141
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
142 properties = lower (properties);
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
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
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
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
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
150 if (ismember ("basic", properties))
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
151 properties = union (properties, {"Area", "Centroid", "BoundingBox"});
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
152 properties = setdiff (properties, "basic");
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
153 endif
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
154
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
155 if (ismember ("all", properties))
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
156 properties = all_props;
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
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
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
159 if (!iscellstr (properties))
393
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
160 error ("%s %s", "regionprops: properties must be specified as a list of",
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
161 "strings or a cell array of strings");
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
162 endif
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
163
585
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
164 ## Fix capitalisation, underscores of user-supplied properties...
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
165 for k = 1:numel (properties)
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
166 property = lower (strrep(properties{k}, "_", ""));
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
167 [~, idx] = ismember (property, lower (all_props));
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
168 if (!idx)
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
169 error ("regionprops: unsupported property: %s", property);
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
170 endif
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
171 properties(k) = all_props{idx};
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
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
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
174 N = ndims (bw);
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
175
393
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
176 ## Get a labelled image
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
177 if (!islogical (bw) && all (bw >= 0) && all (bw == round (bw)))
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
178 L = bw; # the image was already labelled
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
179 num_labels = max (L (:));
585
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
180 elseif (N > 2)
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
181 [L, num_labels] = bwlabeln (bw);
393
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
182 else
396
39a521b5484e More properties implemented
hauberg
parents: 393
diff changeset
183 [L, num_labels] = bwlabel (bw);
393
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
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
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
186 ## Return an empty struct with specified properties if there are no labels
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
187 if num_labels == 0
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
188 retval = struct ([properties; repmat({{}}, size(properties))]{:});
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
189 return;
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
190 endif
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
191
393
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
192 ## Compute the properties
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
193 retval = struct ();
585
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
194 for property = lower(properties)
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
195 property = property{:};
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
196 switch (property)
393
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
197 case "area"
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
198 for k = 1:num_labels
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
199 retval (k).Area = local_area (L == k);
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
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
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
203 for k = 1:num_labels
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
204 retval (k).EulerNumber = bweuler (L == k);
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
205 endfor
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
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
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
208 for k = 1:num_labels
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
209 retval (k).BoundingBox = local_boundingbox (L == k);
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
210 endfor
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
211
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
212 case "extent"
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
213 for k = 1:num_labels
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
214 bb = local_boundingbox (L == k);
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
215 area = local_area (L == k);
585
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
216 idx = length (bb)/2 + 1;
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
217 retval (k).Extent = area / prod (bb(idx:end));
393
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
218 endfor
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
219
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
220 case "perimeter"
585
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
221 if (N > 2)
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
222 warning ("regionprops: skipping perimeter for Nd image");
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
223 else
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
224 for k = 1:num_labels
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
225 retval (k).Perimeter = sum (bwperim (L == k) (:));
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
226 endfor
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
227 endif
393
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
228
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
229 case "centroid"
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
230 for k = 1:num_labels
738
f12450b102d6 Fix centroid coordinate ordering
jordigh
parents: 709
diff changeset
231 C = all_coords (L == k, true);
585
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
232 retval (k).Centroid = [mean(C)];
393
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
233 endfor
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
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
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
236 for k = 1:num_labels
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
237 retval (k).PixelIdxList = find (L == k);
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
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
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
241 for k = 1:num_labels
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
242 retval (k).FilledArea = sum (bwfill (L == k, "holes") (:));
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
243 endfor
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
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
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
246 for k = 1:num_labels
585
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
247 C = all_coords (L == k, true, true);
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
248 retval (k).PixelList = C;
393
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
249 endfor
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
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
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
252 for k = 1:num_labels
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
253 retval (k).FilledImage = bwfill (L == k, "holes");
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
254 endfor
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
255
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
256 case "image"
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
257 for k = 1:num_labels
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
258 tmp = (L == k);
585
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
259 C = all_coords (tmp, false);
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
260 idx = arrayfun (@(x,y) x:y, min (C), max (C), "unif", 0);
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
261 idx = substruct ("()", idx);
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
262 retval (k).Image = subsref (tmp, idx);
393
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
263 endfor
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
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
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
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
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
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
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
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
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
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
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
276 for k = 1:num_labels
585
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
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
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
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
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
281 endfor
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
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
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
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
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
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
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
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
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
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
39a521b5484e More properties implemented
hauberg
parents: 393
diff changeset
293 case "orientation"
585
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
294 if (N > 2)
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
295 warning ("regionprops: skipping orientation for Nd image");
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
296 break
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
297 endif
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
298
396
39a521b5484e More properties implemented
hauberg
parents: 393
diff changeset
299 for k = 1:num_labels
39a521b5484e More properties implemented
hauberg
parents: 393
diff changeset
300 [Y, X] = find (L == k);
39a521b5484e More properties implemented
hauberg
parents: 393
diff changeset
301 if (numel (Y) > 1)
39a521b5484e More properties implemented
hauberg
parents: 393
diff changeset
302 C = cov ([X(:), Y(:)]);
39a521b5484e More properties implemented
hauberg
parents: 393
diff changeset
303 [V, lambda] = eig (C);
39a521b5484e More properties implemented
hauberg
parents: 393
diff changeset
304 [max_val, max_idx] = max (diag (lambda));
39a521b5484e More properties implemented
hauberg
parents: 393
diff changeset
305 v = V (:, max_idx);
39a521b5484e More properties implemented
hauberg
parents: 393
diff changeset
306 retval (k).Orientation = 180 - 180 * atan2 (v (2), v (1)) / pi;
39a521b5484e More properties implemented
hauberg
parents: 393
diff changeset
307 else
39a521b5484e More properties implemented
hauberg
parents: 393
diff changeset
308 retval (k).Orientation = 0; # XXX: What does the other brand do?
39a521b5484e More properties implemented
hauberg
parents: 393
diff changeset
309 endif
39a521b5484e More properties implemented
hauberg
parents: 393
diff changeset
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
39a521b5484e More properties implemented
hauberg
parents: 393
diff changeset
312 %{
39a521b5484e More properties implemented
hauberg
parents: 393
diff changeset
313 case "majoraxislength"
39a521b5484e More properties implemented
hauberg
parents: 393
diff changeset
314 for k = 1:num_labels
39a521b5484e More properties implemented
hauberg
parents: 393
diff changeset
315 [Y, X] = find (L == k);
39a521b5484e More properties implemented
hauberg
parents: 393
diff changeset
316 if (numel (Y) > 1)
39a521b5484e More properties implemented
hauberg
parents: 393
diff changeset
317 C = cov ([X(:), Y(:)]);
39a521b5484e More properties implemented
hauberg
parents: 393
diff changeset
318 lambda = eig (C);
39a521b5484e More properties implemented
hauberg
parents: 393
diff changeset
319 retval (k).MajorAxisLength = (max (lambda));
39a521b5484e More properties implemented
hauberg
parents: 393
diff changeset
320 else
39a521b5484e More properties implemented
hauberg
parents: 393
diff changeset
321 retval (k).MajorAxisLength = 1;
39a521b5484e More properties implemented
hauberg
parents: 393
diff changeset
322 endif
39a521b5484e More properties implemented
hauberg
parents: 393
diff changeset
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
39a521b5484e More properties implemented
hauberg
parents: 393
diff changeset
325 case "minoraxislength"
39a521b5484e More properties implemented
hauberg
parents: 393
diff changeset
326 for k = 1:num_labels
39a521b5484e More properties implemented
hauberg
parents: 393
diff changeset
327 [Y, X] = find (L == k);
39a521b5484e More properties implemented
hauberg
parents: 393
diff changeset
328 if (numel (Y) > 1)
39a521b5484e More properties implemented
hauberg
parents: 393
diff changeset
329 C = cov ([X(:), Y(:)]);
39a521b5484e More properties implemented
hauberg
parents: 393
diff changeset
330 lambda = eig (C);
39a521b5484e More properties implemented
hauberg
parents: 393
diff changeset
331 retval (k).MinorAxisLength = (min (lambda));
39a521b5484e More properties implemented
hauberg
parents: 393
diff changeset
332 else
39a521b5484e More properties implemented
hauberg
parents: 393
diff changeset
333 retval (k).MinorAxisLength = 1;
39a521b5484e More properties implemented
hauberg
parents: 393
diff changeset
334 endif
39a521b5484e More properties implemented
hauberg
parents: 393
diff changeset
335 endfor
39a521b5484e More properties implemented
hauberg
parents: 393
diff changeset
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
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
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
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
340 #case "convexhull"
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
341 #case "solidity"
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
342 #case "conveximage"
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
343 #case "subarrayidx"
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
344 #case "eccentricity"
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
345 #case "equivdiameter"
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
346
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
347 otherwise
585
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
348 error ("regionprops: unsupported property '%s'", property);
393
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
349 endswitch
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
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
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
353 endfunction
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
354
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
355 function retval = local_area (bw)
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
356 retval = sum (bw (:));
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
357 endfunction
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
358
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
359 function retval = local_boundingbox (bw)
585
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
360 C = all_coords (bw);
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
361 retval = [min(C) - 0.5, max(C) - min(C) + 1];
393
394c85635f2b New function: regionprops
hauberg
parents:
diff changeset
362 endfunction
585
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
363
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
364 function C = all_coords (bw, flip = true, singleton = false)
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
365 N = ndims (bw);
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
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
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
368
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
369 ## Coordinate convention for 2d images is to flip the X and Y axes
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
370 ## relative to matrix indexing. Nd images inherit this for the first
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
371 ## two dimensions.
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
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
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
374 endif
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
375
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
376 ## Some functions above expect to work columnwise, so don't return a
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
377 ## vector
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
378 if (rows (C) == 1 && !singleton)
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
379 C = [C; C];
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
380 endif
67d43bf75000 Overhaul regionprops for Nd images
jordigh
parents: 561
diff changeset
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)