Skip to content

Commit 675c6f1

Browse files
committed
add binarize method
1 parent f8f2f6b commit 675c6f1

File tree

7 files changed

+97
-9
lines changed

7 files changed

+97
-9
lines changed

src/@Image/binarize.m

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
function bin = binarize(obj, varargin)
2+
% Binarize a grayscale or intensity image.
3+
%
4+
% BIN = binarize(IMG)
5+
% Converts the grayscale or intensity image IMG into a binary image by
6+
% applying threshold.
7+
%
8+
% BIN = binarize(IMG, T)
9+
% Converts to a binary image by explicitely specifying the threshold
10+
% value. Result is equivalent to IMG > T.
11+
%
12+
%
13+
% Example
14+
% img = Image.read('coins.png');
15+
% bin = binarize(img);
16+
% show(bin)
17+
%
18+
% See also
19+
% componentLabeling, otsuThreshold, maxEntropyThreshold
20+
%
21+
22+
% ------
23+
% Author: David Legland
24+
% e-mail: david.legland@inrae.fr
25+
% INRAE - BIA Research Unit - BIBS Platform (Nantes)
26+
% Created: 2020-11-18, using Matlab 9.8.0.1323502 (R2020a)
27+
% Copyright 2020 INRAE.
28+
29+
if isempty(varargin)
30+
bin = otsuThreshold(obj);
31+
else
32+
var1 = varargin{1};
33+
if isnumeric(var1)
34+
bin = img > value;
35+
else
36+
error('Unknown option');
37+
end
38+
end
39+
40+
% setup result image name
41+
if ~isempty(obj.Name)
42+
bin.Name = sprintf('binarize(%s)', obj.Name);
43+
end

src/@Image/componentLabeling.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,8 @@
4343
res = Image('Data', data, ...
4444
'Parent', obj, ...
4545
'Type', 'label');
46+
47+
% setup result image name
48+
if ~isempty(obj.Name)
49+
res.Name = sprintf('componentLabeling(%s)', obj.Name);
50+
end

src/@Image/fold.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
% the number of rows in the array (N).
88
%
99
% Example
10-
% % Read color image, and convert to N-by-P data array
10+
% % Read color image, and convert to N-by-P Table class
1111
% img = Image.read('peppers.png');
12-
% data = double(unfold(img));
12+
% tab = unfold(img);
1313
% % Performs basic clustering
14-
% km = kmeans(data, 6);
14+
% km = kmeans(tab, 6);
1515
% % Convert result to imaeg using the 'fold' method
1616
% img2 = Image.fold(km+1, size(img), 'type', 'label', 'Name', 'km3 labels');
1717
% figure; show(label2rgb(img2, 'jet'));

src/@Image/label2rgb.m

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@
22
% Convert label image to RGB image.
33
%
44
% RGB = label2rgb(LBL)
5-
% Covnerts the label image LBL to a RGB image. The result image is
5+
% Converts the label image LBL to a RGB image. The result image is
66
% encoded into uint8.
77
%
88
% Example
9-
% label2rgb
9+
% img = Image.read('coins.png');
10+
% bin = closing(img > 80, ones(3,3));
11+
% lbl = componentLabeling(bin);
12+
% rgb = label2rgb(lbl, 'jet', 'w', 'shuffle');
13+
% rgb.Name = 'regions(coins)';
14+
% figure; show(rgb);
1015
%
1116
% See also
12-
% watershed
17+
% watershed, binarize
1318
%
1419

1520
% ------
@@ -58,4 +63,9 @@
5863
end
5964

6065
% create new image
61-
rgb = Image('data', data, 'parent', obj, 'type', 'color');
66+
rgb = Image('Data', data, 'Parent', obj, 'Type', 'Color');
67+
68+
% setup result image name
69+
if ~isempty(obj.Name)
70+
rgb.Name = sprintf('label2rgb(%s)', obj.Name);
71+
end

src/@Image/maxEntropyThreshold.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
%
1515
%
1616
% See also
17-
% otsuThreshold, maxEntropyThresholdValue
17+
% binarize, otsuThreshold, maxEntropyThresholdValue
1818
%
1919

2020
% ------

src/@Image/otsuThreshold.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
%
2626
%
2727
% See also
28-
% histogram, otsuThresholdValue, maxEntropyThreshold, lt, le
28+
% binarize, otsuThresholdValue, maxEntropyThreshold, lt, le, histogram
2929
%
3030

3131
% ------

tests/image/test_binarize.m

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
function tests = test_binarize
2+
% Test suite for the file binarize.
3+
%
4+
% Test suite for the file binarize
5+
%
6+
% Example
7+
% test_binarize
8+
%
9+
% See also
10+
% binarize
11+
12+
% ------
13+
% Author: David Legland
14+
% e-mail: david.legland@inrae.fr
15+
% Created: 2020-11-18, using Matlab 9.8.0.1323502 (R2020a)
16+
% Copyright 2020 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 = magic(5);
24+
img = Image(data);
25+
26+
bin = binarize(img);
27+
28+
assertTrue(testCase, isa(bin, 'Image'));
29+
assertTrue(testCase, isBinaryImage(bin));
30+

0 commit comments

Comments
 (0)