-
Notifications
You must be signed in to change notification settings - Fork 0
/
vislabels.m
54 lines (46 loc) · 1.65 KB
/
vislabels.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
function vislabels(L)
%VISLABELS Visualize labels of connected components
% VISLABELS is used to visualize the output of BWLABEL.
%
% VISLABELS(L), where L is a label matrix returned by BWLABEL,
% displays each object's label number on top of the object itself.
%
% Note: VISLABELS requires the Image Processing Toolbox.
%
% Example
% -------
% bw = imread('text.png');
% L = bwlabel(bw);
% vislabels(L)
% axis([1 70 1 70])
% Steven L. Eddins
% Copyright 2008 The MathWorks, Inc.
% Form a grayscale image such that both the background and the
% object pixels are light shades of gray. This is done so that the
% black text will be visible against both background and foreground
% pixels.
background_shade = 0;
foreground_shade = 250;
I = zeros(size(L), 'uint8');
I(L == 0) = background_shade;
I(L ~= 0) = foreground_shade;
% Display the image, fitting it to the size of the figure.
figure,
imageHandle = imshow(I, 'InitialMagnification', 'fit');
% Get the axes handle containing the image. Use this handle in the
% remaining code instead of relying on gca.
axesHandle = ancestor(imageHandle, 'axes');
% Get the extrema points for each labeled object.
s = regionprops(L, 'Centroid');
% Superimpose the text label at the left-most top extremum location
% for each object. Turn clipping on so that the text doesn't
% display past the edge of the image when zooming.
hold(axesHandle, 'on');
for k = 1:numel(s)
e = s(k).Centroid;
text(e(1,1), e(1,2), sprintf('%d', k), ...
'Parent', axesHandle, ...
'Clipping', 'on', ...
'Color', 'b');
end
hold(axesHandle, 'off');