how can convert 256x256 gray image to 6 grayscale?

3 次查看(过去 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
What do you mean by 6 grayscale? 6 bit image? Divide your grayscale image in 6 regions?

请先登录,再进行评论。

回答(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
I=imread('peppers.png');
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);
  1 个评论
DGM
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
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
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?

类别

Help CenterFile Exchange 中查找有关 Discrete Multiresolution Analysis 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by