图像处理之sobel算子,Scharr算子,拉普拉斯算子

图像处理之sobel算子,Scharr算子,拉普拉斯算子

不同算子的卷积核

1.sobel算子

sobel算子卷积核

Sobel算子通过将上述模板与图像进行卷积运算来实现边缘检测。对于图像中的每个像素,将Sobel_x和Sobel_y分别与该像素周围的3x3邻域进行卷积,然后将两个方向的梯度值进行合并,得到该像素的边缘梯度。
以上的Gx与Gy分别提取水平梯度和垂直梯度。

2.Scharr算子

Scharr算子卷积核

Scharr算子类似于Sobel算子,但在计算梯度时具有更高的灵敏度。Scharr算子与Sobel算子一样,也有水平和垂直两个方向,分别称为Scharr_x和Scharr_y。
以上的Gx与Gy分别提取水平梯度和垂直梯度。
与Sobel算子相比,Scharr算子的权值更大,这使得它对图像中边缘的响应更为敏感。因此,Scharr算子在一些情况下可以提供比Sobel算子更好的边缘检测效果。
Scharr算子的原理和Sobel算子类似,它也通过将上述模板与图像进行卷积运算来计算每个像素点的梯度值,并利用梯度值来找到图像中的边缘。在边缘检测中,Scharr算子通常可以产生更细致和更准确的边缘检测结果。
在实际应用中,选择使用Sobel算子还是Scharr算子取决于具体的图像处理任务和对边缘检测的要求。如果需要更敏感和准确的边缘检测,可以考虑使用Scharr算子;如果希望算法计算较简单或计算资源有限,Sobel算子可能是更合适的选择。

3.拉普拉斯算子

拉普拉斯算子卷积核

拉普拉斯算子用于图像的边缘检测和图像增强。它可以帮助我们找到图像中的边缘、角点和纹理等特征。
以上3x3的矩阵,称为拉普拉斯模板。它可以对图像进行离散卷积,计算出每个像素点与其周围邻域的差异,从而提取出图像中的高频信息,即边缘和纹理等细节。
拉普拉斯算子的原理是通过计算像素点的二阶导数来寻找图像中的变化。一阶导数可以帮助我们找到边缘,而二阶导数则可以帮助我们找到边缘的交叉点,即角点。拉普拉斯算子在图像中检测出的边缘通常比Sobel和Scharr算子更细,但也更容易受到噪声的干扰。
在实际应用中,拉普拉斯算子通常结合阈值处理来进行边缘检测。边缘点的灰度值会在图像上表现为明显的亮度变化,因此可以通过设置阈值来确定哪些像素点被认为是边缘。阈值处理后,可以得到一个二值图像,其中边缘点被表示为白色,背景为黑色。
由于拉普拉斯算子对噪声比较敏感,因此在实际应用中,通常会进行预处理,如平滑滤波,以减少噪声对边缘检测的影响。常见的预处理方法包括高斯模糊和中值滤波等。

使用opencv实现三种算子代码

import cv2
import numpy as np
import matplotlib.pyplot as plt

# 定义卷积核
Sobel_x = np.array([[-1, 0, 1],
                    [-2, 0, 2],
                    [-1, 0, 1]])

Sobel_y = np.array([[-1, -2, -1],
                    [0, 0, 0],
                    [1, 2, 1]])

Scharr_x = np.array([[-3, 0, 3],
                     [-10, 0, 10],
                     [-3, 0, 3]])

Scharr_y = np.array([[-3, -10, -3],
                     [0, 0, 0],
                     [3, 10, 3]])

Laplacian = np.array([[0, 1, 0],
                      [1, -4, 1],
                      [0, 1, 0]])

# 读取图像
img = cv2.imread('Lena.jpg', cv2.IMREAD_GRAYSCALE)

# 卷积函数
def convolve(image, kernel):
    return cv2.filter2D(image, -1, kernel)

# 应用卷积核并显示结果
sobel_x_result = convolve(img, Sobel_x)
sobel_y_result = convolve(img, Sobel_y)
scharr_x_result = convolve(img, Scharr_x)
scharr_y_result = convolve(img, Scharr_y)
laplacian_result = convolve(img, Laplacian)


# 显示原始图像和结果图像
plt.figure(figsize=(10, 10))

plt.subplot(3, 2, 1)
plt.imshow(img, cmap='gray')
plt.title('Original Image')

plt.subplot(3, 2, 2)
plt.imshow(sobel_x_result, cmap='gray')
plt.title('Sobel X')

plt.subplot(3, 2, 3)
plt.imshow(sobel_y_result, cmap='gray')
plt.title('Sobel Y')

plt.subplot(3, 2, 4)
plt.imshow(scharr_x_result, cmap='gray')
plt.title('Scharr X')

plt.subplot(3, 2, 5)
plt.imshow(scharr_y_result, cmap='gray')
plt.title('Scharr Y')

plt.subplot(3, 2, 6)
plt.imshow(laplacian_result, cmap='gray')
plt.title('Laplacian')

plt.tight_layout()
plt.show()

运行结果
运行结果

以上代码分别实现了sobel算子和Scharr算子提取水平梯度和垂直梯度,这两种方法在实际场景中经常应用
以下是实现整个水平梯度和垂直梯度融合的算法
下面展示一些 内联代码片

import cv2
import numpy as np

def apply_sobel(image):
    # Convert the image to grayscale
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    
    # Apply the Sobel operator to compute gradients in x and y directions
    sobel_x = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=3)
    sobel_y = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=3)
    
    # Convert the gradients back to unsigned 8-bit integers
    abs_sobel_x = np.uint8(np.absolute(sobel_x))
    abs_sobel_y = np.uint8(np.absolute(sobel_y))
    
    # Combine the x and y gradients to get the final Sobel result
    sobel_combined = cv2.bitwise_or(abs_sobel_x, abs_sobel_y)
    
    return sobel_combined

def apply_scharr(image):
    # Convert the image to grayscale
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    
    # Apply the Scharr operator to compute gradients in x and y directions
    scharr_x = cv2.Scharr(gray, cv2.CV_64F, 1, 0)
    scharr_y = cv2.Scharr(gray, cv2.CV_64F, 0, 1)
    
    # Convert the gradients back to unsigned 8-bit integers
    abs_scharr_x = np.uint8(np.absolute(scharr_x))
    abs_scharr_y = np.uint8(np.absolute(scharr_y))
    
    # Combine the x and y gradients to get the final Scharr result
    scharr_combined = cv2.bitwise_or(abs_scharr_x, abs_scharr_y)
    
    return scharr_combined

def apply_laplacian(image):
    # Convert the image to grayscale
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    
    # Apply the Laplacian operator to detect edges
    laplacian = cv2.Laplacian(gray, cv2.CV_8U)
    
    return laplacian

# Load the image
image_path = "path_to_your_image.jpg"  # Replace with the actual image path
image = cv2.imread(image_path)

# Apply Sobel operator
sobel_result = apply_sobel(image)

# Apply Scharr operator
scharr_result = apply_scharr(image)

# Apply Laplacian operator
laplacian_result = apply_laplacian(image)

# Display the results
cv2.imshow("Original Image", image)
cv2.imshow("Sobel Operator", sobel_result)
cv2.imshow("Scharr Operator", scharr_result)
cv2.imshow("Laplacian Operator", laplacian_result)
cv2.waitKey(0)
cv2.destroyAllWindows()
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值