Lets Learn together... Happy Reading

" Two roads diverged in a wood, and I,
I took the one less traveled by,
And that has made all the difference "-Robert Frost

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.



like button Like "IMAGE PROCESSING" page

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 





like button Like "IMAGE PROCESSING" page

Read a file in reverse -from end of file to Beginning of the file


Consider a file that contains the following text,
            Brand New World
After reading the file in the reverse order the result will be
             dlroW weN dnarB









MATLAB CODE:

%Open a text file
fp=fopen('rev.txt');
%Move to the End Of the File
fseek(fp,0,'eof');
%Get the position
fsz=ftell(fp);
%Preallocate the character matrix
M=char(zeros([1 fsz]));
n=1;
%Move one position backward
fseek(fp,-1,'cof');
Here ‘cof’ is Current position of the file
%Read the character
c=fread(fp,1,'*char');
Here the ‘fread’ function reads one data in character format
%Store the character in the matrix
M(1,n)=c;
%Check whether the file pointer has reached the beginning of the file
    while(fseek(fp,-2,'cof')~=-1)
        n=n+1;
        c=fread(fp,1,'*char');
        M(1,n)=c;
    end

fclose(fp);


display(M);



Result:

M =

dlroW weN dnarB





like button Like "IMAGE PROCESSING" page

Image Erosion without using MATLAB function 'imerode'

In MATLAB, ‘imerode’ is a function used to make the objects thin. MATLAB code without using 'imerode' function and explanation is provided here. The input image is binary.



MATLAB CODE:



A=[1 0 1 1 1; 1 0 1 0 0; 1 1 1 0 0;0 0 1 1 1];
%Structuring element
B=[1 1 0];
%Pad array with ones on both sides
C=padarray(A,[0 1],1);
%Intialize the matrix D of size A with zeros
D=false(size(A));
for i=1:size(C,1)
    for j=1:size(C,2)-2
        In=C(i,j:j+2);
        %Find the position of ones in the structuring element
        In1=find(B==1);
        %Check whether the elements in the window have the value one in the
        %same positions of the structuring element
        if(In(In1)==1)
        D(i,j)=1;
        end
    end
end
display(D);


Explanation:
1.     Consider a matrix A and a structuring element B.
2.     Initialize a matrix D of size A with zeros.
3.     Construct a window of size B with the elements of matrix A.
4.     Check whether the ones in the structuring element B overlap the ones in the window.
5.     If it overlaps, then update D with one else zero.





Example 2:


A=imread('circles.png');
figure,imshow(A);
Original Image









%Structuring element
B=getnhood(strel('disk',11));

m=floor(size(B,1)/2);
n=floor(size(B,2)/2);
%Pad array on all the sides
C=padarray(A,[m n],1);
%Intialize a matrix with size of matrix A
D=false(size(A));
for i=1:size(C,1)-(2*m)
    for j=1:size(C,2)-(2*n)
       
        Temp=C(i:i+(2*m),j:j+(2*n));
       
        D(i,j)=min(min(Temp-B));
      
    end
end
figure,imshow(~D);


After Erosion
  

like button Like "IMAGE PROCESSING" page

Image Dilation without using 'imdilate' function


           In MATLAB, ‘imdilate’is the function that dilates the image using a structuring element. Let’s learn how this function works using some examples and codes.


MATLAB CODE:


Example 1:

A=[1 0 0 0 1; 1 0 1 0 0; 1 1 1 0 0;0 0 1 1 1];
%Structuring element
B=[1 0 0; 0 1 0; 0 0 1];
%Pad zeros on all the sides
C=padarray(A,[1 1]);
%Intialize a matrix of matrix size A with zeros
D=false(size(A));
for i=1:size(C,1)-2
    for j=1:size(C,2)-2
        %Perform logical AND operation
        D(i,j)=sum(sum(B&C(i:i+2,j:j+2)));
    end
end

display(D);

Example 2:
A=imread('text.png');
Original Image



A=im2bw(A);
%Structuring element
B2=getnhood(strel('line',7,90));
m=floor(size(B2,1)/2);
n=floor(size(B2,2)/2);
%Pad array on all the sides
C=padarray(A,[m n]);
D=false(size(A));
for i=1:size(C,1)-(2*m)
    for j=1:size(C,2)-(2*n)
        Temp=C(i:i+(2*m),j:j+(2*n));
        D(i,j)=max(max(Temp&B2));
    end
end

figure,imshow(D);

Dilated Image



Example 3:(Method 2)

A=imread('text.png');
A=im2bw(A);
%Structuring element
B=[1 1 1 1 1 1 1;];
C=padarray(A,[0 3]);
D=false(size(A));
for i=1:size(C,1)
    for j=1:size(C,2)-6
        D(i,j)=sum(B&C(i,j:j+6));
    end
end
figure,imshow(D);



Dilated image



























like button Like "IMAGE PROCESSING" page
Previous Post Next Post Home
Google ping Hypersmash.com