Cannot display magnitude of gradient in form of image
I am trying to run this code. I took the blurred image and applied Sobel's kernel on it. kernel applied successfully like I am getting the vertical and horizontal components on the output but when I try to show the gradient it is giving an error. I have tried many ways but I am not getting a solution.
import numpy as np
import cv2 as cv
from scipy import ndimage
img=cv.imread("scarlet_guassian_blur.jpg",cv.IMREAD_UNCHANGED)
Kx = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]], np.float32)
Ky = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]], np.float32)
Ix = ndimage.filters.convolve(img, Kx)
Iy = ndimage.filters.convolve(img, Ky)
print("SobelX_Vertical\n",Ix)
print("SobelY_Horizontal\n",Iy)
cv.imshow("Gray_Scale",img)
cv.imshow("SobelX_Vertical",Ix)
cv.imshow("SobelY_Horizontal",Iy)
hyp=(Ix*Ix) + (Iy*Iy)
G=np.sqrt([hyp])
gradient = G / G.max() * 255
print("Gradient\n",gradient)
phase = np.arctan2(Iy, Ix)
print("Phase\n",phase)
cv.imshow("Gradient",gradient)
cv.waitKey()
cv.destroyAllWindows()
It is giving the following output in my terminal
SobelX_Vertical
[[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]
...
[ 2 2 254 ... 254 255 0]
[ 3 4 254 ... 250 253 0]
[ 1 4 255 ... 248 252 0]]
SobelY_Horizontal
[[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]
...
[ 6 2 0 ... 12 15 16]
[ 1 2 2 ... 8 11 12]
[ 1 2 1 ... 4 4 4]]
Gradient
[[[ 0. 0. 0. ... 0. 0. 0. ]
[ 0. 0. 0. ... 0. 0. 0. ]
[ 0. 0. 0. ... 0. 0. 0. ]
...
[102. 45.6 32.25 ... 196.1 242.4 0. ]
[ 51. 72.1 45.6 ... 161.2 183.8 193.5 ]
[ 22.8 72.1 22.8 ... 144.2 91.2 64.5 ]]]
Phase
[[0. 0. 0. ... 0. 0. 0. ]
[0. 0. 0. ... 0. 0. 0. ]
[0. 0. 0. ... 0. 0. 0. ]
...
[1.249 0.785 0. ... 0.0472 0.05875 1.57 ]
[0.3218 0.4636 0.00787 ... 0.03198 0.04346 1.57 ]
[0.785 0.4636 0.00392 ... 0.01613 0.01587 1.57 ]]
---------------------------------------------------------------------------TypeError Traceback (most recent call last)
<ipython-input-3-84576290db15> in <module>() 23 phase = np.arctan2(Iy, Ix)
24 print("Phase\n",phase)---> 25 cv.imshow("Gradient",gradient) 26 cv.waitKey()
27 cv.destroyAllWindows()TypeError: Expected Ptr<cv::UMat> for argument 'mat'