Effacer les filtres
Effacer les filtres

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

23 vues (au cours des 30 derniers jours)
Flint Marko
Flint Marko le 1 Nov 2021
Modifié(e) : Chris le 1 Nov 2021
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'?

Réponse acceptée

Chris
Chris le 1 Nov 2021
Modifié(e) : Chris le 1 Nov 2021
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.

Plus de réponses (0)

Catégories

En savoir plus sur Geometric Transformation and Image Registration dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by