I am interested in the Sobel function for edge detection. I would like to return the exact position of horizontal and vertical edges. However the sobel operator values are offset. As I understand, the sobel operation give the variation along an axis. Thus, a negative sobel value would mean that the edge is located before and a positive sobel value would mean that the edge is located after.
Is there a standart method to retrieve the exact location of an edge?
current input:
import pandas as pd
import numpy as np
import cv2 as cv2
df = pd.DataFrame(np.array([[1,0,0,0],
[1,0,0,0],
[1,1,1,1],
[1,0,0,0]]), columns=list('ABCD'), index = list('ABCD'))
img = pd.DataFrame.to_numpy(df) #create a numpy array from the df
img = img.astype(np.uint8)
# Sobel Edge Detection
sobelx = cv2.Sobel(src=img, ddepth=cv2.CV_64F, dx=1, dy=0, ksize=3) # Sobel Edge Detection on the X axis
sobely = cv2.Sobel(src=img, ddepth=cv2.CV_64F, dx=0, dy=1, ksize=3) # Sobel Edge Detection on the Y axis
print(sobelx)
print(sobely)
current output for x direction and y direction:
#x_dir
[[ 0. -4. 0. 0.]
[ 0. -3. 0. 0.]
[ 0. -2. 0. 0.]
[ 0. -2. 0. 0.]]
#y_dir
[[0. 0. 0. 0.]
[2. 3. 4. 4.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
First to decouple the
x, andyaxis, try theSobeloperator on two different arrays.The output would be:
According to Wikipedia:
As you can see in the
y-axisarray. Thesobel filterwhich is a 3X3 matrix, starts to respond to the third row already in the second row. This is because convolving the filter over the pixels gives us the gradient in the vicinity depending on the filter.