Python图像处理七:图像空域锐化

一、图像边缘检测

微分算子,可以用来检测边缘和特征提取。skimage 库中通过 filters 模块进
行滤波操作。

1、Roberts 算子

Roberts 算子用于检测边缘,

调用格式:
edges=filters.roberts(image)
也可使用 Roberts 的十字交叉核来进行过滤,以达到检测交叉边缘的目的。
正对角线差分算子对应的函数为:roberts_pos_diag(image)
负对角线差分算子对应的函数为:roberts_neg_diag(image)

例 1:读入一幅数字图像,用 Roberts 算子对图像边缘检测,代码示例如下:

from skimage import data,filters,io 
import matplotlib.pyplot as plt 
from matplotlib.font_manager import FontProperties 
font_set=FontProperties(fname=r"c:\windows\fonts\simsun.ttc",size=12)
img=data.camera()
dst_neg=filters.roberts_neg_diag(img) 
dst_pos=filters.roberts_pos_diag(img) 
dst=filters.roberts(img)
plt.figure('filters',figsize=(8,8))
plt.subplot(221)
plt.title('原图像',fontproperties=font_set) 
plt.imshow(img,plt.cm.gray)
plt.subplot(222) 
plt.title('负对角线算子',fontproperties=font_set) 
plt.imshow(dst_neg,plt.cm.gray)
plt.subplot(223) 
plt.title('正对角线算子',fontproperties=font_set) 
plt.imshow(dst_pos,plt.cm.gray)
plt.subplot(224) 
plt.title('Robers 算子',fontproperties=font_set) 
plt.imshow(dst,plt.cm.gray)
plt.show()

输出:

2、Sobel 算子

sobel 算子函数调用格式为:

edges=filters.sobel(image)
水平边缘检测算子对应的函数为:sobel_h(image)
垂直边缘检测算子对应的函数为:sobel_v(image)

例 2:读入一幅数字图像,用 Sobel 算子对图像边缘检测,代码示例如下:

from skimage import data,filters,io 
import matplotlib.pyplot as plt 
from matplotlib.font_manager import FontProperties 
font_set=FontProperties(fname=r"c:\windows\fonts\simsun.ttc",size=12)
img=data.camera()
img_h=filters.sobel_h(img) 
img_v=filters.sobel_v(img) 
img_sobel=filters.sobel(img)
plt.figure('filters',figsize=(8,8))
plt.subplot(221) 
plt.title('原图像',fontproperties=font_set) 
plt.imshow(img,plt.cm.gray)
plt.subplot(222) 
plt.title('水平边缘',fontproperties=font_set) 
plt.imshow(img_h,plt.cm.gray)
plt.subplot(223) 
plt.title('垂直边缘',fontproperties=font_set) 
plt.imshow(img_v,plt.cm.gray)
plt.subplot(224) 
plt.title('Sobel 算子',fontproperties=font_set) 
plt.imshow(img_sobel,plt.cm.gray)
plt.show()

输出:

3、Prewitt 算子

功能同 sobel,

调用格式:
edges=filters.prewitt(image)
水平边缘检测算子对应的函数为:prewitt_h(image)
垂直边缘检测算子对应的函数为:prewitt_v(image)

例 3:读入一幅数字图像,用 Prewitt 算子对图像边缘检测,代码示例如下:

from skimage import data,filters,io 
import matplotlib.pyplot as plt 
from matplotlib.font_manager import FontProperties 
font_set=FontProperties(fname=r"c:\windows\fonts\simsun.ttc",size=12)
img=data.camera()
img_h=filters.prewitt_h(img) 
img_v=filters.prewitt_v(img) 
img_prewitt=filters.prewitt(img)
plt.figure('filters',figsize=(8,8))
plt.subplot(221) 
plt.title('原图像',fontproperties=font_set) 
plt.imshow(img,plt.cm.gray)
plt.subplot(222) 
plt.title('水平边缘',fontproperties=font_set) 
plt.imshow(img_h,plt.cm.gray)
plt.subplot(223) 
plt.title('垂直边缘',fontproperties=font_set)
plt.imshow(img_v,plt.cm.gray)
plt.subplot(224) 
plt.title('Prewitt 算子',fontproperties=font_set) 
plt.imshow(img_prewitt,plt.cm.gray)
plt.show()

输出:

4、Laplacian 算子

函数调用格式:
edges=filters.laplace(image)

例 4:读入一幅数字图像,用 Laplacian 算子对图像边缘检测,代码示例如下:

