Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to convert 8bit gray scale image to 4bit gray scale image

1,583 views
Skip to first unread message

ujjwal kotdiya

unread,
Apr 5, 2012, 6:30:17 AM4/5/12
to
i had done following steps:
a=imread('xyz.png');
b=rgb2gray(a);
this is a 8bit gray scale image now how ican convert this 8bit image to 4bit image
is there any function is there for performing this conversion.

THnx in advance

ImageAnalyst

unread,
Apr 5, 2012, 8:35:16 AM4/5/12
to
-------------------------------------------------------------
b4bit = uint8(b / 16);

ujjwal kotdiya

unread,
Apr 6, 2012, 7:54:29 AM4/6/12
to
ImageAnalyst <imagea...@mailinator.com> wrote in message <bfd47738-25c4-4d45...@p6g2000yqi.googlegroups.com>...
Is this possible to adjust bit allocation according to our requirement i.e
now if i want image to be 5bit or 3bit or 6 bit etc.. for that purpose what i had to do


thank you

ImageAnalyst

unread,
Apr 6, 2012, 8:08:43 AM4/6/12
to
On Apr 6, 7:54 am, "ujjwal kotdiya" <ujjwalkotd...@gmail.com> wrote:
> Is this possible to adjust bit allocation according to our requirement i.e
> now if i want image to be 5bit or 3bit or 6 bit etc.. for that purpose what i had to do
>
> thank you
--------------------------------------------------------------------
For other bits, just divide by 2^n if you want to do linear scaling.
If you want to extract out and visualize certain bitplanes, I have
demo code for that. See below:

% IMPORTANT: The newsreader may break long lines into multiple lines.
% Be sure to join any long lines that got split into multiple single
lines.
% These can be found by the red lines on the left side of your
% text editor, which indicate syntax errors, or else just run the
% code and it will stop at the split lines with an error.

clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;

% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'cameraman.tif';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));

% Let's compute and display the histogram.
[pixelCount grayLevels] = imhist(grayImage);
subplot(2, 2, 2);
bar(pixelCount);
grid on;
title('Histogram of Original Grayscale Image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Let's use this spot for the output image.
subplot(2, 3, 5);

for bp = 0 : 7
% Compute the bit plane image.
bitPlaneImage = bitand(grayImage, 2^bp);
% Display it as pure black and white (not gray scale).
imshow(bitPlaneImage, []);
caption = sprintf('You are now viewing bitplane %d.', bp);
title(caption, 'FontSize', fontSize);

% If it's not the last bit plane, ask them if they want to
continue.
if bp ~= 7
message = sprintf('%s\nDo you want to continue', caption);
button = questdlg(message, 'Continue?', 'Yes', 'No', 'Yes');
drawnow; % Refresh screen to get rid of dialog box remnants.
if strcmpi(button, 'No')
break;
end
end
end

msgbox('Done with demo');

hliya...@gmail.com

unread,
Jul 23, 2018, 3:35:24 PM7/23/18
to
______________________________________________
Hey guys,
is it wrong to use the following to down scale a rgb image from 8 to 4 bits?

sf=(15/255);
h=imread('peppers.png');
h4bit=h.*sf;
0 new messages