how to calculate gradient between the currently processed point (x,y) and its neighboring point in one of eight compass direction.

Dear All,
i'm working for make iterative threshold technique. This technique is first modified with calculating gradient between currently proccessed point (x,y) and its neighboring point in one of eight compass direction that can determined by this equation :
Gd(x,y) = |I(x,y)-I(xd,yd)|
where :
(xd,yd) neighbors to (x,y) in direction d, and I(x,y) and I(xd,yd) denote the gray-level values at locations (x,y) and I(xd,yd). Here d is a value denoting one of eight compass direction.
The value of eight compass direction is
north = [2,10,-6];
north_east = [1,9,-7];
east = [0,8,-8];
south_east = [7,15,-1];
south = [6,14,-2];
south_west = [5,13,-3];
west = [4,12,-4];
nort_west = [3,11,-5];
My problem is how to determined d to take a value of eight compass direction, so i can determined the neighbors point?? i'm really new in gradient based eight direction. Can anyone help me, please?? ^_^

2 Comments

The first number in each of your triples starts at 0 for east and increases by 1 for each multiple of 45 degrees counter-clockwise from east. But I have no idea what the second and third value in each of your triples are intended to represent or how they are to be used.
thanks for the comment Walter.
actually, i have no idea too about it, and that's why did i ask. =D
So, is there any reference code about eight compass direction that can solve my problem??
thanks before ^_^

Sign in to comment.

 Accepted Answer

[nrow, ncol] = size(I);
for x = 1 : ncol
for y = 1 : nrow
if y < nrow; G0(x,y) = abs(I(x,y) - I(x,y+1)); end %E
if x ~= 1 && y < nrow; G1(x,y) = abs(I(x,y) - I(x-1,y+1)); end %NE
if x ~= 1; G2(x,y) = abs(I(x,y) - I(x-1,y)); end %N
[...]
if x < ncol & y < nrow; G8(x,y) = abs(I(x,y) - I(x+1,y+1)); end %SE
end
end
So now what?

2 Comments

thank you very much for the reference code ^_^
can you tell me please, why you give a decision on that looping, just like "if y < nrow", " if ~= 1", and so on.
thanks alot before =)
If you do not use those tests then you will get matrix references that are out of range, such as trying to access I(y,0)

Sign in to comment.

More Answers (2)

The question is not really clear to me, but I like to guess:
I = rand(100, 100, 3);
m = size(I, 1);
n = size(I, 2);
pre = NaN(size(I));
G_n = pre;
G_s = pre;
G_w = pre;
G_e = pre;
G_nw = pre;
G_sw = pre;
G_ne = pre;
G_se = pre;
G_n(1:m-1, :, :) = diff(I, 1, 1);
G_s(2:m, :, :) = G_n(1:m-1, :, :);
G_e(:, 1:n-1, :) = diff(I, 1, 2);
G_w(:, 2:n, :) = G_e(:, 1:n-1, :);
G_ne(2:m, 1:n-1, :) = I(1:m-1, 2:n, :) - I(2:m, 1:n-1, :);
G_nw(2:m, 2:n, :) = I(1:m-1, 1:n-1, :) - I(2:m, 2:n, :);
G_se(2:m, 1:n-1, :) = I(1:m-1, 2:n, :) - I(2:m, 1:n-1, :);
G_sw(2:m, 2:n, :) = I(1:m-1, 1:n-1, :) - I(2:m, 2:n, :);
Thanks All,,
the reference code is very helpfull =)
but i have another problem.
I want to take the mean of Gd(x,y), so i use this coding
n = mean(G_n + G_s + G_e + G_w + G_ne + G_nw + G_se + G_sw);
but there is an error :
??? Error using ==> plus
Matrix dimensions must agree.
Error in ==> tetangga at 13
n = mean(G_n + G_s + G_e + G_w + G_ne + G_nw + G_se + G_sw);
how i can solve this error??

11 Comments

What are the size() of each of the array? If one of them is not the same size as the others, which one is it? Now go back to the code and figure out why that one is smaller.
sorry, i'm late for reply =)
i'd been doing experiments on images with size 512x512
this is the list of size of each array:
G_n = [512 512] ==> north
G_s = [512 512] ==> south
G_e = [512 511] ==> east
G_w = [511 512] ==> west
G_ne = [512 511] ==> northeast
G_nw = [512 512] ==> northwest
G_se = [511 511] ==> southeast
G_sw = [511 512] ==> southwest
So, what should i do to make it have same size?
thanks before =)
Did you use my code, but forget to include the "G_[] = pre;" lines? Then add these lines.
If you are in the south-most row, what is south of it that you could calculate a "south" value for that row?
Did you use my code, but forget to include the "G_[] = pre;" lines? Then add these lines.
Using the mean of these values is unusual: The gradient in n-s and e-w direction have a shorter base line, while the gradients in ne-sw and nw-se direction use a base line, which is sqrt(2) times longer.
Dear All,,
please pardon me because my research about image gradient is fairly new.
i'd checked the error and correct me if my analysis is wrong. obviously, after i add "G_[] = pre" lines where the "pre = NaN(size())" line is modified into "pre = zeros(size())" line, it can be calculated. So, i think the value of it is the mean of Gd(x,y).
And so, what do you think?? =)
Is it meaningful to be using those zeros that are not being replaced by calculated values?
heheheee :D
Actually, i'm really not sure about it :D. It just can be calculated when i used those zeros. So, can you correct me?? =)
You could apply your functions to your G_* arrays indexed at (2:end-1, 2:end-1) . You at least have calculated data for all those locations. But Jan or I cannot offer corrected code as it does not make sense to us to be taking the mean or whatever of those values; see Jan's comments about the differing scales.
What is the current problem exactly?
If you use "G_e = zeros(size(I))" or "G_e = NaN(size(I))" does not matter the ability to be computed. The problem was, that Ge had the size [512 511], and this meand, that you have called "G_e(:, 1:n-1, :) = diff(I, 1, 2);" without the pre-allocation.
The gradient is a vector for 2D data. Building the mean of the components is not meaningful. If the value is +1 in n-s direction and -1 in w-e direction, the mean 0 is free of use.
Jan, I think you missed the absolute value bars in the original problem statement.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!