gray image to 8 bit planes using bit plane slicing
Show older comments
i have written a code to find the 8 bit planes of the gray image. i saved those images which should be in binary now. but when i am reading those images it is showing its pixel values few 0 and most 255 in binary only 0's and 1's should be there and when i did this size(d) it displayed 598 931 3.
i want it to be a in a form of 2-d array matrix with only 0's and 1's
can any one tell me what is the problem occurring?
A=imread('boy.tif');
B=bitget(A,1); figure, imshow(logical(B));title('Bit plane 1');
B=bitget(A,2); figure, imshow(logical(B));title('Bit plane 2');
B=bitget(A,3); figure, imshow(logical(B));title('Bit plane 3');
B=bitget(A,4); figure, imshow(logical(B));title('Bit plane 4');
B=bitget(A,5); figure, imshow(logical(B));title('Bit plane 5');
B=bitget(A,6); figure, imshow(logical(B));title('Bit plane 6');
B=bitget(A,7); figure, imshow(logical(B));title('Bit plane 7');
B=bitget(A,8); figure, imshow(logical(B));title('Bit plane 8');
this what i used then gave names to each of them
and when i read d=imread('bp0.tif') its giving 0 and 255 (only 0 and 255) i want ones and zeros and size should be a 2-d array why does it show 598 931 3
5 Comments
Gholamreza Jahangiri
on 25 Jan 2020
You should first turn your image to a double, using double(A), and then you ccould have your bit for each plane.
A = imread('boy.tif');
ad = double(A);
B=bitget(ad,2); figure, imshow(B);
Walter Roberson
on 26 Jan 2020
Converting to double is not necessary before doing bitget() . bitget() is happy to work on uint8.
abdul suboor
on 16 Apr 2020
Edited: Image Analyst
on 16 Apr 2020
Please help me someone.
Question = read an 8 bit gray scale image using the OpenCV function imread().
Calculate all 8 bit planes of image, and display these bit planes a grid of 4*2 using matplotlib python library. (do on python)
Walter Roberson
on 16 Apr 2020
abdul, none of that appears to be a question about MATLAB. Python has a large active community that you can be talking to, somewhere else.
Image Analyst
on 16 Apr 2020
Abdul, I've attached a bit plane viewer program to my answer below, and here. But it uses MATLAB, not Python and OpenCV. Maybe it will persuade you to dump Python and switch to MATLAB. ?
Accepted Answer
More Answers (2)
humbertinnho
on 21 Mar 2017
Edited: humbertinnho
on 21 Mar 2017
Ps.: Img is a 2D Image (gray colors only):
function y = Linear_Bit( Img )
b1 = double(bitget(Img,1));
b2 = double(bitget(Img,2));
b3 = double(bitget(Img,3));
b4 = double(bitget(Img,4));
b5 = double(bitget(Img,5));
b6 = double(bitget(Img,6));
b7 = double(bitget(Img,7));
b8 = double(bitget(Img,8));
Img_b = cat(8,b1,b2,b3,b4,b5,b6,b7,b8);
figure,
imshow(Img), title('Original:');
figure,
subplot(2,2,1)
imshow(b1), title('Bit Plan: 1');
subplot(2,2,2)
imshow(b2), title('Bit Plan: 2');
subplot(2,2,3)
imshow(b3), title('Bit Plan: 3');
subplot(2,2,4)
imshow(b4), title('Bit Plan: 4');
figure,
subplot(2,2,1)
imshow(b5), title('Bit Plan: 5');
subplot(2,2,2)
imshow(b6), title('Bit Plan: 6');
subplot(2,2,3)
imshow(b7), title('Bit Plan: 7');
subplot(2,2,4)
imshow(b8), title('Bit Plan: 8');
y = Img_b;
If you want to add Planes, u can use this:
Img_b = uint8(Linear_Bit(Img));
b1 = Img_b(:,:,1);
b2 = Img_b(:,:,2)*2;
b3 = Img_b(:,:,3)*4;
b4 = Img_b(:,:,4)*8;
b5 = Img_b(:,:,5)*16;
b6 = Img_b(:,:,6)*32;
b7 = Img_b(:,:,7)*64;
b8 = Img_b(:,:,8)*128;
...and just add like
New_Image = b7 + b8;
imshow(New_Image);
Sufyan Parkar
on 19 Apr 2019
A=imread('boy.tif');
B=rgb2gray(A); %..............as for me i took a color image so i had to do rgb2gray
% the workspace shows the dimensions of the image and mine was 435x580 and the class of my grayscale image was uint8 thus it required 8bits
%hence to extract the plane i performed bitand of B and the binary equivalent of the required plane
for i=1:435
for j=1:580
MSB(i,j)=bitand(B(i,j),bin2dec('10000000'));
LSB(i,j)=bitand(B(i,j),bin2dec('00000001'));
2nd(i,j)=bitand(B(i,j),bin2dec('01000000'));
3rd(i,j)=bitand(B(i,j),bin2dec('00100000'));
4th(i,j)=bitand(B(i,j),bin2dec('00010000'));
5th(i,j)=bitand(B(i,j),bin2dec('00001000'));
6th(i,j)=bitand(B(i,j),bin2dec('00000100'));
7th(i,j)=bitand(B(i,j),bin2dec('00000010'));
end
end
figure,imshow(MSB);
figure,imshow(LSB);
figure,imshow(2nd);
figure,imshow(3rd);
figure,imshow(4th);
figure,imshow(5th);
figure,imshow(6th);
figure,imshow(7th);
%this will give you all the 8bit-plane
4 Comments
Vivek
on 9 Aug 2023
No this code gives an error in MATLAB.
@Vivek you're right. This is a poorly written program that won't even run. You can't start variable names with a number. Here it is less terrible, but really, you should be using my method above instead of this:
rgbImage = imread('peppers.png');
grayImage = rgb2gray(rgbImage); %..............as for me i took a color image so i had to do rgb2gray
subplot(3, 3, 1);
imshow(grayImage);
% The workspace shows the dimensions of the image and mine was 435x580 and the class of my grayscale image was uint8 thus it required 8bits
% hence to extract the plane i performed bitand of B and the binary equivalent of the required plane
MSB=bitand(grayImage,bin2dec('10000000'));
LSB=bitand(grayImage,bin2dec('00000001'));
plane2=bitand(grayImage,bin2dec('01000000'));
plane3=bitand(grayImage,bin2dec('00100000'));
plane4=bitand(grayImage,bin2dec('00010000'));
plane5=bitand(grayImage,bin2dec('00001000'));
plane6=bitand(grayImage,bin2dec('00000100'));
plane7=bitand(grayImage,bin2dec('00000010'));
% Display the bit planes.
subplot(3, 3, 2); imshow(LSB); title('Bit Plane 1')
subplot(3, 3, 3); imshow(plane2); title('Bit Plane 2')
subplot(3, 3, 4); imshow(plane3); title('Bit Plane 3')
subplot(3, 3, 5); imshow(plane4); title('Bit Plane 4')
subplot(3, 3, 6); imshow(plane5); title('Bit Plane 5')
subplot(3, 3, 7); imshow(plane6); title('Bit Plane 6')
subplot(3, 3, 8); imshow(plane7); title('Bit Plane 7')
subplot(3, 3, 9); imshow(MSB); title('Bit Plane 8')
% This will give you all 8 bit-planes.
Here is my method, as well as being attached.
% 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(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 = bitget(grayImage, bp+1);
% 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');
Vivek
on 9 Aug 2023
Thank you. I used your code but how can I save the all bitplane extracted images? So I convert this code into simple form which is look like this.
Image Analyst
on 9 Aug 2023
Otherwise (less likely) if you want to save all the individual arrays to a variable for some reason (instead of just using bitPlaneImage immediately in the loop), then you can write them to a cell array
% Compute the bit plane image and save as a cell in an 8 element cell array.
bitPlaneImage{bp} = bitget(grayImage, bp+1);
% bitPlaneImage = bitand(grayImage, 2^bp);
% Display it as pure black and white (not gray scale).
imshow(bitPlaneImage{bp}, []);
Categories
Find more on Filter Design 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!