You access the indices x-1, y-1 and x+1, y+1. Then replace:
for x=1:size(thinned, 2)
for y=1:size(thinned,1)
by:
for x = 2:size(thinned, 2) - 1
for y = 2:size(thinned, 1) - 1
Currently you overwrite CN in each iteration. Is this wanted?
I think, that a proper indentation and sorting of the indices makes it much easier to debug:
CN = (abs(thinned(y+1, x-1) - thinned(y, x-1)) + ...
abs(thinned(y+1, x) - thinned(y+1, x-1)) + ...
abs(thinned(y+1, x+1) - thinned(y+1, x))) + ...
abs(thinned(y, x-1) - thinned(y-1, x-1)) + ...
abs(thinned(y, x+1) - thinned(y+1, x+1)) + ...
abs(thinned(y-1, x-1) - thinned(y-1, x)) + ...
abs(thinned(y-1, x) - thinned(y-1, x+1)) + ...
abs(thinned(y-1, x+1) - thinned(y, x+1));
CN = CN/2;
Best Answer