Vector components along another one

4 ビュー (過去 30 日間)
Gianfranco
Gianfranco 2014 年 7 月 15 日
回答済み: Roger Stafford 2014 年 7 月 15 日
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?

採用された回答

Roger Stafford
Roger Stafford 2014 年 7 月 15 日
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'.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangePoint Cloud Processing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by