Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Visually display gradient direction

I have computed the gradient magnitude and gradient direction for an image. Whats an easy way to visualise this gradient direction in OpenCV Python or C++? I've seen in tutorials a visual representation that overlays arrows on the original image. How would I produce this aswell?

I imagine I grid the gradient direction into cells, compute the average direction for each cell then draw an arrow in the centre of that cell. Is there an existing function in OpenCV that does this? Similar to drawKeyPoints()? When I manually calculate the

Visually display gradient direction

I have computed the gradient magnitude and gradient direction for an image. Whats an easy way to visualise this gradient direction in OpenCV Python or C++? I've seen in tutorials a visual representation that overlays arrows on the original image. How would I produce this aswell?

I imagine I grid the gradient direction into cells, compute the average direction for each cell then draw an arrow in the centre of that cell. Is there an existing function in OpenCV that does this? Similar to drawKeyPoints()? When I manually calculate the average I get a value that is 'distorted' by outliers. Ie, the zero gradient directions pull the average down:

def avg_gradient_directions(direction, w=2, h=2):

    res = np.zeros(direction.shape, dtype=np.uint8)
    kernel_w = w
    kernel_h = h
    n_cols = int(direction.shape[1] / kernel_w)
    n_rows = int(direction.shape[0] / kernel_h)

    for c in range(n_cols):
        for r in range(n_rows):
            roi = get_roi(direction, (c*kernel_w, r*kernel_h), ((c+1)*kernel_w, (r+1)*kernel_h))

            mean, stddev = cv2.meanStdDev(roi)
            print(mean)

    return res