Can you use an image gradient to complete a quiver plot?
6 views (last 30 days)
Show older comments
Is it possible to plot a quiver plot of a gradient, such as the one attached, showing the magnitude and the direction of the change in pixel intensity?
Could the resulting plot then be used to create a boundary between the two extreme color pixel values?
2 Comments
Accepted Answer
darova
on 12 May 2020
Try this madness
I0 = imread('image.png');
I1 = rgb2gray(I0); % convert to gray/intensity
I1 = double(I1); % conver integer to double
[m,n] = size(I1);
[u,v] = gradient(I1,1,1); % calculate gradient (dx and dy)
[x,y] = ndgrid(1:m,1:n); % x,y grid
ii = 1:100:numel(x);
quiver(x(ii),y(ii),u(ii),v(ii))
2 Comments
Flint Marko
on 12 Aug 2021
Edited: Flint Marko
on 12 Aug 2021
I used this code on a gradient image and saw pretty good results. I did change one bit as I wanted to display the arrows on every pixel:
ii = 1:1:numel(x); % to add an arrow for every pixel
I am confused as to what exactly the direction of the arows means. In general they are pointing from darkest(0) to lightest pixel(up to 255) in their vicinity, with the size of the arrow corresponding to how drastic the change is. For example a black pixel(0) neighboring a white pixel(255) would have a larger arrow than a black pixel(0) neighboring a grey pixel(~50).
With the code you provided however, the angles of the arrows are not pointing directly at the center of the brightest neighboring pixel as you might expect, at times it will be in a horizontal directions. Still, in general the arrows point in the desired direction, but I am unsure how you would use this to quantify a gradient. For example how can the data extracted from [Gmag,Gdir] be used to show that there is a difinitive difference between an image with a gradient in a clear direction vs an image with no gradient and no clear direction? Additionally, can we sum the vectors to show one big arrow showing the angle in which the gradient is generally headed?
darova
on 14 Aug 2021
Sorry, didn't test the code. I made a mistake (see comments). I made some changes to the code, mainly removed large values in gradients (appears because of boundary)
I0 = imread('image.png');
I1 = rgb2gray(I0); % convert to gray/intensity
I1 = double(I1); % conver integer to double
[m,n] = size(I1);
[u,v] = gradient(I1); % calculate gradient (dx and dy)
[y,x] = ndgrid(1:m,1:n); % x,y grid (made a mistake before)
ind = find( abs(u) < 10 & abs(v) < 10 ); % choose only small values
ii = ind(1:1500:numel(ind)); % reduce quivers
quiver(x(ii),y(ii),u(ii),v(ii))
axis equal
More Answers (0)
See Also
Categories
Find more on Vector Fields in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!