## Scalar Quantization

Hello!

Today I’ll explain a little about scalar quantization. The goal of scalar quantization is to try to display an image using less quantization levels or less bit to represent each color level.

This method is very simple and very intuitive but the results aren’t that good. So, what’s the idea of scalar quantization? Imagine that we have an RGB image with color quantization from 0 to 255 (8 bits) and we want to represent the colors level only with 2 bits for each color. So instead of using 24 bits we’ll use only 6.

Ok, but how do we do it? It’s pretty simple, imagine that we divide our range from 0 to 255 in 4 groups for each color:  0 to 63,  64 to 127, 128 to 191, 192 to 255. For each group the average value is 32, 96, 160, 224. So we’ll have in total 64 colors.

So for every pixel, we check every color and find in which group the color fits and replace its value for the average value of that group. For example, for a pixel whose values are (R, G, B) = (25, 78, 230), we’ll convert it to (32, 96, 224). And we do this for every pixel of the image. This is that simple!

One disadvantage of this algorithm is that it process each color in separate way and so we’ll have more color distortion as we can see in the background. We can also see lots of artifacts due lost of resolution. We can compare the result image against the original one below.

Is there any way to improve quality using scalar quantization technique? Yes, we can improve it by using kmeans which aims minimize the mean squared error. We can use the 64 colors define in scalar quantization as centroid to the kmeans function in matlab. The results we can check in the image below. We can easily note an improvement in quality but at cost of more processing.

Next post I’ll show you another algorithm called Vector Quantization. We’ll see that we’ll have much better results

Here the source code for octave/matlab for only scalar quantization

Here the source code for octave/matlab for scalar quantization + kmeans. For Octave we need to adapt kmeans function.  Remember, we need statistics package for kmeans in octave.

That’s all!

Marcelo

Marcelo Jo

Marcelo Jo is an electronics engineer with 10+ years of experience in embedded system, postgraduate in computer networks and masters student in computer vision at Université Laval in Canada. He shares his knowledge in this blog when he is not enjoying his wonderful family – wife and 3 kids. Live couldn’t be better.

### 3 Responses to 'Scalar Quantization'

1. Vinu says:

Thank you for the post sir. It is very informative.

2. Jonathan Orrego says:

Just to add something useful… i used Maxim 9526 to decode video from my PAL / NTSC signal from TV cable, that IC is great, then the output was procesed by a Xilinx spartan 3E to detect a color pattern and detect start/end of some spots in public tv. The whole process works great, spartan 3E works good to detect color patterns, substitute them if neccesary and the you can use an Analog Devices IC to get the video signal back in PAL/NTSC… May be that hard + this article can work together as a real time video compressor.

3. Jonathan says:

Great info, thanks!

This site uses Akismet to reduce spam. Learn how your comment data is processed.