How to show 8 bits of an image like this photo?

 Accepted Answer

Shimaa - if we assume that the image is 8-bit (unsigned integer) and is grayscale (like in your attachment) and that it is two-dimensional only (i.e. 256x256), then we can determine the first bit only (least-significant bit and so is right most bit in the 8-bit pixel) by using bitand as follows
mask = 1;
singleBitImg = bitand(cameraManImg, mask);
In the above, we are applying a mask of 1 (or, in binary, 00000001) which will zero all but the first bit from each pixel in our image. We can then plot the pixel1Img and move to the second bit. Since the binary 00000001 zeroed all bits but the first, then we want to zero all bits but the second, so we need a binary mask of 00000010. We can easily get this using the bitshift function which shifts the bits to the left (or right). In this case, we want to shift the bits of our mask one bit to the left.
mask = 1;
mask = bitshift(mask,1);
singleBitImg = bitand(cameraManImg, mask);
And you do the same for the remaining six bits, shifting the mask one bit to the left on each iteration of a loop. You can use then use subplot to place each of your nine images (the original along with the eight calculated above) into nine different subplots within the same figure.

10 Comments

Thanks alot. But i want full answer with loop and subplot because i'm begginer on matlab
That's exactly what I gave you. Did you overlook my answer?
Sure.but because i'm begginer i can't do for loop &supbplot right on your answer
That makes no sense. First you say "i want full answer with loop and subplot because i'm begginer" and then when I say that's exactly what I gave you, you then say "because i'm begginer i can't do for loop & supbplot", which directly contradicts what you said you wanted at first. So let's get it clear: Yes or No -- do you want an answer with a for loop and subplot()?
Yes exactly , I have an image and l want to make it like the attached img
Shimaa - given the two answers, you should be able to complete your assignment. Try to do so, and if you have any further questions then please post what you have attempted and describe what problems you are facing.
Shimaa, look at the picture in your post. Then look at my post and the picture in it. Other than the trivial change of the titles above the images being slightly different, tell me what makes my answer not what you need.
It seems to be complicated for me. I couldn't understand some parts of it like this:
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
button = menu('Use which demo image?', 'CameraMan', 'Moon', 'Eight', 'Coins', 'Pout');
if button == 1
baseFileName = 'cameraman.tif';
elseif button == 2
baseFileName = 'moon.tif';
elseif button == 3
baseFileName = 'eight.tif';
elseif button == 4
baseFileName = 'coins.png';
else
baseFileName = 'pout.tif';
end
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
% 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(3, 3, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));
What does this block do???
Some parts? That's the whole thing. And nearly every line has a comment associated with it that explains what it does. If I were to say what it does, I'd just be repeating the comments. Surely there must be some lines in there that you understand, so let's start with some line that you don't understand - either my comment or the explanation in the help documentation - and we'll try to expand on the description.
Do not forget one of the most important feature of Matlab!

Sign in to comment.

More Answers (1)

Looks like my demo. You can use bitget() like I did in my demo (below).
% Demo to extract and view bitplanes in a gray scale image.
clc; % Clear the command window.
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');
button = menu('Use which demo image?', 'CameraMan', 'Moon', 'Eight', 'Coins', 'Pout');
if button == 1
baseFileName = 'cameraman.tif';
elseif button == 2
baseFileName = 'moon.tif';
elseif button == 3
baseFileName = 'eight.tif';
elseif button == 4
baseFileName = 'coins.png';
else
baseFileName = 'pout.tif';
end
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
% 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(3, 3, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));
for bp = 0 : 7
% Extract the bit plane image using bpget().
bitPlaneImage = bitget(grayImage, bp+1);
% bitPlaneImage = bitand(grayImage, 2^bp);
% Display it as pure black and white (not gray scale).
subplot(3, 3, bp+2);
imshow(bitPlaneImage, []);
caption = sprintf('Bit Plane %d.', bp);
title(caption, 'FontSize', fontSize);
end
msgbox('Done with demo');

Categories

Find more on Images in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!