Sobel与Canny边缘检测代码如下所示
import cv2 as cv
import matplotlib.pyplot as plt
读取图像的代码如下所示
# 读取图像
img = cv.imread('Lena.jpg')
lenna_img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
图像转换成灰度图像的代码如下所示
# 图像转换成灰度图像
grayImage = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
Sobel算子的代码如下所示
'''
Sobel算子
'''
x = cv.Sobel(grayImage, cv.CV_16S, 1, 0) # 对x求一阶导数
y = cv.Sobel(grayImage, cv.CV_16S, 0, 1) # 对y求一阶导数
absX = cv.convertScaleAbs(x)
absY = cv.convertScaleAbs(y)
Sobel = cv.addWeighted(absX, 0.5, absY, 0.5, 0)
Canny算子的代码如下所示
'''
Canny算子
'''
# 高斯滤波降噪
gaussian = cv.GaussianBlur(grayImage, (3, 3), 0)
# Canny算子
Canny = cv.Canny(gaussian, 50, 100)
显示正常中文的标签的代码如下所示
# 显示正常中文的标签
plt.rcParams['font.sans-serif'] = ['SimHei']
titles = [u'(a)原始图像', u'(b)Sobel图像', u'(c)Canny图像']
images = [lenna_img, Sobel, Canny]
for i in range(3):
plt.subplot(1, 3, i + 1), plt.imshow(images[i], 'gray')
plt.title(titles[i])
plt.xticks([]), plt.yticks([])
plt.show()
边缘检测运行结果结果如下所示
Sobel与Canny边缘检测的优缺点
Susan与Harris角点检测代码如下所示
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
读取图像的代码如下所示
# 读取图像
img = cv.imread('Lena.jpg')
lenna_img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
图像转换成灰度图像的代码如下所示
# 图像转换成灰度图像
grayImage = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
grayImage = np.float32(grayImage)
Harris算子的代码如下所示
'''
Harris算子
'''
harrisImage = cv.cornerHarris(grayImage, 2, 3, 0.04)
harrisImage = cv.dilate(harrisImage, None)
设置阈值的代码如下所示
# 设置阈值
thresImage = 0.006 * harrisImage.max()
img[harrisImage > thresImage] = [255, 0, 0]
Susan算子的代码如下所示
'''
Susan算子
'''
def img_extraction(image):
"""
img_extraction 函数利用susan角点检测算法,对图像进行处理
"""
print("最小灰度值,%d" % image.min())
print("最大灰度值,%d" % image.max())
threshold_value = (int(image.max())-int(image.min())) / 10
print("初始阈值为: %d" % threshold_value)
offsetX = [
-1, 0, 1,
-2, -1, 0, 1, 2,
-3, -2, -1, 0, 1, 2, 3,
-3, -2, -1, 0, 1, 2, 3,
-3, -2, -1, 0, 1, 2, 3,
-2, -1, 0, 1, 2,
-1, 0, 1
]
offsetY = [
-3, -3, -3,
-2, -2, -2, -2, -2,
-1, -1, -1, -1, -1, -1, -1,
0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2,
3, 3, 3
]
for i in range(3, image.shape[0] - 3): # 利用圆形模板遍历图像,计算每点处的USAN值
for j in range(3, image.shape[1] - 3):
same = 0
for k in range(0, 37):
if abs(int(image[i + int(offsetY[k]), j + int(offsetX[k]), 0]) - int(image[i, j, 0])) < threshold_value: # 计算相似度
same += 1
if same < 18:
image[i, j, 0] = 18 - same
image[i, j, 1] = 18 - same
image[i, j, 2] = 18 - same
else:
image[i, j, 0] = 0
image[i, j, 1] = 0
image[i, j, 2] = 0
return image
def img_revise(image):
"""
img_revise 函数用于对角点处理后的图像,进行非极大值抑制修正
"""
X = [-1, -1, -1, 0, 0, 1, 1, 1] # X轴偏移
Y = [-1, 0, 1, -1, 1, -1, 0, 1] # Y轴偏移
for i in range(4, image.shape[0]-4):
for j in range(4, image.shape[1]-4):
flag = 0
for k in range(0, 8):
print(i)
if image[i, j, 0] <= image[int(i + X[k]), int(j + Y[k]), 0]:
flag += 1
break
if flag == 0: # 判断是否是周围8个点中最大的值,是则保留
image[i, j, 0] = 255
image[i, j, 1] = 255
image[i, j, 2] = 255
else:
image[i, j, 0] = 0
image[i, j, 1] = 0
image[i, j, 2] = 0
return image
originImage = cv.imread('Lena.jpg')
originImage = cv.cvtColor(originImage, cv.COLOR_BGR2RGB)
图像转换成灰度图像的代码如下所示
# 图像转换成灰度图像
grayImage = np.float32(originImage)
调用susan角点检测算法的代码如下所示
# 调用susan角点检测算法
extraImage = img_extraction(originImage)
调用img_revise()非极大值抑制修正的代码如下所示
# 调用img_revise()非极大值抑制修正
susanImages = img_revise(extraImage)
显示正常中文的标签的代码如下所示
# 显示正常中文的标签
plt.rcParams['font.sans-serif'] = ['SimHei']
titles = [u'(a)原始图像', u'(b)Harris图像', u'(c)Susan图像']
images = [lenna_img, img, susanImages]
for i in range(3):
plt.subplot(1, 3, i + 1), plt.imshow(images[i], 'gray')
plt.title(titles[i])
plt.xticks([]), plt.yticks([])
plt.show()
角点检测运行结果结果如下所示
Susan与Harris角点检测算子的优缺点