Skip to main content

Calculate the Gradient Magnitude and Angle

 How to Calculate the Gradient Magnitude and Angle?


import cv2
import numpy as np

def calculate(image):

image = np.sqrt(image)

gx = cv2.Sobel(np.float32(image), cv2.CV_32F, 1, 0)
gy = cv2.Sobel(np.float32(image), cv2.CV_32F, 0, 1)

mag, ang = cv2.cartToPolar(gx, gy)

return mag, ang, gx, gy

img=cv2.imread("bird.jpeg")
m,a,gx,gy=calculate(img)
cv2.imshow("gx",gx)
cv2.imshow("gy",gy)

print(m)
print(a)

cv2.waitKey()

Input:


Output:


Comments

Popular posts from this blog

OpenCV Add Border to Image in Python

How to add border to image in python? To create a frame around the image, the function cv2.copyMakeBorder () is used. This function takes the following arguments: src - login image top, bottom, left, right - edge width in number of pixels in corresponding directions borderType - defines what type of border to add. There may be the following types cv2.BORDER_CONSTANT  cv2.BORDER_REFLECT - cv2.BORDER_REFLECT_101 cv2.BORDER_REPLICATE cv2.BORDER_WRAP  value  - Color of border if border type is cv2.BORDER_CONSTANT import cv2 import numpy as np from matplotlib import pyplot as plt BLUE = [ 255 , 0 , 0 ] img1 = cv2.imread( 'flower.jpg' ) replicate = cv2.copyMakeBorder(img1 , 10 , 10 , 10 , 10 , cv2.BORDER_REPLICATE) reflect = cv2.copyMakeBorder(img1 , 10 , 10 , 10 , 10 , cv2.BORDER_REFLECT) reflect101 = cv2.copyMakeBorder(img1 , 10 , 10 , 10 , 10 , cv2.BORDER_REFLECT_101) wrap = cv2.copyMakeBorder(img1 , 10 , 10 , 10 , 10 , cv2.BORDER_WRAP) constant= cv2.copyMakeBorder(img1 ...