Quantcast
Channel: CodesMesh » Syed Wajahat Ali Shah
Viewing all articles
Browse latest Browse all 11

Skeleton of an image in Matlab

$
0
0

Skeleton of an image is in many ways same as skeleton of human body. It can be defined as if we perform erosion of a set A with a circular disk which is largest to fit in that set and it touches the boundary of set A at two or more places.

Skeleton of a set A can be expressed with iterative openings and closings as shown below.

S(A)=U(Sk(A)) where k varies from 0-K

and, Sk(A)=(A (erosion) kB)-( A (erosion) kB) (opening) B

where (A (erosion) kB)=(…(A (erosion) B) (erosion) B) (erosion) ….) (erosion) B or in other words erode A with set B k times. K is the last iterative step when A erodes to an empty set. An image and its skeleton is shown below.

 

The left side shows a triangle shaped binary image whose skeleton is sshown on right side.

Matlab Code for Skeleton of an image

a=imread('lec.png');
a=a(:,:,3);
imshow(a);
[r,c]=size(a);
b=uint8(zeros(r,c));
b
for i=1:r
    for j=1:c
        if(a(i,j)==255)
            a(i,j)=1;
        end  
    end
end
for i=1:r
    for j=1:c
        if(a(i,j)==1)
             min=a(i-1,j);
            if(a(i,j-1)<min)
             min=a(i,j-1);
             end
             a(i,j)=min+1;
           end     
end 
end
 for i=r:-1:1  
   for j=c:-1:1
         if(a(i,j)>1)
            min=a(i,j+1);
         if(a(i+1,j)<min)
             min=a(i+1,j);

         end
             if(min+1<a(i,j))  
                a(i,j)=min+1;
              end  
       end 
    end 
end 
max=0 
for i=1:r
for j=1:c
 if(a(i,j)>=max);
            max=a(i,j);
        end
    end
end
figure
imshow(a*10)

for g=1:max
for i=1:r
    for j=1:c    
        count=0;
        if(a(i,j)==g)
            if(a(i-1,j)>g)
                count=count+1;
            end
                if(a(i+1,j)>g)
                    count=count+1;
                end
                if(a(i,j-1)>g)
                    count=count+1;
                end
                if(a(i,j+1)>g)
                    count=count+1;
                end
                if(a(i-1,j+1)>g)
                    count=count+1;
                end
                if(a(i-1,j-1)>g)
                    count=count+1;
                end
                if(a(i+1,j-1)>g)
                    count=count+1;
                end
                if(a(i+1,j+1)>g)
                count=count+1;
            end
            if(count<=1)
                b(i,j)=a(i,j);
                b(i,j)=255;
                count=0;
            end

        end

    end

end
end
max

figure
imshow(b)

Note:

You have to download this “lec.png” file for working of this code properly as its used in it. For downloading right click small image shown below and save it as “lec.png” where you have your matlab code. Thanks for any further queries please comment below.


Viewing all articles
Browse latest Browse all 11

Trending Articles