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

5 views (last 30 days)
I have an image, and I want to show it like this photo:

Accepted Answer

Geoff Hayes
Geoff Hayes on 1 Nov 2015
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
Image Analyst
Image Analyst on 2 Nov 2015
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.
Ilham Hardy
Ilham Hardy on 2 Nov 2015
Do not forget one of the most important feature of Matlab!

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 1 Nov 2015
Edited: Image Analyst on 1 Nov 2015
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 Introduction to Installation and Licensing 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!