You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to visualize gradient data using arrows.
9 views (last 30 days)
Show older comments
I need help with the quiver function. I want to visualize the magnitude and angle data i have gotten from a block processing, with arrows in each block just like the (b) image below. Please how can i do that?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/287793/image.jpeg)
2 Comments
Ameer Hamza
on 26 Apr 2020
Can you share the information about the arrows directions in a .mat file?
Fego Etese
on 26 Apr 2020
I have attached the matab files and my matlab workspace. I am using the gray image to get the orientation.
I am getting the average magnitude and direction for each block of size 16 x 16 and after getting these in the OrientationEstimation file with the variables coherence and angle, i will like to draw the arrows in the direction of the angle with the length as the coherence value in each block.
Accepted Answer
Ameer Hamza
on 26 Apr 2020
Edited: Ameer Hamza
on 26 Apr 2020
According to the coherence and angle variable you shared. You can make the quiver plot using this
dX = coherence.*cosd(angle);
dY = coherence.*sind(angle);
x = 1:size(dX,2);
y = 1:size(dX,1);
[X, Y] = meshgrid(x, y);
quiver(X,Y,dX,dY);
axis tight
But it does not look like the image you shared. Are you sure that the formula used to calculate coherence and angle is correctly implemented?
28 Comments
Fego Etese
on 26 Apr 2020
Edited: Fego Etese
on 26 Apr 2020
Yes i believe i implemented it correctly. The result of the quiver is very different from the one i have above. The thing is the size of the coherence matrix is smaller than the actual image, because of the block processing, is it possible to plot an arrow in each block on the image representing the magnitude and direction of that block? maybe using block processing?
Fego Etese
on 26 Apr 2020
Looking at the code again, you're converting the magnitude and angle back to cartesian coordinates before passing into the quiver function, i thought in the quiver function the parameter u, the third parameter is the magnitude while the 4th parameter is the angle, or am i getting this all wrong?
Ameer Hamza
on 26 Apr 2020
I was doubtful because if you look, the outer edges of the coherence matrix have several 1s, which shows that the arrow magnitude will be largest. However, your image suggests that there are no arrows near the edges.
quiver function takes the x and y components of the vector field. You can see examples of the quiver.
Fego Etese
on 26 Apr 2020
Sorry, the 1s are caused by me, i did an if statement to check if all the pixels in a block are white and if they are i gave the block a coherence value of 1 and an angle of 0, that was to stop the error of NaN i was getting during the block processing.
So does that mean the quiver function uses the cartesian values of the vector field?
Ameer Hamza
on 26 Apr 2020
So should those 1s in coherence matrix be treated as 0s?
Yes, the quiver take the cartesian components of the vector field.
Fego Etese
on 26 Apr 2020
Yes, like they are not actually relevant as later on i will be segmenting the image before applying the orientatino estimation and those parts will be cut out, i just wanted to implement the orientation estimation first before i do the segmentation.
Please can you explain what the parameters of the quiver means? Like what does the x,y,u,v parameters take when passing arguments to the quiver in the context of gadients. I have tried to read the docs but i still dont understand.
Thanks
Ameer Hamza
on 26 Apr 2020
x and y define the grid of on which quiver will draw the vector field. Quiver works like this.
It takes an x-y coordinate, say
(x(i,j), y(i,j))
It goes to corresponding locations u and v matrices
(u(i,j), v(i,j))
and it draws a vector at location
and the x-y magnitude of vector is given by
. It iterates over all the locations in the matrix (x,y) and draws the vector with the corresponding location components in (u,v)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/287835/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/287836/image.png)
Fego Etese
on 26 Apr 2020
If i understand correctly, shouldn't u and v be the magnitude and angle of the arrow quiver draws, this has me confused? also, will it make sense if in the block processing, i get the coordinate of the center pixel of each block and use that to draw the vectors on the original gray image?
Please what do you use the meshgrid to do?
Fego Etese
on 26 Apr 2020
Edited: Fego Etese
on 26 Apr 2020
I just did some research and i think that the values for the coherence and direction of a block are to be applied to every pixel in that block when using the quiver, because the calculation for the coherence and direction is to get the average directional field of the image so as to get the orientation of the ridges in the gray image.
Ameer Hamza
on 27 Apr 2020
No, u, and v are the x and y components of the vector. This makes it easy to extend it to a higher dimension, such as quiver3. In 3D, using angles to specify the vector direction will be very cumbersome.
I think when you write the getAvgCoherence and getAvgAngle, you can return a block of 16x16 as output so that the dimension between input and output remains the same.
Fego Etese
on 27 Apr 2020
I have used this approach just now but the arrows drawn are too much in the image i would like to draw one big arrow or a line in each box using the angle and coherence. I am setting the coherence to 1 so it will be the same size all through the image. Please how can i do this? Something like this below
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/288075/image.png)
Ameer Hamza
on 28 Apr 2020
quiver() does not output an image, so there is no direct way to make the output of quiver of the same size as the input image. Also, I guess, the issue right now is the calculation of arrow direction at the edges. The coherence value is maximum at the edges, which makes the arrows very large. The arrow in the middle is smaller in comparison. You need to have a way to filter out the non-required coherence values.
Fego Etese
on 28 Apr 2020
Edited: Fego Etese
on 28 Apr 2020
I have filtered out the non-required coherence values by setting the coherence to be zero at the edges so it doesn't draw any arrows there but only in places that have a black pixel. And I set the rest to 1 to have uniform size. I understand that quiver doesn't output an image but at least is there a way to draw one big arrow in each block instead of pixel by pixel even if it's not using quiver? Because I'm starting to doubt whether quiver can draw this. Or should I increase the scale of the arrow and draw the arrow at the middle pixel of each block?
Ameer Hamza
on 28 Apr 2020
Does the function in blockproc return a single-pixel or a grid of 16x16? If it returns a single-pixel then the code in my answer should plot one array for a block.
Fego Etese
on 28 Apr 2020
At the moment it returns a grid of 16 by 16. Please how can I make it return a single pixel and still find that pixel in the quiver? Will this single pixel also be the center pixel in the block? Also please, when you say plot one array what do you mean?
Ameer Hamza
on 28 Apr 2020
I guess in the original code, you attached in your question. The function getAvgAngle and the other function take a block of 16x16 and return a single value. Right?
Fego Etese
on 28 Apr 2020
Yes that was what the original code outputted, but the single value is the averaged value for the whole block
Ameer Hamza
on 28 Apr 2020
Isn't that what you want. Just a single arrow for each block.
Fego Etese
on 28 Apr 2020
Yes, that's what I want but I want to plot it over the original image size, not on its own so that the arrows are in each block that was processed, maybe in the middle of each block.
Ameer Hamza
on 28 Apr 2020
Ok. I get it now. You want to plot it over the original image?? Apart from that, are you able to get correct arrows if you plot it on regular MATLAB axes using quiver?
Fego Etese
on 28 Apr 2020
Edited: Fego Etese
on 28 Apr 2020
Since I changed the coherence values for white pixels to 0 I haven't tried plotting again on Matlab axes. I will try that once I can access my laptop. But the last time I tried it with the code you gave me it didn't work as expected. I'm still trying to check my implementation whether it's correct.
Ameer Hamza
on 28 Apr 2020
I guess that if you are able to plot it correctly on MATLAB axes(), then you can also be able to plot it on the image. So, you first need to check, if the direction and size of arrows looks correct on axes().
Fego Etese
on 28 Apr 2020
I am now able to plot it at the center pixel of each block, I looked for the index of the center element in each block and set that center element to the coherence and angle values while leaving the rest of the elements in the block at zero so that when quiver plays the arrows it only plots at the center element and it plots on the axes of the original image and then I scaled the size of the arrow up by 15 so the arrows are bigger. Though unfortunately it seems like what I implemented did not work as the arrows aren't following the black ridges in the image. I guess that has to do with the algorithm I implemented right?
Ameer Hamza
on 28 Apr 2020
Are you able to create a small matrix (1value for 16x15 block)? If yes, then you don't need to increase its size to be the same as your image manually. I found that the quiver function has an option to overlay a smaller matrix on a larger image. If you can attach the image and the smaller matrix with coherence and angle information, I can suggest a solution.
Fego Etese
on 29 Apr 2020
Hi, Ameer, I'm sorry for being gone for a while, I've been trying to do some research into my implementation and i found out that I actually missed something there in it.
Firstly, I forgot to divide the final angle calculation by 2 and secondly I was plotting the wrong data. I was plotting the ridge valley direction instead of the gradient direction. And thirdly i hadn't actually finished the implementation.
Please now I am stuck at one point of the implementation, in this paper i don't understand the Eq 10 about the Guassian filter, I have only one value for each direction from Eq 8 and 9, but it asks me to apply a 2D gaussian filter to the image. I am assuming that I am to create two matrices of zeros same size as the block, fill the two block with the two values i have individually, and do a gaussian filter on both matrices using imgaussfilt, and then pick only one value from each of the filtered matrices. the problem here is that i don't understand that EQ10. Please am I correct in my understanding so far? Thank you ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/288686/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/288686/image.png)
Ameer Hamza
on 30 Apr 2020
I am not sure about these equations. I don't have a strong background in image processing, and understanding it will require some preliminary knowledge.
Fego Etese
on 30 Apr 2020
Oh, I understand. Thank you so much. I'll try to look into it more. Maybe raise a new question about it. When I'm done with understanding and implementing it I'll come back to sort out the quiver issue.
I really appreciuate your time.
Thanks
Ameer Hamza
on 30 Apr 2020
Yes, that will be good. You are likely to get an answer on this issue from someone else. You can then comment here again when this issue is resolved.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)