|
| 1 | +function [boxes, labels] = regionBoxes(obj, varargin) |
| 2 | +% Bounding box of regions within a 2D or 3D binary or label image. |
| 3 | +% |
| 4 | +% BOX = regionBoxes(IMG) |
| 5 | +% Compute the bounding boxes of the regions within the label image IMG. |
| 6 | +% If the image is binary, a single box corresponding to the foreground |
| 7 | +% (i.e. the pixels with value 1) is computed. |
| 8 | +% |
| 9 | +% The result is a N-by-4 array BOX = [XMIN XMAX YMIN YMAX], containing |
| 10 | +% coordinates of the box extent. |
| 11 | +% |
| 12 | +% The same result could be obtained with the regionprops function. The |
| 13 | +% advantage of using regionBoxes is that equivalent boxes can be |
| 14 | +% obtained in one call. |
| 15 | +% |
| 16 | +% BOX = regionBoxes(IMG3D) |
| 17 | +% If input image is a 3D array, the result is a N-by-6 array, containing |
| 18 | +% the maximal coordinates in the X, Y and Z directions: |
| 19 | +% BOX = [XMIN XMAX YMIN YMAX ZMIN ZMAX]. |
| 20 | +% |
| 21 | +% [BOX, LABELS] = regionBoxes(...) |
| 22 | +% Also returns the labels of the regions for which a bounding box was |
| 23 | +% computed. LABELS is a N-by-1 array with as many rows as BOX. |
| 24 | +% |
| 25 | +% [...] = regionBoxes(IMG, LABELS) |
| 26 | +% Specifies the labels of the regions whose bounding box need to be |
| 27 | +% computed. |
| 28 | +% |
| 29 | +% |
| 30 | +% Example |
| 31 | +% % Compute bounding box of coins regions |
| 32 | +% img = Image.read('coins.png'); % read image |
| 33 | +% bin = opening(img > 80, ones([3 3])); % binarize |
| 34 | +% lbl = componentLabeling(bin); % compute labels |
| 35 | +% figure; show(img); % display image |
| 36 | +% boxes = regionBoxes(lbl); % compute bounding boxes |
| 37 | +% hold on; drawBox(boxes, 'b'); % display boxes |
| 38 | +% |
| 39 | +% See also |
| 40 | +% drawBox, regionCentroids |
| 41 | +% |
| 42 | + |
| 43 | +% ------ |
| 44 | +% Author: David Legland |
| 45 | +% e-mail: david.legland@inrae.fr |
| 46 | +% INRAE - BIA Research Unit - BIBS Platform (Nantes) |
| 47 | +% Created: 2021-02-02, using Matlab 9.8.0.1323502 (R2020a) |
| 48 | +% Copyright 2021 INRAE. |
| 49 | + |
| 50 | +% check image type |
| 51 | +if ~(isLabelImage(obj) || isBinaryImage(obj)) |
| 52 | + error('Requires a label of binary image'); |
| 53 | +end |
| 54 | + |
| 55 | +% check if labels are specified |
| 56 | +labels = []; |
| 57 | +if ~isempty(varargin) && isnumeric(varargin{1}) && size(varargin{1}, 2) == 1 |
| 58 | + labels = varargin{1}; |
| 59 | +end |
| 60 | + |
| 61 | +% extract the set of labels, without the background |
| 62 | +if isempty(labels) |
| 63 | + labels = findRegionLabels(obj); |
| 64 | +end |
| 65 | + |
| 66 | +% switch processing depending on dimension |
| 67 | +nd = ndims(obj); |
| 68 | +if nd == 2 |
| 69 | + %% Process planar case |
| 70 | + props = regionprops(obj.Data, 'BoundingBox'); |
| 71 | + props = props(labels); |
| 72 | + bb = reshape([props.BoundingBox], [4 length(props)])'; |
| 73 | + |
| 74 | + % convert to (x,y) indexing convention |
| 75 | + boxes = [bb(:, 2) bb(:, 2)+bb(:, 4) bb(:, 1) bb(:, 1)+bb(:, 3)]; |
| 76 | + |
| 77 | + % spatial calibration |
| 78 | + if isCalibrated(obj) |
| 79 | + spacing2 = obj.Spacing([1 1 2 2]); |
| 80 | + origin2 = obj.Origin([1 1 2 2]); |
| 81 | + boxes = bsxfun(@plus, bsxfun(@times, (boxes - 1), spacing2), origin2); |
| 82 | + end |
| 83 | + |
| 84 | +elseif nd == 3 |
| 85 | + %% Process 3D case |
| 86 | + props = regionprops3(obj.Data, 'BoundingBox'); |
| 87 | + props = props(labels); |
| 88 | + bb = reshape([props.BoundingBox], [6 size(props, 1)])'; |
| 89 | + bb = bb(labels, :); |
| 90 | + |
| 91 | + % convert to (x,y,z) indexing convention |
| 92 | + boxes = [bb(:, 2) bb(:, 2)+bb(:, 5) bb(:, 1) bb(:, 1)+bb(:, 4) bb(:, 3) bb(:, 3)+bb(:, 6)]; |
| 93 | + |
| 94 | + % spatial calibration |
| 95 | + if isCalibrated(obj) |
| 96 | + spacing2 = obj.Spacing([1 1 2 2 3 3]); |
| 97 | + origin2 = obj.Origin([1 1 2 2 3 3]); |
| 98 | + boxes = bsxfun(@plus, bsxfun(@times, (boxes - 1), spacing2), origin2); |
| 99 | + end |
| 100 | +else |
| 101 | + error('Image dimension must be 2 or 3'); |
| 102 | +end |
0 commit comments