Vector components along another one

5 views (last 30 days)
this is my situation: I have a 30x30 image and I want to calculate the radial and tangent component of the gradient of each point (pixel) along the straight line passing through the centre of the image (15,15) and the same (i,j) point.
[dx, dy] = gradient(img);
for i=1:30
for j=1:30
pt = [dx(i, j), dy(i,j)];
line = [i-15, j-15];
costh = dot(line, pt)/(norm(line)*norm(pt));
par(i,j) = norm(costh*line);
tang(i,j) = norm(sin(acos(costh))*line);
end
end
is this code correct?

Accepted Answer

Roger Stafford
Roger Stafford on 15 Jul 2014
In the line
par(i,j) = norm(costh*line);
it should be
par(i,j) = costh*norm(line);
to allow the inward and outward radial components to be distinguished.
In the line
tang(i,j) = norm(sin(acos(costh))*line);
you will be unable to distinguish between clockwise and counterclockwise tangential components. To correct this you need to compute something other than the cosine of the angle between the two vectors. The sine of acos will always be positive. I would recommend this for both 'par' and 'tang':
line = line/norm(line);
tang(i,j) = line(1)*pt(2)-line(2)*pt(1);
par(i,j) = line(1)*pt(1)+line(2)*pt(2);
instead of computing 'costh'.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!