how can convert 256x256 gray image to 6 grayscale?

6 次查看(过去 30 天)
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.

回答(2 个)

Antoni Garcia-Herreros
编辑:Antoni Garcia-Herreros 2023-4-11
Hello Emirhan,
You could try something like this:
Use the command entropy
%Create the 256 grayscale image for the example
IG256=im2double(IBW)*255; % This would be your image
title('256 Grayscale')
IG6=round(IG256/val); % Assign a value from 0 to 5
imshow(IG6,[]);title('6 Grayscale')
  1 个评论
DGM 2023-4-11
编辑:DGM 2023-4-11
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 2023-4-11
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
% 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
% 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
% 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
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
% colormap approximation (map to black & white)
graypict6 = rgb2ind(expanded,map3,'dither'); % quantize
graypict6 = im2uint8(ind2gray(graypict6,map3)); % convert to an intensity image
% colormap approximation (map to extrema)
graypict7 = rgb2ind(expanded,map4,'dither'); % quantize
graypict7 = im2uint8(ind2gray(graypict7,map4)); % convert to an intensity image
% compare them
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?

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by