13 May 2018

Bitplane slicing

I was quite impressed with the person who thought of this technique. Slicing bits into planes to be able to use them to compress an image. The concept is so simple that not much explanation is needed. You'll see how it works directly from the code. Two techniques are presented here. One from Rashi Agarwal's use of mod and Angel Johnsy's use of bitget and bitset.


Once the image is sliced into bit planes, this is what you'll get:


MSB is the Most Significant Bit. LSB is the Least Significant Bit. You can choose to reconstruct the image with any of the planes which have sufficient information to represent the image. In this case I'm taking planes 7 and 6, although you could even take plane 6 and 8 or even 5 and 7.



Method 1: Using mod

clear all; close all; clc;
I = imread('moon.tif'); figure,imshow(I); title('original'); impixelinfo;
II = double(I);

%---Slice into bit planes
c0 = mod(II, 2);
c1 = mod(floor(II/2), 2);
c2 = mod(floor(II/4), 2);
c3 = mod(floor(II/8), 2);
c4 = mod(floor(II/16), 2);
c5 = mod(floor(II/32), 2);
c6 = mod(floor(II/64), 2);
c7 = mod(floor(II/128), 2);

%---display planes
figure;
sr = 2; sc = 4; si = 1;
subplot(sr, sc, si);si=si+1; imshow(c0); title('plane1. LSB');
subplot(sr, sc, si);si=si+1; imshow(c1); title('plane2');
subplot(sr, sc, si);si=si+1; imshow(c2); title('plane3');
subplot(sr, sc, si);si=si+1; imshow(c3); title('plane4');
subplot(sr, sc, si);si=si+1; imshow(c4); title('plane5');
subplot(sr, sc, si);si=si+1; imshow(c5); title('plane6');
subplot(sr, sc, si);si=si+1; imshow(c6); title('plane7');
subplot(sr, sc, si);si=si+1; imshow(c7); title('plane8. MSB');

%---restore from bit planes
figure,imshow(uint8(c7*128 + c6*64));title('restored with plane 7 and 6');
figure,imshow(uint8(c7*128 + c6*64 + c5*32 + c4*16 + c3*8 + c2*4 + c1*2 + c0));title('Fully restored');
% ri=2*(2*(2*(2*(2*(2*(2*c7+c6)+c5)+c4)+c3)+c2)+c1)+c0;%shortcut technique
% figure,imshow(uint8(ri)); title('Restored image');



Method 2: Using bitget and bitset

clear all; close all; clc;

I = imread('moon.tif'); figure, imshow(I); title('Original');
sr = 2; sc = 4; si = 1;

%---slice into planes
b1 = bitget(I,1);
b2 = bitget(I,2);
b3 = bitget(I,3);
b4 = bitget(I,4);
b5 = bitget(I,5);
b6 = bitget(I,6);
b7 = bitget(I,7);
b8 = bitget(I,8);

%---display planes
figure;
subplot(sr,sc,si);imshow(logical(b1));title('Plane1 LSB');si=si+1;
subplot(sr,sc,si);imshow(logical(b2));title('Plane2');si=si+1;
subplot(sr,sc,si);imshow(logical(b3));title('Plane3');si=si+1;
subplot(sr,sc,si);imshow(logical(b4));title('Plane4');si=si+1;
subplot(sr,sc,si);imshow(logical(b5));title('Plane5');si=si+1;
subplot(sr,sc,si);imshow(logical(b6));title('Plane6');si=si+1;
subplot(sr,sc,si);imshow(logical(b7));title('Plane7');si=si+1;
subplot(sr,sc,si);imshow(logical(b8));title('Plane8 MSB');si=si+1;

%---reconstruct using plane 7 and 8
IR = zeros(size(I));
IR = bitset(IR,7, b7);
IR = bitset(IR,8, b8);
figure, imshow(uint8(IR)); title('reconstructed with plane 7+8');

Once the planes are sliced, you can choose to take just plane 7 + plane 8 and rearrange the bits of each plane in bunches of 8 bits. Then you can convert each bunch of 8 bits to a decimal number. This is called bit plane compression

No comments: