Skip to content

Commit 702f1d3

Browse files
committed
rename centroid as regionCentroids and add support for spatial calibration
1 parent 5d9ea51 commit 702f1d3

File tree

5 files changed

+144
-3
lines changed

5 files changed

+144
-3
lines changed

src/@Image/centroid.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
function [points, labs] = centroid(obj, varargin)
22
%CENTROID Centroid(s) of a binary or label image.
33
%
4+
% Deprecated: renamed as regionCentroids
5+
%
46
% C = centroid(I)
57
% Returns the centroid C of the binary image I. C is a 1-by-2 or 1-by-3
68
% row vector, depending on the dimension of the image.
@@ -31,6 +33,8 @@
3133
% Created: 2018-07-03, using Matlab 9.4.0.813654 (R2018a)
3234
% Copyright 2018 INRA - Cepia Software Platform.
3335

36+
warning('deprecated, use ''regionCentroids'' instead');
37+
3438
% check image type
3539
if ~(isLabelImage(obj) || isBinaryImage(obj))
3640
error('Requires a label of binary image');

src/@Image/findLabels.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
end
2828

2929
if ~isLabelImage(obj)
30-
error('Reqsuires a label image');
30+
error('Requires a label image');
3131
end
3232

3333
% special case of CC structure, not used at the moment

src/@Image/regionCentroids.m

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
function [points, labels] = regionCentroids(obj, varargin)
2+
% Centroid of region(s) in a binary or label image.
3+
%
4+
% C = regionCentroids(I)
5+
% Returns the centroid C of the binary image I. C is a 1-by-2 or 1-by-3
6+
% row vector, depending on the dimension of the image.
7+
%
8+
% C = regionCentroids(LBL)
9+
% If LBL is a label D-dimensional image, returns an array of N-by-D
10+
% values, corresponding to the centroids of the N regions within the
11+
% image.
12+
%
13+
% [C, LABELS] = regionCentroids(LBL)
14+
% Also returns the labels of the regions that were measured.
15+
%
16+
% Example
17+
% % Compute centroids of coins particles
18+
% img = Image.read('coins.png'); % read image
19+
% bin = opening(img > 80, ones([3 3])); % binarize
20+
% lbl = componentLabeling(bin); % compute labels
21+
% figure; show(img); % display image
22+
% pts = regionCentroids(lbl); % compute centroids
23+
% hold on; plot(pts(:,1), pts(:,2), 'b+'); % display centroids
24+
%
25+
% See also
26+
% analyzeRegions, findRegionLabels, componentLabeling, regionprops
27+
28+
% ------
29+
% Author: David Legland
30+
% e-mail: david.legland@inrae.fr
31+
% Created: 2018-07-03, using Matlab 9.4.0.813654 (R2018a)
32+
% Copyright 2018 INRA - Cepia Software Platform.
33+
34+
% check image type
35+
if ~(isLabelImage(obj) || isBinaryImage(obj))
36+
error('Requires a label of binary image');
37+
end
38+
39+
% check if labels are specified
40+
labels = [];
41+
if ~isempty(varargin) && size(varargin{1}, 2) == 1
42+
labels = varargin{1};
43+
end
44+
45+
% extract the set of labels, without the background
46+
if isempty(labels)
47+
labels = findRegionLabels(obj);
48+
end
49+
nLabels = length(labels);
50+
51+
% allocate memory for result
52+
nd = ndims(obj);
53+
points = zeros(nLabels, nd);
54+
55+
% switch processing depending on image dimensionality
56+
if nd == 2
57+
for i = 1:nLabels
58+
% extract points of the current particle
59+
[x, y] = find(obj.Data == labels(i));
60+
61+
% coordinates of particle regionCentroids
62+
xc = mean(x);
63+
yc = mean(y);
64+
65+
points(i, :) = [xc yc];
66+
end
67+
68+
elseif nd == 3
69+
dim = size(obj.Data);
70+
for i = 1:nLabels
71+
% extract points of the current particle
72+
inds = find(obj.Data == labels(i));
73+
[x, y, z] = ind2sub(dim, inds);
74+
75+
% coordinates of particle regionCentroids
76+
xc = mean(x);
77+
yc = mean(y);
78+
zc = mean(z);
79+
80+
points(i, :) = [xc yc zc];
81+
end
82+
83+
else
84+
error('Requires an image of dimension 2 or 3');
85+
end
86+
87+
% calibrate result
88+
points = bsxfun(@plus, bsxfun(@times, points-1, obj.Spacing), obj.Origin);

src/@Image/regionElementCounts.m

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
function [counts, labels] = regionElementCounts(obj, varargin)
22
% Count the number of pixels/voxels within each region of a label image.
33
%
4-
% output = regionElementCounts(input)
4+
% CNT = regionElementCounts(LBL)
5+
% For each region on the label image LBL, count the number of elements
6+
% (pixels or voxels) that constitute this region. Return a column vector
7+
% with as many elements as the number of regions.
8+
%
9+
% [CNT, LABELS] = regionElementCounts(LBL)
10+
% Also returns the labels of the regions.
511
%
612
% Example
7-
% regionElementCounts
13+
% img = Image.read('coins.png');
14+
% bin = fillHoles(img > 100);
15+
% lbl = componentLabeling(bin);
16+
% regionElementCounts(lbl)'
17+
% ans =
18+
% 2563 1899 2598 1840 2693 1906 2648 2725 1935 2796
819
%
920
% See also
21+
% regionCentroids, findRegionLabels
1022
%
1123

1224
% ------

tests/image/test_regionCentroids.m

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
function tests = test_regionCentroids
2+
% Test suite for the file regionCentroids.
3+
%
4+
% Test suite for the file regionCentroids
5+
%
6+
% Example
7+
% test_regionCentroids
8+
%
9+
% See also
10+
% regionCentroids
11+
12+
% ------
13+
% Author: David Legland
14+
% e-mail: david.legland@inrae.fr
15+
% Created: 2021-02-02, using Matlab 9.8.0.1323502 (R2020a)
16+
% Copyright 2021 INRAE - BIA-BIBS.
17+
18+
tests = functiontests(localfunctions);
19+
20+
function test_Simple(testCase) %#ok<*DEFNU>
21+
% Test call of function without argument.
22+
23+
data = zeros([10 10], 'uint8');
24+
data(2:4, 2:4) = 3;
25+
data(6:10, 2:4) = 4;
26+
data(2:4, 6:10) = 7;
27+
data(6:10, 6:10) = 8;
28+
img = Image('Data', data, 'Type', 'Label');
29+
30+
[centroids, labels] = regionCentroids(img);
31+
32+
assertEqual(testCase, size(centroids), [4 2]);
33+
assertEqual(testCase, centroids, [3 3;8 3;3 8;8 8]);
34+
assertEqual(testCase, size(labels), [4 1]);
35+
assertEqual(testCase, labels, [3;4;7;8]);
36+
37+

0 commit comments

Comments
 (0)