How exactly does the gradient function work when applying it to an image?
20 views (last 30 days)
Show older comments
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'?
0 Comments
Accepted Answer
Chris
on 1 Nov 2021
Edited: Chris
on 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.
0 Comments
More Answers (0)
See Also
Categories
Find more on Geometric Transformation and Image Registration in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!