0
votes

I'm trying to understand the idea of quantization with images and how to do so properly. So the image I'd like to do is a bmp file. Looking at mathworks I figured that imquantize would be the proper way to do this, and with what I wanted to uniformly quantize the image into certain K levels. So for example I did this:

K4 = multithresh(I1,4);
I1_quant1 = imquantize(I1,K4);
No5 = figure;
image(I1_quant1);

and with 16 levels right after. That seemed to work well. Unfortunately when I tried to do so with 32 and 64 it doesn't work. Looking at the documentation I saw that you can't go further than 20. So I'm trying to figure out where I went wrong there.

I've attempted with using both linspace:

K64 = linspace(0,1,64); K64(1) = [];
I1_quant4 = imquantize(I1,K64);
No8 = figure;
image(K64(I1_quant4));

and with the suggestion on someone, generalizing linspace to help make the levels:

NLevelsA = 32;
Levels = linspace(min(I1(:)), max(I1(:)), NLevelsA + 1); 
Levels(1) = [];
I1_quant3 = imquantize(I1,Levels(1));
No7 = figure;
image(K32(I1_quant3));

Yet those don't seem to work, so I'm asking to see what's possibly wrong here?

1
Can't you multiply with some value such that the range of your pixels is [0,31], and then round?Cris Luengo
What do you mean by that?Marorin
If in is in the range [0,255] (8-big unsigned), then in/8 is in the range [0,31]. MATLAB probably returns a uint8 again, so rounding is built in, but in general round(double(in)/256*K) would uniformly quantize to K grey levels.Cris Luengo

1 Answers

0
votes

To quantize an image to N distinct grey levels, with all levels equally spaced, you can simply bring the image to the range [0,N), and round intensities down:

N = 28;
I1 = double(I1); % convert to double-precision so we can compute easily
I1 = I1 - min(I1(:));
I1 = I1 / max(I1(:)) * (N - 1e-9); % linear stretch to the range [0,N)
I1 = floor(I1);
I1 = uint8( I1 * 255 / (N-1) ); % stretch to full uint8 range and cast

Obviously, the code above yields uneven distances between output grey levels because I choses a value of N that doesn't divide 256 evenly. You can always choose to not scale to the [0,255] range if that bothers you.