Can you use an image gradient to complete a quiver plot?

조회 수: 10 (최근 30일)
Pranaya Kansakar
Pranaya Kansakar 2020년 5월 10일
댓글: darova 2021년 8월 14일
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
darova
darova 2020년 5월 10일
Yes it's possible. Try gradient
Pranaya Kansakar
Pranaya Kansakar 2020년 5월 12일
편집: Pranaya Kansakar 2020년 5월 12일
This was obviously the first thing I did but your suggestions came out as nonsenscial images.
I put the code:
sc = rgb2gray(sc);
>> [x y] = imgradient(sc);
>> figure;
imshowpair(x,y,'montage')
And the resulting image was as follows:
Surely, there is a better way to accurately extract the gradient representing the increase of pixel values with respect to distance?
something along the lines of https://en.wikipedia.org/wiki/Image_gradient#/media/File:Gradient2.svg

댓글을 달려면 로그인하십시오.

채택된 답변

darova
darova 2020년 5월 12일
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
Flint Marko
Flint Marko 2021년 8월 12일
편집: Flint Marko 2021년 8월 12일
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
darova 2021년 8월 14일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by