Can I use numpy gradient function with images
Central differences in the interior and first differences at the boundaries.
15 - 10
13 - 10 / 2
24 - 15 / 2
...
39 - 28
Gradient for Color Image in Python
Find each channel's gradient separately like this
gradR = np.sqrt(np.square(Rx) + np.square(Ry))
gradG = np.sqrt(np.square(Gx) + np.square(Gy))
gradB = np.sqrt(np.square(Bx) + np.square(By))
make a new image
grad = np.dstack((gradR,gradG,gradB))
How to apply gradient/magnitude to an image using OpenCV?
I think there might be an issue with ndimage.filters.convolve. I got similar results as you. But the following seems to work fine using Python/OpenCV
Input:
import cv2
import numpy as np
import skimage.exposure
img = cv2.imread('black_dress.png', cv2.IMREAD_GRAYSCALE)
img = cv2.GaussianBlur(img, (0,0), sigmaX=1.5, sigmaY=1.5)
Kx = np.array([[-1, 0, 1],
[-2, 0, 2],
[-1, 0, 1]])
Ky = np.array([[1, 2, 1],
[0, 0, 0],
[-1, -2, -1]])
Ix = cv2.filter2D(img, -1, Kx)
Iy = cv2.filter2D(img, -1, Ky)
G = np.hypot(Ix, Iy)
G = skimage.exposure.rescale_intensity(G, in_range='image', out_range=(0,255)).astype(np.uint8)
theta = np.arctan2(Iy, Ix)
theta = skimage.exposure.rescale_intensity(theta, in_range='image', out_range=(0,255)).astype(np.uint8)
cv2.imwrite('black_dress_gradient_magnitude.png', G)
cv2.imwrite('black_dress_gradient_direction.png', theta)
cv2.imshow("magnitude", G)
cv2.imshow("direction", theta)
cv2.waitKey(0)
Magnitude:
Direction:
Gradient of edges Python
If what you want is the total number of pixels comprising horizontal vs vertical edges, I would suggest defining some threshold for horizontal vs vertical (say 15 degrees). So you can count the number of elements of theta
for which abs(theta) < pi/12
(horizontal)
or abs(theta) > pi-pi/12
(horizontal)
or pi/2 - pi/12 < abs(theta) < pi/2+pi/12
(vertical)
What you're storing in v
and h
are the vertical and horizontal components of the gradient at each point and what you need is to compare the values of v
and h
to determine for each point if the gradient vector should count as horizontal or vertical. Comparing theta
is probably the most intuitive way to do this.
In order to get the number of elements of theta that satisfy a particular condition, I would suggest using a generator expression:
sum(1 for i in theta if (abs(i)<pi/12) or (abs(i)>pi-pi/12))
would give you the number of horizontal edge pixels for example.
Related Topics
Replacing Blank Values (White Space) With Nan in Pandas
How to Remove Parentheses from a String
How to Remove Square Brackets from List in Python
How to Remove Strings Present in a List from a Column in Pandas
How to Clear Only Last One Line in Python Output Console
Parentheses and Quotation Marks in Output
How to Save All the Variables in the Current Python Session
Remove Partial String from Dataframe With Pandas
How to Call a Django Function on Button Click
Why I Get Key Error Even Though Column Present in Pandas
How to Remove Nan from List Python/Numpy
How to Do a Conditional Count After Groupby on a Pandas Dataframe
Python: [Errno 10054] an Existing Connection Was Forcibly Closed by the Remote Host
How to Limit a Number to Be Within a Specified Range (Python)
Python: Fastest Way to Compare Arrays Elementwise