gray image to 8 bit planes using bit plane slicing

조회 수: 257 (최근 30일)
faraz.a
faraz.a 2013년 5월 29일
댓글: Image Analyst 2023년 8월 9일
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
Walter Roberson
Walter Roberson 2020년 4월 16일
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
Image Analyst 2020년 4월 16일
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. ?

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2013년 5월 29일
편집: Image Analyst 2020년 4월 16일
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
Image Analyst
Image Analyst 2013년 5월 30일
None of that is necessary. It looks like you're trying to calculate the entropy. If so, just do this:
entropyOfThisImage = entropy(d);
faraz.a
faraz.a 2013년 5월 30일
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

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

humbertinnho
humbertinnho 2017년 3월 21일
편집: humbertinnho 2017년 3월 21일
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
Sufyan Parkar 2019년 4월 19일
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
Vivek
Vivek 2023년 8월 9일
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
Image Analyst 2023년 8월 9일
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}, []);

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by