gray image to 8 bit planes using bit plane slicing

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 commentaires

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);
Converting to double is not necessary before doing bitget() . bitget() is happy to work on uint8.
abdul suboor
abdul suboor le 16 Avr 2020
Modifié(e) : Image Analyst le 16 Avr 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)
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.
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. ?

Connectez-vous pour commenter.

 Réponse acceptée

Image Analyst
Image Analyst le 29 Mai 2013
Modifié(e) : Image Analyst le 16 Avr 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 commentaires

i wrote this code
>> B=imread('boy.tif');
>> % Extract the individual red, green, and blue color channels.
redChannel = B(:, :, 1);
greenChannel = B(:, :, 2);
blueChannel = B(:, :, 3);
Index exceeds matrix dimensions.
and when i wrote this one it showed nothing
>> B=imread('boy.tif');
>> % 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
Sorry - I thought you meant B has 3 planes, but you meant d. But you did not show how you used imwrite to create "bp0.tif". What array did you save? B? Do a "whos" on that variable just before you call imwrite(...,'bp0.tif').
i didn't used any command imwrite. when the bit plane images opened in the image tool i just clicked on>>file>>save as and saved the image as bpo.tif
because i need to save those bit planes so that i can use it as binary images
and sir B is the original image. d is one of the bit planes of that image. and when you meant size(B) in your code you meant the original image boy.tif right?
"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."
>> d=imread('bp0.tif');
>> whos d
Name Size Bytes Class Attributes
d 598x932x3 1672008 uint8
No, don't do that. File/saveas saves the whole figure as an image. You want just the bit plane, so please use only imwrite() to save bp0.tif.
okay how to use that sir imwrite(figure1)? suppose i want to save the bit plane 1 which will open in figure 1 image tool how should i write it?
A=imread('boy.tif');
B=bitget(A,1); figure, imshow(logical(B));title('Bit plane 1');
imwrite(uint8(B), 'bp1.png');
yup working
size(d)
1024 1536
faraz.a
faraz.a le 29 Mai 2013
Modifié(e) : Walter Roberson le 26 Jan 2020
hello sir problem again
as you told i wrote the code
A=imread('boy.tif');
B=bitget(A,1); figure, imshow(logical(B));title('Bit plane 1');imwrite(uint8(B), 'bp0.png');
B=bitget(A,2); figure, imshow(logical(B));title('Bit plane 2');imwrite(uint8(B), 'bp1.png');
B=bitget(A,3); figure, imshow(logical(B));title('Bit plane 3');imwrite(uint8(B), 'bp2.png');
B=bitget(A,4); figure, imshow(logical(B));title('Bit plane 4');imwrite(uint8(B), 'bp3.png');
B=bitget(A,5); figure, imshow(logical(B));title('Bit plane 5');imwrite(uint8(B), 'bp4.png');
B=bitget(A,6); figure, imshow(logical(B));title('Bit plane 6');imwrite(uint8(B), 'bp5.png');
B=bitget(A,7); figure, imshow(logical(B));title('Bit plane 7');imwrite(uint8(B), 'bp6.png');
B=bitget(A,8); figure, imshow(logical(B));title('Bit plane 8');imwrite(uint8(B), 'bp7.png');
then i wrote this
d=imread('bp7.png'); imshow(d)
it displayed a complete blank image why is that now?
faraz.a
faraz.a le 29 Mai 2013
if i am using imwrite(uint8(B), 'bp0.png'); it is just saving a black image.. complete black image
It's not, it's just that with a gray level of 0 or 1 you can't see it because it's too dark. If you looked in the variable editor, you'd see the values. Try displaying it with []:
imshow(d, []);
oh okay i got it thank you. sir i want to take pixel value from the binary image and assign it to some variable X. like this
d=imread('bp0.png');
i=0;j=0;
d(i,j)=X .. i want to give X as the pixel value of 0th row 0th column of the binary image
i know it is not correct, then what is the correct way to do this
sir i trying to execute this but it is not displaying any value.
>> d=imread('bp5.png');
[n,m]=size(d); %n rows m is columns
i=0;j=0;cont0=0;cont1=0;cont=0;
for i=0:n;
for j=0:m;
X=d(i,j); % X is for 1st step pixel value of 0th row and 0th column
if X==0;
cont0= 1 + cont0;
end
if X==1;
cont1= 1 + cont1;
end
cont= 1 + cont;
end_for
end_for
p0= mod(cont0,cont);
p1= mod(cont1,cont);
h= -p0*log2(p0) -p1*log2(p1);
disp(h);
disp(p0);
disp(p1);
end
None of that is necessary. It looks like you're trying to calculate the entropy. If so, just do this:
entropyOfThisImage = entropy(d);
yes sir but in some cases i need to consider the pixel value neighbors
my program method is correct i am sure but there is some problem in if else and else if statements. i am unable to rectify it can you please tell what wrong i am doing with the if else statements please may be i am not ending the if else stements properly but unable to understand how to do please check sri
d=imread('bp5.png');
[n,m]=size(d);
i00=1;
i01=2;
i10=3;
i11=4;
c0(i00)=0;c1(i00)=0;c0(i01)=0;c1(i01)=0;c0(i10)=0;c1(i10)=0;c0(i11)=0;c1(i11)=0;count(i00)=0;count(i01)=0;count(i10)=0;count(i11)=0;cont=0; %define
i=2; j=2; % i=rows, j= columns
for i=2:n;
for j=2:m;
X=d(i,j);
A=d(i,j-1);
B=d(i-1,j);
if A==0 && B==0
if X==0
c0(i00)=1+c0(i00);
else
c1(i00)=1+c1(i00);
end
count(i00)=1+count(i00);
end
if A==0 && B==1
if X==0
c0(i01)=1+c0(i01);
else
c1(i01)=1+c1(i01);
end
count(i01)=1+count(i01);
end
if A==1 && B==0
if X==0
c0(i10)=1+c0(i10);
else
c1(i10)=1+c1(i10);
end
count(i10)=1+count(i10);
end
if A==1 && B==1
if X==0
c0(i11)=1+c0(i11);
else if X==1
c1(i11)=1+c1(i11);
end
count(i11)=1+count(i11);
end
cont=1+cont;
end
end
end
end
end
% c0me steady state probabilities
p=count/cont;
%now display these all the below conditional probabilities p0(i00)=mod(c0(i00),cont(i00))
p0=c0/count;
p1=c1/count;
h= -p0*log2(p0)- p1*log2(p1);
E=sum(p*h)
end
end
end
end

Connectez-vous pour commenter.

Plus de réponses (2)

humbertinnho
humbertinnho le 21 Mar 2017
Modifié(e) : humbertinnho le 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);
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 commentaires

Vivek
Vivek le 9 Août 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
Vivek le 9 Août 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.
You can use imwrite if you want to save them to a disk file.
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}, []);

Connectez-vous pour commenter.

Catégories

En savoir plus sur Convert Image Type dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by