from skimage import data,filters,io 
import matplotlib.pyplot as plt 
from matplotlib.font_manager import FontProperties 
font_set=FontProperties(fname=r"c:\windows\fonts\simsun.ttc",size=12)
img=data.camera()
img_laplace=filters.laplace(img)
plt.figure('filters',figsize=(8,8))
plt.subplot(121) 
plt.title('原图像',fontproperties=font_set) 
plt.imshow(img,plt.cm.gray)
plt.subplot(122) 
plt.title('Laplacian 算子',fontproperties=font_set) 
plt.imshow(img_laplace,plt.cm.gray)
plt.show()

输出:

5、LoG 算子

例 5:读入一幅数字图像,用 LoG 算子对图像边缘检测,代码示例如下:

from skimage import data,filters,io 
import matplotlib.pyplot as plt 
from matplotlib.font_manager import FontProperties 
font_set=FontProperties(fname=r"c:\windows\fonts\simsun.ttc",size=12)
img=data.camera()
img_gauss=filters.gaussian(img,sigma=2) 
img_laplace=filters.laplace(img) 
img_log=filters.laplace(img_gauss)
plt.figure('filters',figsize=(8,8))
plt.subplot(221) 
plt.title('原图像',fontproperties=font_set) 
plt.imshow(img,plt.cm.gray)
plt.subplot(222) 
plt.title('Laplacian 算子',fontproperties=font_set) 
plt.imshow(img_laplace,plt.cm.gray)
plt.subplot(223)
plt.title('高斯平滑图像',fontproperties=font_set) 
plt.imshow(img_gauss,plt.cm.gray)
plt.subplot(224) 
plt.title('LoG 算子',fontproperties=font_set) 
plt.imshow(img_log,plt.cm.gray)
plt.show()

输出:

6、对比

from skimage import data,filters,io 
import matplotlib.pyplot as plt 
from matplotlib.font_manager import FontProperties 
font_set=FontProperties(fname=r"c:\windows\fonts\simsun.ttc",size=12)
img=data.camera()
img_gauss=filters.gaussian(img,sigma=2) 
img_laplace=filters.laplace(img) 
img_log=filters.laplace(img_gauss)
img_sobel=filters.sobel(img)
img_prewitt=filters.prewitt(img)
img_Robers=filters.roberts(img)
plt.figure('filters',figsize=(14,14))
plt.subplot(321) 
plt.title('原图像',fontproperties=font_set) 
plt.imshow(img,plt.cm.gray)
plt.subplot(322) 
plt.title('Laplacian 算子',fontproperties=font_set) 
plt.imshow(img_laplace,plt.cm.gray)
plt.subplot(323)
plt.title('Prewitt 算子',fontproperties=font_set) 
plt.imshow(img_prewitt,plt.cm.gray)
plt.subplot(324) 
plt.title('LoG 算子',fontproperties=font_set) 
plt.imshow(img_log,plt.cm.gray)
plt.subplot(325) 
plt.title('Robers 算子',fontproperties=font_set) 
plt.imshow(img_Robers,plt.cm.gray)
plt.subplot(326) 
plt.title('Sobel 算子',fontproperties=font_set) 
plt.imshow(img_sobel,plt.cm.gray)
plt.show()

输出:

二、图像空域锐化

由于锐化图像等于原图像加上加重的边缘,只需把原图像与检测出的边缘图像相加即可。

例 6:读入一幅数字图像,用 Laplacian 算子对图像做锐化,代码示例如下:

from skimage import data,filters,io 
import matplotlib.pyplot as plt 
from matplotlib.font_manager import FontProperties 
font_set=FontProperties(fname=r"c:\windows\fonts\simsun.ttc",size=12)
img=data.camera()
img_laplace=filters.laplace(img)
img_1=img+2*img_laplace
plt.figure('filters',figsize=(8,8))
plt.subplot(131) 
plt.title('原图像',fontproperties=font_set)
plt.imshow(img,plt.cm.gray)
plt.subplot(132) 
plt.title('Laplacian 算子',fontproperties=font_set) 
plt.imshow(img_laplace,plt.cm.gray)
plt.subplot(133) 
plt.title('锐化图像',fontproperties=font_set) 
plt.imshow(img_1,plt.cm.gray)
plt.show()

输出:

每日“大饼”:
把脸一直向着阳光 这样就不会看到阴影

作者:氿 柒原文地址:https://blog.csdn.net/weixin_52051554/article/details/127958519

%s 个评论

要回复文章请先登录注册