OpenCV-图像梯度与canny边缘检测

图像梯度

图像梯度计算的是图像变化的速度,对于图像的边缘部分,其灰度值变化较大,梯度值也较大;相反,对于图像中比较平滑的部分,其灰度值变化较小,相应的梯度值也较小。一般情况下,图像梯度计算的是图像的边缘信息。
在这里插入图片描述
在这里插入图片描述

Sobel算子

import cv2
import numpy as np
def cv_show(name,img):
    cv2.imshow(name,img)
    cv2.waitKey()
    cv2.destroyAllWindows()
img = cv2.imread('yuan.png',cv2.IMREAD_GRAYSCALE)
cv_show("yuan",img)

将Sobel算子与原始图像src进行卷积计算,可以计算水平方向上的像素值变化情况。

sobelx = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=3)
#因为右侧像素减去左边像素,存在负值的情况,因此使用cv2.convertScaleAbs取绝对值操作
sobelx = cv2.convertScaleAbs(sobelx)
cv_show("sobelx",sobelx)

将Sobel算子与原始图像src进行卷积计算,可以计算垂直方向上的像素值变化情况。

sobely = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=3)
#因为下侧像素减去下边像素,存在负值的情况,因此使用cv2.convertScaleAbs取绝对值操作
sobely = cv2.convertScaleAbs(sobely)
cv_show("sobely",sobely)

合并

sobel_demo = cv2.addWeighted(sobelx,0.5,sobely,0.5,0.5)
cv_show("sobel_demo",sobel_demo)
sobelxy = cv2.Sobel(img,cv2.CV_64F,1,1,ksize=3)
sobel=cv2.convertScaleAbs(sobelxy)
cv_show
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Python的NumPy和Matplotlib库来实现Canny边缘检测算法。以下是一个简单的实现示例: ```python import numpy as np import matplotlib.pyplot as plt def canny_edge_detection(image, sigma=1, kernel_size=5, low_threshold=0.1, high_threshold=0.3): # Convert image to grayscale gray = np.mean(image, axis=2) # Apply Gaussian blur blurred = np.zeros_like(gray) kernel = np.zeros((kernel_size, kernel_size)) for i in range(kernel_size): for j in range(kernel_size): kernel[i, j] = np.exp(-((i - kernel_size // 2) ** 2 + (j - kernel_size // 2) ** 2) / (2 * sigma ** 2)) kernel /= np.sum(kernel) for i in range(kernel_size // 2, gray.shape[0] - kernel_size // 2): for j in range(kernel_size // 2, gray.shape[1] - kernel_size // 2): blurred[i, j] = np.sum(gray[i - kernel_size // 2:i + kernel_size // 2 + 1, j - kernel_size // 2:j + kernel_size // 2 + 1] * kernel) # Compute gradient magnitude and direction dx = np.zeros_like(blurred) dy = np.zeros_like(blurred) for i in range(1, blurred.shape[0] - 1): for j in range(1, blurred.shape[1] - 1): dx[i, j] = blurred[i, j + 1] - blurred[i, j - 1] dy[i, j] = blurred[i + 1, j] - blurred[i - 1, j] magnitude = np.sqrt(dx ** 2 + dy ** 2) direction = np.arctan2(dy, dx) # Non-maximum suppression suppressed = np.zeros_like(magnitude) for i in range(1, magnitude.shape[0] - 1): for j in range(1, magnitude.shape[1] - 1): angle = direction[i, j] * 180 / np.pi if angle < 0: angle += 180 if (angle >= 0 and angle < 22.5) or (angle >= 157.5 and angle < 180): if magnitude[i, j] >= magnitude[i, j - 1] and magnitude[i, j] >= magnitude[i, j + 1]: suppressed[i, j] = magnitude[i, j] elif (angle >= 22.5 and angle < 67.5): if magnitude[i, j] >= magnitude[i - 1, j - 1] and magnitude[i, j] >= magnitude[i + 1, j + 1]: suppressed[i, j] = magnitude[i, j] elif (angle >= 67.5 and angle < 112.5): if magnitude[i, j] >= magnitude[i - 1, j] and magnitude[i, j] >= magnitude[i + 1, j]: suppressed[i, j] = magnitude[i, j] elif (angle >= 112.5 and angle < 157.5): if magnitude[i, j] >= magnitude[i - 1, j + 1] and magnitude[i, j] >= magnitude[i + 1, j - 1]: suppressed[i, j] = magnitude[i, j] # Double thresholding and edge tracking low_threshold *= np.max(suppressed) high_threshold *= np.max(suppressed) edges = np.zeros_like(suppressed) strong_i, strong_j = np.where(suppressed >= high_threshold) weak_i, weak_j = np.where((suppressed >= low_threshold) & (suppressed < high_threshold)) edges[strong_i, strong_j] = 1 while len(weak_i) > 0: i, j = weak_i[0], weak_j[0] weak_i, weak_j = np.delete(weak_i, 0), np.delete(weak_j, 0) if edges[i + 1, j] == 1 or edges[i - 1, j] == 1 or edges[i, j + 1] == 1 or edges[i, j - 1] == 1 or edges[i + 1, j + 1] == 1 or edges[i - 1, j - 1] == 1 or edges[i + 1, j - 1] == 1 or edges[i - 1, j + 1] == 1: edges[i, j] = 1 return edges # Test the function on a sample image image = plt.imread('lena.png') edges = canny_edge_detection(image) plt.imshow(edges, cmap='gray') plt.show() ``` 这个实现使用了高斯滤波器来平滑图像,计算梯度幅值和方向,进行非极大值抑制,双阈值处理和边缘跟踪。可以通过调整参数来控制算法的性能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值