forked from leolca/color-quantize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
impalette.m
34 lines (29 loc) · 1.04 KB
/
impalette.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
function [p, idx] = impalette (X, plength, method)
% [p, idx] = impalette (X, plength, method)
%
% Find the best color palette (codebook) for a given image,
% using a chosen color distance metric. You must provide
% the input image @{X}, the color palette length @{plength} and
% the desired color distance metric @{methor}, in accordance with
% the function @{colordistance}.
%
% example:
% X = imread ('lena.png');
% [p, idx] = impalette (X, 3, 'EUCLIDEAN');
% X image
% plength length of the desired palette
% method color distance metric
if nargin < 3, method = 'CIE76'; endif
method = upper (method);
if any (strcmp ({'CIE76','CIE94','CIE2K'}, method)),
X = rgb2lab (X);
endif
if isinteger (X), X = double (X); endif;
P = reshape(X, [size(X,1)*size(X,2) 3]);
colordistanceMethod = @(v1, v2) colordistance (v1, v2, method);
[idx, p, sumD, dist] = kmeans (P, plength, colordistanceMethod);
idx = reshape(idx, size(X,1), size(X,2));
if any (strcmp ({'CIE76','CIE94','CIE2K'}, method)),
p = 255*lab2rgb (p);
endif
endfunction