|
| 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); |
0 commit comments