how can convert 256x256 gray image to 6 grayscale?

8 views (last 30 days)
elTurco on 11 Apr 2023
Edited: DGM on 11 Apr 2023
Hi everyone.
I want to convert a gray 256x256 pixel image to 6 grayscale.
And i want to calculate the entropy of new image. Could anyone help me??
Have a good day.
Antoni Garcia-Herreros on 11 Apr 2023
What do you mean by 6 grayscale? 6 bit image? Divide your grayscale image in 6 regions?
elTurco on 11 Apr 2023
I mean convert 256 gray level to 6 gray level.

Antoni Garcia-Herreros on 11 Apr 2023
Edited: Antoni Garcia-Herreros on 11 Apr 2023
Hello Emirhan,
You could try something like this:
Use the command entropy
%Create the 256 grayscale image for the example
IBW=rgb2gray(I);
IG256=im2double(IBW)*255; % This would be your image
subplot(1,2,1)
imshow(IG256,[])
title('256 Grayscale')
val=255/5;
IG6=round(IG256/val); % Assign a value from 0 to 5
subplot(1,2,2)
imshow(IG6,[]);title('6 Grayscale')
Entropy=entropy(IG6);
DGM on 11 Apr 2023
Edited: DGM on 11 Apr 2023
If you run the code with a different image, you may notice that the calculated entropy is zero. That's because of two things. First, the image is not correctly scaled for its class. Second, because the image is low-contrast and has no content near black, the uniform quantization means that all pixels are in the same gray level due to truncation (again, because the image is improperly-scaled). As far as entropy() is concerned, it's being given a featureless white image.
You can simplify the code and fix the error like so. (I omitted everything but the core operations)
I = imread('pout.tif'); % this image is low-contrast
IBW = im2gray(I); % convert if RGB
N = 6; % number of gray levels
IG6 = round(im2double(IBW)*(N-1))/(N-1); % correctly-scaled for its class
Entropy = entropy(IG6)
Entropy = 0.9296
This result should be the same as using gray2ind()

DGM on 11 Apr 2023
You haven't mentioned at all how you want to quantize the image. I imagine that the entropy depends on that.
% assuming input is gray
inpict = imread('pout.tif'); % single-channel, uint8
nlevels = 6;
% uniform quantized using gray2ind()
% mapping corresponds to nominal range, not image extrema
[graypict1 map1] = gray2ind(inpict,nlevels); % quantize
graypict1 = im2uint8(ind2gray(graypict1,map1)); % convert to an intensity image
imshow(graypict1)
% minimum variance quantization using rgb2ind()
% mapping corresponds to image extrema
expanded = repmat(inpict,[1 1 3]); % expand the image
[graypict2 map2] = rgb2ind(expanded,nlevels,'nodither'); % quantize
graypict2 = im2uint8(ind2gray(graypict2,map2)); % convert to an intensity image
imshow(graypict2)
% colormap approximation (map to black & white)
% approximately the same as uniform quant
% since the map is uniform and full-range
map3 = gray(nlevels); % a full-range color table
graypict3 = rgb2ind(expanded,map3,'nodither'); % quantize
graypict3 = im2uint8(ind2gray(graypict3,map3)); % convert to an intensity image
imshow(graypict3)
% colormap approximation (map to extrema)
% in this case the map is still uniform, but only spans the image extrema
map4 = rescale(map3,im2double(min(inpict(:))),im2double(max(inpict(:))));
graypict4 = rgb2ind(expanded,map4,'nodither'); % quantize
graypict4 = im2uint8(ind2gray(graypict4,map4)); % convert to an intensity image
imshow(graypict4)
Now do the same things, but with FS error-diffusion dithering
% minimum variance quantization
expanded = repmat(inpict,[1 1 3]); % expand the image
[graypict5 map5] = rgb2ind(expanded,nlevels,'dither'); % quantize
graypict5 = im2uint8(ind2gray(graypict5,map5)); % convert to an intensity image
imshow(graypict5)
% colormap approximation (map to black & white)
graypict6 = rgb2ind(expanded,map3,'dither'); % quantize
graypict6 = im2uint8(ind2gray(graypict6,map3)); % convert to an intensity image
imshow(graypict6)
% colormap approximation (map to extrema)
graypict7 = rgb2ind(expanded,map4,'dither'); % quantize
graypict7 = im2uint8(ind2gray(graypict7,map4)); % convert to an intensity image
imshow(graypict7)
% compare them
[entropy(graypict1)
entropy(graypict2)
entropy(graypict3)
entropy(graypict4)
entropy(graypict5)
entropy(graypict6)
entropy(graypict7)]
ans = 7×1
0.9296 2.2557 0.9250 1.8247 2.3047 1.3267 1.7909
So what's the entropy of the an arbitrarily quantized image? How long is a piece of string?