Scipy 教程-SciPy 多维图像处理
SciPy 提供了 ndimage(n维图像)包,其中包含许多用于通用图像处理和分析的函数。它专用于图像处理。我们可以执行图像处理中的多种任务,如图像的输入/输出、分类、特征提取、注册等。
打开和写入图像文件
from scipy import miscf = misc.face()misc.imsave('face.jpg', f)import matplotlib.pyplot as pltplt.imshow(f)plt.show()
输出:
👉点击领取:最全Python资料合集
我们可以执行一些基本操作,例如图像旋转、图像上下翻转。考虑以下图像上下翻转的示例:
from scipy import miscface = misc.face()flip_ud_face = np.flipud(face)import matplotlib.pyplot as pltplt.imshow(flip_ud_face)plt.show()
SciPy 提供了 rotate() 函数,它可以将图像旋转到指定角度。
from scipy import misc,ndimageface = misc.face()rotate_face = ndimage.rotate(face, 30) #rotating the image 30 degreeimport matplotlib.pyplot as pltplt.imshow(rotate_face)plt.show()
过滤器
模糊
模糊是一种用于减少图像中噪声的技术。我们可以执行过滤器操作并观察图像的变化。以下是一个示例:
from scipy import miscfrom scipy import ndimageface = misc.face()blurred_image = ndimage.gaussian_filter(face, sigma=4)import matplotlib.pyplot as pltplt.imshow(blurred_face)plt.show()
边缘检测
考虑以下示例:
import scipy.ndimage as ndimport numpy as npim = np.zeros((256, 256))im[64:-64, 64:-64] = 1im[90:-90,90:-90] = 2im = ndimage.gaussian_filter(im, 10)import matplotlib.pyplot as pltplt.imshow(im)plt.show()
输出图像看起来像一个彩色方块。现在,我们将找到这些彩色块的边缘。ndimage 提供了 sobel() 函数来执行此操作。而 NumPy 提供了 hypot() 函数,用于将两个结果矩阵合并为一个。考虑以下示例:
import scipy.ndimage as ndimport matplotlib.pyplot as pltim = np.zeros((256, 256))im[64:-64, 64:-64] = 1im[90:-90,90:-90] = 2im = ndimage.gaussian_filter(im, 8)zx = ndimage.sobel(im, axis = 0, mode = 'constant')zy = ndimage.sobel(im, axis = 1, mode = 'constant')sobl = np.hypot(zx, zy)plt.imshow(sobl)plt.show()
热门推荐