Skip to content

Commit c36ebc9

Browse files
committed
kmeans: add support for grayscale images (results in multi-levels threshold)
1 parent 65dcdcb commit c36ebc9

File tree

3 files changed

+51
-16
lines changed

3 files changed

+51
-16
lines changed

src/@Image/interp.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
%% Compute interpolation
4646

4747
nv = size(point, 1);
48-
nc = channelNumber(obj);
48+
nc = channelCount(obj);
4949
val = zeros(nv, nc);
5050

5151
if nd == 2

src/@Image/kmeans.m

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22
% Apply k-means clustering to a multi-channel image.
33
%
44
% CLASSES = kmeans(IMG, K)
5-
% Compute kmeans clustering of the multi-channel image IMG, using K
6-
% classes.
5+
% Compute kmeans clustering of the image IMG, using K classes. The result
6+
% is a label image with values ranging from 1 no K.
77
%
88
% [CLASSES, CENTERS] = kmeans(IMG, K);
99
% Also returns the coordinates of class centers. CENTERS is a K-by-NC
1010
% array, where NC is the number of channels of original image IMG.
1111
%
12+
% Requires the statistics toolbox.
1213
%
1314
% Example
15+
% % k-means segmentation of a RGB color image
1416
% img = Image.read('peppers.png');
1517
% cls = kmeans(img, 6);
1618
% % display the classes with arbitrary colors
@@ -22,6 +24,16 @@
2224
% rgb = label2rgb(cls, map);
2325
% figure; show(rgb);
2426
%
27+
% % multi-level segmentation of a grayscale image with k-means
28+
% nc = 5;
29+
% img = Image.read('cameraman.tif');
30+
% [classes, values] = kmeans(img, nc);
31+
% res = Image.zeros(size(img),'uint8');
32+
% for i = 1:nc
33+
% res(classes == i) = values(i);
34+
% end
35+
% figure; show(res);
36+
%
2537
% See also
2638
% label2rgb, fold
2739
%
@@ -33,26 +45,20 @@
3345
% Created: 2020-01-15, using Matlab 9.7.0.1247435 (R2019b) Update 2
3446
% Copyright 2020 INRAE.
3547

36-
if channelNumber(obj) < 2
37-
error('Requires a multi-channel image');
38-
end
39-
4048
% convert image content into nr-by-nc array
41-
nr = elementNumber(obj);
42-
nc = channelNumber(obj);
49+
nr = elementCount(obj);
50+
nc = channelCount(obj);
4351
data = double(reshape(obj.Data, [nr nc]));
4452

4553
% run the kmeans algorithm
54+
% (use statistics toolbox function)
4655
[idx, centers] = kmeans(data, k);
4756

4857
% convert to label image, by avoiding zero label
4958
idx = reshape(idx, size(obj));
5059

5160
% create parent image
52-
newName = 'classes';
53-
if ~isempty(obj.Name)
54-
newName = sprintf('%s-kmeans%d', obj.Name, k);
55-
end
56-
res = Image('Data', idx, 'Type', 'label', ...
57-
'parent', obj, 'Name', newName, ...
58-
'ChannelNames', {'Class'});
61+
newName = createNewName(obj, sprintf('%%s-kmeans%d', k));
62+
res = Image('Data', idx, 'Parent', obj, ...
63+
'Type', 'label', 'Name', newName, ...
64+
'ChannelNames', {'Class'});

tests/image/test_kmeans.m

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

0 commit comments

Comments
 (0)