|
1 | 1 | function [circle, labels] = imInscribedCircle(lbl, varargin) |
2 | | -%IMINSCRIBEDCIRCLE Maximal circle inscribed in a particle |
| 2 | +% Maximal circle inscribed in a region. |
3 | 3 | % |
4 | 4 | % CIRC = imInscribedCircle(IMG) |
5 | 5 | % Computes the maximal circle inscribed in a given particle, or |
6 | 6 | % around each labeled particle in the input image. |
7 | 7 | % |
8 | | -% CIRC = imInscribedCircle(IMG, LABELS) |
| 8 | +% CIRC = imInscribedCircle(IMG, SPACING) |
| 9 | +% CIRC = imInscribedCircle(IMG, SPACING, ORIGIN) |
| 10 | +% Takes into account the spatical calibration to compute the results in |
| 11 | +% physical units. Both SPACING and ORIGIN are 1-by-2 row vectors, that |
| 12 | +% correspond to the spacing between pixels and to the position of the |
| 13 | +% first pixel, respectively. |
| 14 | +% |
| 15 | +% CIRC = imInscribedCircle(..., LABELS) |
9 | 16 | % Specify the labels for which the inscribed circle needs to be computed. |
10 | 17 | % The result is a N-by-3 array with as many rows as the number of labels. |
11 | 18 | % |
|
15 | 22 | % img = imFillHoles(imread('circles.png')); |
16 | 23 | % imshow(img); hold on; |
17 | 24 | % circ = imInscribedCircle(img); |
18 | | -% drawCircle(circ, 'linewidth', 2) |
| 25 | +% drawCircle(circ, 'LineWidth', 2) |
19 | 26 | % |
20 | 27 | % % Compute and display the equivalent ellipses of several particles |
21 | 28 | % img = imread('rice.png'); |
22 | 29 | % img2 = img - imopen(img, ones(30, 30)); |
23 | 30 | % lbl = bwlabel(img2 > 50, 4); |
24 | 31 | % circles = imInscribedCircle(lbl); |
25 | 32 | % imshow(img); hold on; |
26 | | -% drawCircle(circles, 'linewidth', 2, 'color', 'g'); |
| 33 | +% drawCircle(circles, 'LineWidth', 2, 'Color', 'g'); |
27 | 34 | % |
28 | 35 | % See also |
29 | 36 | % drawCircle, imEnclosingCircle, imInertiaEllipse, imInscribedBall |
|
35 | 42 | % Created: 2012-07-08, using Matlab 7.9.0.529 (R2009b) |
36 | 43 | % Copyright 2012 INRA - Cepia Software Platform. |
37 | 44 |
|
| 45 | +%% Process input arguments |
| 46 | + |
| 47 | +% default values |
| 48 | +spacing = [1 1]; |
| 49 | +origin = [1 1]; |
| 50 | +calib = false; |
| 51 | + |
| 52 | +% extract spacing |
| 53 | +if ~isempty(varargin) && sum(size(varargin{1}) == [1 2]) == 2 |
| 54 | + spacing = varargin{1}; |
| 55 | + varargin(1) = []; |
| 56 | + calib = true; |
| 57 | + origin = [0 0]; |
| 58 | +end |
| 59 | + |
| 60 | +% extract origin |
| 61 | +if ~isempty(varargin) && sum(size(varargin{1}) == [1 2]) == 2 |
| 62 | + origin = varargin{1}; |
| 63 | + varargin(1) = []; |
| 64 | +end |
| 65 | + |
38 | 66 | % check if labels are specified |
39 | 67 | labels = []; |
40 | 68 | if ~isempty(varargin) && size(varargin{1}, 2) == 1 |
|
47 | 75 | end |
48 | 76 | nLabels = length(labels); |
49 | 77 |
|
| 78 | + |
| 79 | +%% Main processing |
| 80 | + |
50 | 81 | % allocate memory for result |
51 | 82 | circle = zeros(nLabels, 3); |
52 | 83 |
|
53 | 84 | for iLabel = 1:nLabels |
54 | 85 | % compute distance map |
55 | | - distMap = imDistanceMap(lbl==iLabel); |
| 86 | + distMap = imDistanceMap(lbl==labels(iLabel)); |
56 | 87 |
|
57 | 88 | % find value and position of the maximum |
58 | 89 | maxi = max(distMap(:)); |
|
61 | 92 | circle(iLabel,:) = [xc yc maxi]; |
62 | 93 | end |
63 | 94 |
|
| 95 | +% apply spatial calibration |
| 96 | +if calib |
| 97 | + circle(:,1:2) = bsxfun(@plus, bsxfun(@times, circle(:,1:2) - 1, spacing), origin); |
| 98 | + circle(:,3) = circle(:,3) * spacing(1); |
| 99 | +end |
0 commit comments