Hilditch Algorithm - some minor problems

13 views (last 30 days)
Shing Yan
Shing Yan on 7 Jan 2015
Edited: Shing Yan on 7 Jan 2015
I have been writing the code for Hilditch's Thinning Algorithm. The procedures are quite simple and straightforward. But let me explain what I am trying to do in general.
1. Open the image file
2. Convert to BW (Black = 0; White = 1)
3. Add extra rows and columns around the image (to prevent "Index out of bound" error while scanning through the image)
4. If all of the following conditions are met, the pixel will be deleted http://cgm.cs.mcgill.ca/~godfried/teaching/projects97/azar/skeleton.html The link tells everything.
The code:
clc
clear all
imdir='C:\Users\bryan\Desktop\matlab - skeletonization\';
imfile=['apple.jpg'];%num2str(Imagefile,'%05d')
I=imread([imdir,imfile]);
% Convert to BW by setting threshold (black = 0; white = 1)
I=im2bw(I,0.4); %range = 0-1
[row, col, depth] = size(I);
thinning = 1;
eraceCount = 0;
% add rows to image
addrow = ones(1, col);
I = [addrow; addrow; I; addrow];
[row, col, depth] = size(I);
% add columns to image
addcol = ones(row, 1);
I = horzcat(addcol, I, addcol, addcol);
[row, col, depth] = size(I);
eracePixel(row,col) = 0;
%start thinning
while thinning == 1
for r = 3:row-1
for c = 2:col-2
condition1 = 0;
condition2 = 0;
condition3 = 0;
condition4 = 0;
if I(r,c) == 0; %black pixel found
%for condition 1
B = 0;
B = I(r-1,c) + I(r-1,c+1) + I(r,c+1) +I(r+1,c+1) + I(r+1,c) + I(r+1,c-1) + I(r,c-1) + I(r-1,c-1);
if B>=2 && B<=6
condition1 = 1;
B = 0;
end
%for condition 2
A = 0;
if I(r-1,c) == 0 && I(r-1,c-1) == 1
A = A + 1;
end
if I(r-1,c+1) == 0 && I(r-1,c) == 1
A = A + 1;
end
if I(r,c+1) == 0 && I(r-1,c+1) == 1
A = A + 1;
end
if I(r+1,c+1) == 0 && I(r,c+1) == 1
A = A + 1;
end
if I(r+1,c) == 0 && I(r+1,c+1) == 1
A = A + 1;
end
if I(r+1,c-1) == 0 && I(r+1,c) == 1
A = A + 1;
end
if I(r,c-1) == 0 && I(r+1,c-1) == 1
A = A + 1;
end
if I(r-1,c-1) == 0 && I(r,c-1) == 1
A = A + 1;
end
if A == 1
condition2 = 1;
A = 0;
end
%for condition 3
if I(r-1,c) == 1 && I(r,c+1) == 1 && I(r,c-1) == 1
condition3 = 1;
end
%A(p2) =!1 (condition 3)
A2 = 0;
if I(r-2,c) == 0 && I(r-2,c-1) == 1
A2 = A2 + 1;
end
if I(r-2,c+1) == 0 && I(r-2,c) == 1
A2 = A2 + 1;
end
if I(r-1,c+1) == 0 && I(r-2,c+1) == 1
A2 = A2 + 1;
end
if I(r,c+1) == 0 && I(r-1,c+1) == 1
A2 = A2 + 1;
end
if I(r,c) == 0 && I(r,c+1) == 1
A2 = A2 + 1;
end
if I(r,c-1) == 0 && I(r,c) == 1
A2 = A2 + 1;
end
if I(r-1,c-1) == 0 && I(r,c-1) == 1
A2 = A2 + 1;
end
if I(r-2,c-1) == 0 && I(r-1,c-1) == 1
A2 = A2 + 1;
end
if A2 ~= 1
condition3 = 1;
end
%for condition 4
if I(r-1,c) == 1 && I(r,c+1) == 1 && I(r+1,c) == 1
condition4 = 1;
end
%A(p4) =!1 (condition 4)
A4 = 0;
if I(r-1,c+1) == 0 && I(r-1,c) == 1
A4 = A4 + 1;
end
if I(r-1,c+2) == 0 && I(r-1,c+1) == 1
A4 = A4 + 1;
end
if I(r,c+2) == 0 && I(r-1,c+2) == 1
A4 = A4 + 1;
end
if I(r+1,c+2) == 0 && I(r,c+2) == 1
A4 = A4 + 1;
end
if I(r+1,c+1) == 0 && I(r+1,c+2) == 1
A4 = A4 + 1;
end
if I(r+1,c) == 0 && I(r+1,c+1) == 1
A4 = A4 + 1;
end
if I(r,c) == 0 && I(r+1,c) == 1
A4 = A4 + 1;
end
if I(r-1,c) == 0 && I(r,c) == 1
A4 = A4 + 1;
end
if A4 ~= 1
condition4 = 1;
end
%marked as to be eraced
if condition1 + condition2 + condition3 + condition4 == 4
eracePixel(r,c) = 1;
eraceCount = eraceCount + 1;
end
end
end
end
if eracePixel == 0;
thinning = 0;
end
I = I + eracePixel;
%clear all values in matrix eracePixel
eracePixel = zeros(row, col);
end
imshow(I);
/////////////////end of code//////////////////////////////////////////
This is the image that I used for testing
It might be some silly mistakes inside. I hope someone can help me out while I am still looking for improvements. Thank you.

Answers (0)

Categories

Find more on MATLAB Report Generator in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!