How do I get the angle of an image gradient, using Python and Opencv

Asked

Viewed 149 times

0

I need to get the angle of the gradient of an image, I’m programming in Python and using Google Colab for the codes.

I found this code here, which should show me the angle of the gradient:

import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('./dt.jpg',0)
sobelx = cv2.Sobel(img,cv2.CV_32F,1,0,ksize=3)
sobely= cv2.Sobel(img,cv2.CV_32F,0,1,ksize=3)
phase=cv2.phase(sobelx,sobely,angleInDegrees=True)
print(phase)

but I also found this other code that should show me the angle:

gx = cv2.Sobel(img, cv2.CV_32F, 1, 0)
gy = cv2.Sobel(img, cv2.CV_32F, 0, 1)
print("\nX\n", gx, "\nY\n", gy)
mag, ang = cv2.cartToPolar(gx, gy)
print("\nMAG\n",mag,"\nANG\n",ang)

Both return me Arrays, but how do I convert this information to the degree of an angle? or is there another way to achieve this result?

  • 1

    I think you’re confusing that function Sobel realizes. It returns a vector with the derivatives of various gradient points in the image, used to find edges. So, by finding Sobel’s angle with Phase or cart2polar, you get a vector with all the angles of these derivatives...

  • Got it, I thought I’d have to calculate the angle with the vector that the method returned, thank you!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.