Skip to content

Instantly share code, notes, and snippets.

@YaronBlinder
Created August 10, 2016 02:03
Show Gist options
  • Save YaronBlinder/44e8a78d7e4fad007b5cbbc38d842b3c to your computer and use it in GitHub Desktop.
Save YaronBlinder/44e8a78d7e4fad007b5cbbc38d842b3c to your computer and use it in GitHub Desktop.
Zhang_Suen_Odd
function out = Zhang_Suen_Odd(nhood);
sum1 = sum(nhood(:))-nhood(5); % Sum over neighborhood of a given pixel excluding center pixel
condition_1 = (3<=sum1)&(sum1<=6); % Condition 1 = pixel must have between 3 and 6 nonzero neighbors
ring1 = [nhood(1) nhood(4) nhood(7) nhood(8) nhood(9) nhood(6) nhood(3) nhood(2)]; % Create array from surrounding pixels starting with top left neighbor
ring2 = [ring1(2:8) ring1(1)]; % Same as ring1 but start at top middle entry
crossing_number = sum((1-ring1).*ring2); % Compute crossing number
condition_2 = crossing_number==1; % Condition 2: crossing number is equal to 1
product1 = nhood(4).*nhood(8).*nhood(6); % Odd iteration will only delete pixels at top left corner, right side, and bottom
product2 = nhood(8).*nhood(6).*nhood(2);
condition_3 = (product1==0)&(product2==0);
if condition_1&&condition_2&&condition_3&&nhood(5)==1; % If all three conditions are met
out=0; % Delete chosen pixel (set to 0)
else
out=nhood(5); % If not, keep selected pixel
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment