个性化阅读
专注于IT技术分析

SciPy图像处理:ndimage模块的详细使用图解

点击下载

SciPy提供了ndimage(n维图像)包, 其中包含许多常规图像处理和分析功能。它专用于图像处理。我们可以在图像处理中执行多项任务, 例如输入/输出图像, 分类, 特征提取, 配准等。

打开和写入图像文件

scipy.ndimage提供了misc包, 其中包含一些图像。我们将使用这些图像并执行图像操作。考虑以下示例:

from scipy import misc
f = misc.face()
misc.imsave('face.jpg', f)
import matplotlib.pyplot as plt
plt.imshow(f)
plt.show()

输出

SciPy ndimage

矩阵格式的数字代表任何图像及其颜色组合。机器使用这些数字进行操作。有两种表示图像的方式, 即灰度和RGB。 RGB是最流行的表示方式。

我们可以执行一些基本操作, 例如图像旋转, 图像上下颠倒。考虑以下图像颠倒的示例:

from scipy import misc
face = misc.face()
flip_ud_face = np.flipud(face)
import matplotlib.pyplot as plt
plt.imshow(flip_ud_face)
plt.show()

输出

SciPy ndimage

SciPy提供了rotate()函数, 该函数将图像旋转到指定角度。

from scipy import misc, ndimage
face = misc.face()
rotate_face = ndimage.rotate(face, 30) #rotating the image 30 degree
import matplotlib.pyplot as plt
plt.imshow(rotate_face)
plt.show()

输出

SciPy ndimage

筛选器

过滤是我们修改和增强图像的过程。例如, 可以将滤镜应用于图像以突出显示某些功能或消除其他功能。通过滤波实现的图像处理操作包括平滑和边缘增强。考虑使用SciPy ndimage进行以下操作。

  • 模糊化

模糊是用于减少图像噪声的技术。我们可以执行滤镜操作并观察图像的变化。示例如下:

from scipy import misc
from scipy import ndimage
face = misc.face()
blurred_image = ndimage.gaussian_filter(face, sigma=4)
import matplotlib.pyplot as plt
plt.imshow(blurred_face)
plt.show()

输出

SciPy ndimage

sigma值表示5级模糊程度。你可以更改sigma值并查看差异。

  • 边缘检测

边缘检测是一种图像处理术语, 用于查找图像内对象的边界。它用于图像处理, 计算机视觉和机器视觉等领域中的图像分割和数据提取。要更具体地了解边缘检测, 请访问我们的教程-单击此处。

考虑以下示例:

import scipy.ndimage as nd
import numpy as np
im = np.zeros((256, 256))
im[64:-64, 64:-64] = 1
im[90:-90, 90:-90] = 2
im = ndimage.gaussian_filter(im, 10)
import matplotlib.pyplot as plt
plt.imshow(im)
plt.show()

输出

SciPy ndimage

输出图像看起来像一个正方形的颜色块。现在, 我们将找到这些彩色块的边缘。 ndimage提供sobel()函数来执行此操作。而NumPy提供了hypot()函数, 该函数用于将两个结果矩阵组合为一个。考虑以下示例:

import scipy.ndimage as nd
import matplotlib.pyplot as plt
im = np.zeros((256, 256))
im[64:-64, 64:-64] = 1
im[90:-90, 90:-90] = 2
im = 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()

输出

SciPy ndimage

赞(0)
未经允许不得转载:srcmini » SciPy图像处理:ndimage模块的详细使用图解

评论 抢沙发

评论前必须登录!