我对用于边缘检测的Sobel函数感兴趣。我想返回水平和垂直边缘的确切位置。然而,sobel算子的值被抵消。据我所知,sobel操作给出了一个轴的变化。因此,负sobel值意味着边缘位于前面,正sobel值表示边缘位于后面。
是否有一种标准方法来检索边缘的准确位置?
当前输入:
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)X方向和y方向的电流输出:
#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.]]发布于 2022-08-08 14:18:32
首先,为了解耦x和y轴,在两个不同的数组上尝试使用Sobel操作符。
import cv2 as cv2
import numpy as np
#create numpy array of 10 by 10 with zeros
arr_1 = np.zeros((10,10))
arr_2 = np.zeros((10,10))
#set third row to 1
arr_1[2,:] = 1
# and fourth column to 1
arr_2[:,3] = 1
img_1 = arr_1.astype(np.uint8)
img_2 = arr_2.astype(np.uint8)
# Sobel Edge Detection
sobely = cv2.Sobel(src=img_1, ddepth=cv2.CV_64F, dx=0, dy=1, ksize=3) # Sobel Edge Detection on the Y axis
print(img_1, '\n')
print(sobely)
sobelx = cv2.Sobel(src=img_2, ddepth=cv2.CV_64F, dx=1, dy=0, ksize=3) # Sobel Edge Detection on the X axis
print(img_2, '\n')
print(sobelx,'\n')产出如下:
[[0 0 0 0 0 0 0 0 0 0]
[0 0 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. 0.]
[ 0. 0. 4. 0. -4. 0. 0. 0. 0. 0.]
[ 0. 0. 4. 0. -4. 0. 0. 0. 0. 0.]]根据维基百科
从技术上讲,它是一个离散微分算子,计算图像强度函数梯度的近似。
正如您在y-axis数组中看到的那样。sobel filter是一个3X3矩阵,它开始响应第二行中的第三行。这是因为将滤波器转移到像素上会给我们附近的梯度,这取决于滤光片。
发布于 2022-08-08 14:31:06
拉普拉斯算子可以帮上忙。它计算给定数组的二阶导数。在这样做时,可以确定边缘的确切位置。
对于下面的数组c
>>> c
array([[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0]], dtype=uint8)这是拉普拉斯的结果:
>>> dst = cv2.Laplacian(c, cv2.CV_64F, ksize=3)
>>> dst
array([[ 0., 4., -8., 4., 0.],
[ 0., 4., -8., 4., 0.],
[ 0., 4., -8., 4., 0.],
[ 0., 4., -8., 4., 0.]])https://stackoverflow.com/questions/73277897
复制相似问题