Skip to content

Quantization

gabrielazapatero edited this page May 11, 2021 · 3 revisions

Quantization of an image

This tool allows to quantize an image in n bits. Quantization is based on the discretization of the intensity values of an image.

For the proper use of this application the user must specify the number of bits in which the image must be quantized. Given k bits, the number of levels the digitized image holds is:

The lower the number of bits specified, the less the amount of intensity levels, hence the fewer colors the image will have.

Performance of the application

Once the application has been compiled and both the file and lecture have been selected, the main window of the Quantization app will appear. Now, the number of bits must be selected in the slider. The slider has been set to a minimum of 0 and a maximum of 10. This range has been set because from one level on the amount of intensity levels used by the image will not change despite increasing the levels. In practice, this means that at a certain level L, the image will look exactly the same with L levels than with > L.

After the bits parameter has been chosen, the execute button must be pushed in order to visualize the displayed image. The parameters can be changed as many times as desired without needing to recompile the application.

The example demonstrates that from a certain level forward, the resultant image is the same as the original one, meaning that the 256 levels for each channel are not necessarily employed.

MATLAB's code

The code responsible for this performance is based on calculating the size of each level by dividing the number of current levels (256 levels) by the desired levels (2^n levels).

        bits = app.Slider.Value;
        levels= 2.^bits;
        %quantlevels = max(I(:)); 
        quantlevels = 256;
        levels2= quantlevels/levels;
        
        vector = [1:levels];
        for i= 1:levels
            vector(i) = i*levels2;  
        end

Now that the number and size of the levels has been obtained, the next step is to calculate the values that will fill these levels.

        values = [1:levels+1];
        
        for k= 1
            values(k) = (vector(k)+ 0)/2;
        end
        
        for k= 2:levels
           values(k) = (vector(k) + vector(k-1))/2;
        end

By applying the following code we will allow the application to work with both grayscale and color images. The first step is to check whether the image is from one type or the other. To achieve this, we will look at the number of channels. If the number of channels is 1 it shows that we are dealing with a grayscale image.

        [rows, columns, numberOfColorChannels] = size(I);
        if numberOfColorChannels == 1
            quant = imquantize (I, vector, values);

If it is not grayscale we apply this transformation to each RGB channel independently and subsequently join them together for its proper representation.

        else 

        quantR = imquantize (R, vector, values);
        quantG  = imquantize (G, vector, values);
        quantB = imquantize (B, vector, values);

        quant=zeros([size(R,1) size(R,2) 3]);
        quant(:,:,1) = quantR;
        quant(:,:,2) = quantG;
        quant(:,:,3) = quantB;
        
        end
        figure;
        imshow(uint8(quant))
        axis off
        title('QUANTIZED IMAGE')

Clone this wiki locally