import cv2 as cv
import matplotlib.pyplot as plt
# 统一图片路径
img_url = './image/horse.jpg'
# Sober边缘检测
# 1读取图像
img = cv.imread(img_url, 0)
# 2计算Sobel卷积结果
x = cv.Sobel(img, cv.CV_16S, 1, 0)
y = cv.Sobel(img, cv.CV_16S, 0, 1)
# 3将数据进行转换
Scale_absX = cv.convertScaleAbs(x) # convert转换scale缩放
Scale_absY = cv.convertScaleAbs(y)
# 4结果合成
result = cv.addWeighted(Scale_absX, 0.5, Scale_absY, 0.5, 0)
# 5图像显示
plt.rc("font", family='Microsoft YaHei')
plt.figure(figsize=(10, 8), dpi=100)
plt.subplot(121), plt.imshow(img, cmap=plt.cm.gray), plt.title('原图')
plt.xticks([]), plt.yticks([])
plt.subplot(122), plt.imshow(result, cmap=plt.cm.gray), plt.title('Sobel滤波后结果')
plt.xticks([]), plt.yticks([])
plt.show()
# Scharr边缘检测
# 1读取图像
img = cv.imread(img_url, 0)
# 2计算Sobe1卷积结果
x = cv.Sobel(img, cv.CV_16S, 1, 0, ksize=-1)
y = cv.Sobel(img, cv.CV_16S, 0, 1, ksize=-1)
# 3将数据进行转换
Scale_absx = cv.convertScaleAbs(x) # convert转换scale缩放
Scale_absY = cv.convertScaleAbs(y)
# 4结果合成
result = cv.addWeighted(Scale_absX, 0.5, Scale_absY, 0.5, 0)
# 5图像显示
plt.figure(figsize=(10, 8), dpi=100)
plt.subplot(121), plt.imshow(img, cmap=plt.cm.gray), plt.title('原图')
plt.xticks([]), plt.yticks([])
plt.subplot(122), plt.imshow(result, cmap=plt.cm.gray), plt.title('Scharri滤波后结果')
plt.xticks([]), plt.yticks([])
plt.show()
# Laplacian边缘检测
# 1读取图像
img = cv.imread(img_url, 0)
# 2 laplacian转换
result = cv.Laplacian(img, cv.CV_16S)
Scale_abs = cv.convertScaleAbs(result)
# 3图像展示
plt.figure(figsize=(10, 8), dpi=100)
plt.subplot(121), plt.imshow(img, cmap=plt.cm.gray), plt.title('原图')
plt.xticks([]), plt.yticks([])
plt.subplot(122), plt.imshow(Scale_abs, cmap=plt.cm.gray), plt.title('Laplacian检测结果')
plt.xticks([]), plt.yticks([])
plt.show()
# Canny边缘检测
# 1图像读取
img = cv.imread(img_url, 0)
# 2 Cannyi边缘检测
lowThreshold = 0
max_lowThreshold = 100
canny = cv.Canny(img, lowThreshold, max_lowThreshold)
# 3图像展示
plt.figure(figsize=(10, 8), dpi=100)
plt.subplot(121), plt.imshow(img, cmap=plt.cm.gray), plt.title('原图')
plt.xticks([]), plt.yticks([])
plt.subplot(122), plt.imshow(canny, cmap=plt.cm.gray), plt.title('Canny检测后结果')
plt.xticks(), plt.yticks()
plt.show()
THE END
暂无评论内容