Digital Image Processing Bitplane Slicing/Compression

1. Bit plane slice

1 Overview

        Bit plane slicing is a well known technique used in image processing. Bitplane slicing is used in image compression. Bitplane slicing is converting an image into a multilevel binary image.

        These binary images are then compressed using different algorithms. Using this technique, significant bits can be separated from grayscale images, which is useful for processing this data with very low time complexity.

2. What is bit plane slice?

        The gray level of each pixel in a digital image is stored in a computer as one or more bytes.

        For 8-bit images, 0 is encoded as 00000000 and 255 is encoded as 11111111. Any number between 0 and 255 is encoded as a byte.

        The leftmost bit is called the most significant bit (MSB), because changes to this bit can significantly alter the byte-encoded value.

        The rightmost bit is called the Least Significant Bit (LSB) because a change in this bit does not change the encoded grayscale value much.

3. The main role of bit plane slices

        Convert a grayscale image to a binary image.

        Represent an image with fewer bits and map the image to a smaller size

        Enhance the image by focusing.

4. Examples

(1) Brief description

        A picture like this 

        The binary data of the pixel value of the selected yellow area is as follows

         It can then be defined that plane 1 contains the least significant bits of all pixels in the image.

         Plane 8 contains the highest bits of all pixels in the image.

 (2) matlab reads and displays

        Use matlab to read the picture and use the bitget function to get each slice.

A=imread('coins.png');

B=bitget(A,1);
figure,
subplot(2,2,1);imshow(logical(B));title('Bit plane 1');

B=bitget(A,2);
subplot(2,2,2);imshow(logical(B));title('Bit plane 2');

B=bitget(A,3);
subplot(2,2,3);imshow(logical(B));title('Bit plane 3');


B=bitget(A,4);
subplot(2,2,4);imshow(logical(B));title('Bit plane 4');

        The displayed results are as follows, only the first four slices are shown.

         With these slices in hand, we can use them to combine or process the reconstructed image. To achieve compression or focus and other purposes.

2. Bit plane compression

1 Overview

        This is a lossy compression technique . In other words, some data is lost during compression compared to the original image.

2. Description of the compression process

(1) There is such a matrix

 (2) dec2bin to binary

 (3) Extract the most significant bit (MSB) of each value in the matrix from the binary representation. MATLAB function 'bitget'.

 (4) Rearrange the above MSB values ​​so that each row contains 8 columns or 8 bits. This can be done by padding the matrix with zeros at the end to form a matrix with 8 columns per row.

 (5) Convert the binary representation in each row to a decimal number and store. Convert binary values ​​to decimal values ​​using the 'bin2dec' MATLAB function.

 3. Description of the decompression process

(1) Convert the decimal value of the compressed data to binary format. Remove extra zeros attached to the matrix if needed.

 (2) Use the MATLAB function "reshape" to reshape the matrix to the size of the original matrix A.

 (3) Preallocate a matrix of the same size as the original matrix A, and replace the MSB (Most Significant Bit) of each value in the matrix with the bits we decompressed in the previous step. Use the MATLAB function 'bitset'

 (4) From this we get the original data of this bit slice

 4. Matrix example

(1) Compression matrix

clear all
clc

A = [180 4 80 33 201; 120 27 11 160 28; 224 1 133 67 144];
A = uint8(A);

%Encoding

%Check whether zeros has to be appended to the matrix
rem = mod(numel(A),8);
if rem~=0
rem = 8-rem;
end

%Extract the MSB
bit8 = bitget(A,8);
b8 = bit8';
b8 = b8(:);
b8 = [b8;zeros(rem,1)];

%Reshape the matrix as such each row contains 8 columns
mat8 = reshape(b8,8,[])';
str8 = num2str(mat8);
str8 = str8(:,1:3:end);

%Convert the binary to decimal
compressedbit8 = uint8(bin2dec(str8));

(2) Decompress data

%Decoding

%Convert Decimal to Binary
decompressedbit8 = dec2bin(compressedbit8,8);

%Reshape the matrix to the size of original matrix size
%And remove extra zeros appended to the matrix
dbit8 = decompressedbit8';
dbit8 = dbit8(:);
dbit8 = dbit8(1:end-rem);
dbit8 = reshape(dbit8,size(A,2),size(A,1))';

%Preallocate a matrix
Image = zeros([size(A,1) size(A,2)]);
slice8 = zeros([size(A,1) size(A,2)]);

%Set the MSB with the binary values obtained from decompressed matrix
ind_bit8 = find(dbit8=='1');
slice8(ind_bit8) = 1;
Image = bitset(Image,8,slice8);
Image = uint8(Image);

%Display data
display(Image);

5. Image example

(1) Extract 678 bits

%ENCODING
clear all
clc


%INPUT IMAGE
A = imread('cameraman.tif');

%Encoding
bitnums = [6;7;8]; %BIT PLANES

%CHECK IF PADDING WITH ZEROS IS NEEDED OR NOT
rem = mod(numel(A),8);
if(rem~=0)
rem = 8-rem;
end

%EXTRACT EACH BIT AND STORE IT IN THE MATRIX

forinc =1:length(bitnums)

Ind = bitnums(inc);

%EXTRACT THE 'n'th BIT
bitval = bitget(A,Ind);

%PAD WITH ZEROS AND RESHAPE THE MATRIX
bval = bitval';
bval = bval(:);
bval = [bval;zeros(rem,1)];

matv = reshape(bval,8,[])';
strv = num2str(matv);
strv = strv(:,1:3:end);

%CONVERT BINARY TO DECIMAL
compressedbitv(:,inc) = uint8(bin2dec(strv));

end

%STORE THE COMPRESSED DATA IN A FILE
%OPTIONAL
fp = fopen('compressed_data678.data','wb');
fwrite(fp,compressedbitv','uint8');
fclose(fp);

(2) Read and restore to pictures

%DECOMPRESSION
clear all
clc

%INPUT FROM THE USER
M = 256; %SIZE OF THE ORIGINAL IMAGE
N = 256; %SIZE OF THE ORIGINAL IMAGE
bitnums = [6;7;8]; %BIT PLANES USED

rem = mod(M*N,8);

if(rem~=0)
rem = 8-rem;
end

%READ THE COMPRESSED DATA
fp = fopen('compressed_data678.data','rb');
compressedbitv = fread(fp,[length(bitnums),Inf],'uint8')';
fclose(fp);

%PREALLOCATE THE MATRIX
Image = zeros([M N]);
forinc = 1:length(bitnums)

Ind = bitnums(inc);

%CONVERT DECIMAL TO BINARY
decompressedbitv = dec2bin(compressedbitv(:,inc),8);

%REMOVE EXTRA ZEROS AND RESHAPE THE MATRIX
dbitv = decompressedbitv';
dbitv = dbitv(:);
dbitv = dbitv(1:end-rem);
dbitv = reshape(dbitv,N,M)';

%SET THE 'n'th BIT
slicev = zeros([M N]);
ind_bitv = find(dbitv == '1');
slicev(ind_bitv) = 1;
     Image = bitset(Image,Ind,slicev);

end

%DISPLAY THE IMAGE
Image = uint8(Image);
figure,imagesc(Image);colormap(gray);

(3) Sample pictures

7, 8 slices

 

2468 slices

 

Guess you like

Origin blog.csdn.net/bashendixie5/article/details/123603381