gray image to 8 bit planes using bit plane slicing
106 views (last 30 days)
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
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
Image Analyst
on 29 May 2013
Edited: Image Analyst
on 16 Apr 2020
You have a color image. I don't know what bitget() returns or means in the case of a 3 plane color image. Why don't you use rgb2gray() or extract one of the color channels?
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(B);
if numberOfColorBands > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale.
grayImage = rgb2gray(B); % Take weighted average of channels.
end
or
% Extract the individual red, green, and blue color channels.
redChannel = B(:, :, 1);
greenChannel = B(:, :, 2);
blueChannel = B(:, :, 3);
Let us know what happens after that.
Also, see my attached bit plane viewer program.
15 Comments
Image Analyst
on 30 May 2013
None of that is necessary. It looks like you're trying to calculate the entropy. If so, just do this:
entropyOfThisImage = entropy(d);
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);
0 Comments
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
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}, []);
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!