Do you want to record the difference once or twice? If you count 8 differece per pixel, then you are recording each difference twice: once between i
and j
and once (the same abs difference) between j
and i
.
Here's a loop-free option for 2D image I
d1 = abs( I(:,1:end-1) - I(:,2:end) ); % difference left-right
d2 = abs( I(1:end-1,:) - I(2:end,:) ); % diff up
d3 = abs( I(1:end-1,1:end-1) - I(2:end,2:end) ); % 1st diagonal
d4 = abs( I(1:end-1,2:end) - I(2:end,1:end-1) ); % 1st diagonal
allDiff = [d1(:); d2(:); d3(:); d4(:) ]; % stack them together.