Wednesday, August 18, 2010

MATLAB rgb2ind bug

In MATLAB, we can use the function rgb2ind to reduce the number of colours presented in an image. This is implemented during the process of converting an RGB image to an indexed image.

The syntax for uniform quantisation is
[X,map] = rgb2ind(RGB, tol)
where tol is a scalar bewteen 0.0 and 1.0. The function description explains that the map contains at most (floor(1/tol)+1)^3 colors.

Now let's run the following program.
im =imread('peppers.png');
[im_uq map_uq] =rgb2ind(im, 0.4); % uniform quantization

The MATLAB output is
ans =


This is the number of colours used. If we substitute 0.4 into (floor(1/tol)+1)^3, we will get 27. There must be something wrong.

After checking the code of rgb2ind, I found the problem. The function round is used in the program instead of floor. Thus to solve this problem, you can either modify the function or ask MathWorks to update the description.

Clearly, this bug can only be found when round(1/tol) is not equal to floor(1/tol).

No comments:

Post a Comment