Montag, 5. November 2012

Bit-Plane Slicing


            Digitally, an image is represented in terms of pixels.
These pixels can be expressed further in terms of bits.
Consider the image ‘coins.png’ and the pixel representation of the image.

Consider the pixels that are bounded within the yellow line. The binary formats for those values are (8-bit representation)


The binary format for the pixel value 167 is 10100111
Similarly, for 144 it is 10010000
This 8-bit image is composed of eight 1-bit planes.
Plane 1 contains the lowest order bit of all the pixels in the image.

And plane 8 contains the highest order bit of all the pixels in the image.

Let’s see how we can do this using MATLAB

A=[167 133 111
      144 140 135
      159 154 148]



B=bitget(A,1);  %Lowest order bit of all pixels
‘bitget’ is a MATLAB function used to fetch  a bit from the specified position from all the pixels.
B=[1 1 1
      0 0 1
      1 0 0]
B=bitget(A,8);%Highest order bit of all pixels
B=[1 1 0
      1 1 1 
      1 1 1]

MATLAB CODE:
%Bit Planes from 1 to 8. Output Format: Binary

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');

 

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







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


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


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

 

Image reconstruction using n bit planes.

1.     The nth plane in the pixels are multiplied by the constant 2^n-1
2.     For instance, consider the matrix
A= A=[167 133 111
      144 140 135
      159 154 148] and the respective bit format

         
3.     Combine the 8 bit plane and 7 bit plane.
For 10100111, multiply the 8 bit plane with 128 and 7 bit plane with 64.
(1x128)+(0x64)+(1x0)+(0x0)+(0x0)+(1x0)+(1x0)+(1x0)=128

4.     Repeat this process for all the values in the matrix and the final result will be
[128 128 64
 128 128 128
128 128 128]


MATLAB CODE:
%Image reconstruction by combining 8 bit plane and 7 bit plane
A=imread('coins.png');
B=zeros(size(A));
B=bitset(B,7,bitget(A,7));
B=bitset(B,8,bitget(A,8));
B=uint8(B);
figure,imshow(B);

Image reconstructed using 8 and 7 bit planes
Explanation:
‘bitset’ is used to set  a bit at a specified position. Use ‘bitget’ to get the bit at the positions 7 and 8 from all the pixels in matrix A and use ‘bitset’ to set these bit values at the positions 7 and 8 in the matrix B.


%Image reconstruction by combining 8,7,6 and 5 bit planes
A=imread('coins.png');
B=zeros(size(A));
B=bitset(B,8,bitget(A,8));
B=bitset(B,7,bitget(A,7));
B=bitset(B,6,bitget(A,6));
B=bitset(B,5,bitget(A,5));
B=uint8(B);
figure,imshow(B);
Image reconstructed using 5,6,7 and 8 bit planes






              Also check  bit plane compression.



Dienstag, 9. Oktober 2012

Read words in a file in reverse order


Consider a file that contains the following text,
            Brand New World
The result after reading the words in the reverse order
         World New Brand





Steps to be performed:
1.      Open a file and move the pointer to the end of the file.
2.      Move the pointer to the current-1 position
3.      Read the character and store it in an array.
4.      Move the pointer to the current-2 position and read the character and store it in an array.
5.      If the character is a blank space, then reverse the array. To reverse array use 'fliplr'.
6.      Append the array to a string and initialize the array to zero.
7.      Repeat this procedure till the pointer reaches the beginning of the file.




MATLAB CODE:

%Open a file to read
fp=fopen('rev.txt');

%Move the pointer to the end of the file
fseek(fp,0,'eof');

%Find the position
fsz=ftell(fp);

n=1;
%Move the pointer to the current-1 position
fseek(fp,-1,'cof');

%Read a character
c=fread(fp,1,'*char');

%Store in the matrix
M(1,n)=c;
Words=0;

%Check whether the file pointer has reached the beginning of the file
    while(fseek(fp,-2,'cof')~=-1)
       
        c=fread(fp,1,'*char');
        %When a space is encountered reverse the character array and append
        %it to a string
        if(isspace(c))
              if(Words==0)
                  %Intially, the string is empty.
                  %Append the array in the reverse order with a blank space
                   Words=[fliplr(M) blanks(1)];
              else
                  %Append the reversed character array to the string
                   Words=[Words fliplr(M)];
              end
           n=1;
           M='';
        else
            %The array is updated with the characters until blank space is
            %encountered
           n=n+1;
           M(1,n)=c;
          
        end


    end
   
Words=[Words fliplr(M)];
display(Words);

fclose(fp);








Result:

Words =

World New Brand