1 | initial version |
There are two possibilities:
Displaying arrows: you need to draw them manually. Divide the image in 5x5 pixel windows. Draw a line from the central pixel of each window (x0,y0)
to `(x0+5cos(direction),y0+5sin(direction). The drawback of this method is that you'll get a low-resolution vector map: you'll have only one vector for 25 pixels and only 16 directions.
There are variations of this method, for example getting a mean direction vector using PCA; modulating the vector length based on the gradient amplitude; using a different window size; drawing an arrow at the end of the vecotor (two more short lines) and so on.
Color-coded image: this is my favorite; it's simpler and gives a better resolution.
Create a 3 channel matrix with values (D,255,255)
, where D
is the direction normalized between 0-180 (2° units). Then convert this matrix from HSV to BGR (res=cv2.cvtColor(M,COLOR_HSV2BGR)
) and display it. The different colors will indicate the gradient direction (red=0°; yellow=60°, green=120°, blue=240°...)
As a variation, I suggest to add the normalized gradient amplitude A
between (0..255)
to the image, so each pixel becomes (D,255,A)
.