编程实现一阶锐化(sobel算子)和二阶锐化
时间: 2024-02-05 20:02:56 浏览: 81
一阶锐化(sobel算子)和二阶锐化是图像处理中常用的锐化算法,下面是使用Python实现的代码:
一阶锐化(sobel算子):
```python
import cv2
import numpy as np
# 读取图像
img = cv2.imread('image.jpg')
# 灰度化
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Sobel算子
sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=3)
sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=3)
# 计算梯度幅值和方向
grad_x = cv2.convertScaleAbs(sobelx)
grad_y = cv2.convertScaleAbs(sobely)
grad = cv2.addWeighted(grad_x, 0.5, grad_y, 0.5, 0)
# 显示图像
cv2.imshow('image', img)
cv2.imshow('sobel', grad)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
二阶锐化:
```python
import cv2
import numpy as np
# 读取图像
img = cv2.imread('image.jpg')
# 灰度化
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Laplacian算子
laplacian = cv2.Laplacian(gray, cv2.CV_64F)
# 计算梯度幅值
grad = cv2.convertScaleAbs(laplacian)
# 显示图像
cv2.imshow('image', img)
cv2.imshow('laplacian', grad)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
阅读全文