compute The horizontal absolute difference value of a pixel

216 Views Asked by At

We have the test image with M rows and N columns as f(x,y), for x∈ [1,M] and y∈ [1,N ]. The horizontal absolute difference value of a pixel is defined by D (x, y) = |f (x, y +1) − f (x, y −1)|. need help in how to implement it in matlab

3

There are 3 best solutions below

0
On
aux = abs(diff(f,[],2));
D = max(aux(:,1:end-1), aux(:,2:end));

For example: given

f = [3 5 6 4
     2 5 4 3
     8 9 3 1];

the result is

>> D
D =
     2     2
     3     1
     6     6
0
On
D = abs( f(1:end-1,:) - f(2:end,:) );

check out diff command as well. Note that D has 1 row less than f.

0
On

This will generate same size matrix, that you need:

mat1 = [zeros(2,size(f,2)); f];% adds 2 rows of zeros to begining
mat2 = [f;zeros(2,size(f,2))]; %adds 2 row of zeros to the end
Dd = mat1-mat2;
D = Dd(2:((size(Dd,1)-1)),:);%crop Dd matrix to size(f)