Function File: [quant, idx] = imquantize (img, levels)
Function File: […] = imquantize (img, levels, values)

Quantize image with multiple threshold levels and values.

This function reduces the number of unique values in img by performing multiple thresholds, one for each element in levels, and assigning it values from values.

The output quant is the quantized image, of same size as img and same class as values. idx are the indices into values such that quant == values(idx).

For the simpler case where values is not defined, it defaults to 1:N+1 and this function becomes equivalent to:

out = ones (size (img));
for i = 1:numel(levels)
  out(img > levels(i)) += 1;
endfor

So that for example:

>> img = [1 2 3; 4 5 6; 7 8 9];
>> imquantize (img, [3 6])
   ⇒ ans =

         1   1   1
         2   2   2
         3   3   3

For the more general case, when values is defined, this function is equivalent to:

levels = [-Inf levels Inf];
out = zeros (size (img), class (values));
for i = 1:numel(values)
  out(img > levels(i) & img <= levels(i+1)) = values(i);
endfor

So that for example:

>> img = [1 2 3; 4 5 6; 7 8 9];
>> imquantize (img, [3 6], [0 5 8])
   ⇒ ans =

         0   0   0
         5   5   5
         8   8   8

See also: gray2ind, ind2gray, ind2rgb, intlut, label2rgb, lookup, rgb2ind.

Package: image