How exactly does the gradient function work when applying it to an image?

21 ビュー (過去 30 日間)
Flint Marko
Flint Marko 2021 年 11 月 1 日
編集済み: Chris 2021 年 11 月 1 日
I know the description for using gradient is: [FX,FY] = gradient(F) returns the x and y components of the two-dimensional numerical gradient of matrix F. The output FX corresponds to ∂F/∂x, which are the differences in the x(horizontal) direction. The spacing between points is assumed to be 1. The additional output FY corresponds to ∂F/∂y, which are the differences in the y (vertical) direction. The spacing between points in each direction is assumed to be 1.
If I use it as [FX,FY] = gradient(Image), what exactly is does the array it spits out mean? Is it which direction each pixel is changing the most? Does it take into account horizontal pixels or only the ones horizontal and vertical to it? What does ∂F represent here?
Finally, what is the difference between using gradient and imgradient used with 'central'?

採用された回答

Chris
Chris 2021 年 11 月 1 日
編集済み: Chris 2021 年 11 月 1 日
If you type open gradient, in the Command window, the relevant code is:
% Take forward differences on left and right edges
if n > 1
g(1,:) = (f(2,:) - f(1,:))/(h(2)-h(1));
g(n,:) = (f(n,:) - f(n-1,:))/(h(end)-h(end-1));
end
% Take centered differences on interior points
if n > 2
g(2:n-1,:) = (f(3:n,:)-f(1:n-2,:)) ./ (h(3:n) - h(1:n-2));
end
n is the number of points along the dimension being diff'd (the number of pixels)
h is the location of each pixel (if you don't specify, they're numbered with integers so the distance between each pixel is 1).
Away from the edges, it uses a centered difference approximation -- the difference between values of pixels to each side of the pixel being examined, divided by the distance between them (default 2).
At the edges, it uses the edge point and the next point over for the calculation, and the distance between them defaults to 1.
Using your example command [FX,FY] = gradient(F), the result is basically a diff along rows and columns.
imgradient calls gradient in a similar fashion, but then calculates a magnitude (Gmag) and direction (Gdir, -180 to 180 degrees) at each pixel from the results.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGeometric Transformation and Image Registration についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by