Scipy 简明教程

SciPy - Ndimage

SciPy ndimage 子模块专用于图像处理。此处,ndimage 表示 n 维图像。

图像处理中最常见的一些任务如下:

  1. Input/Output, displaying images

  2. 基本操作 − 裁剪、翻转、旋转等。

  3. 图像滤波 − 去噪声、锐化等。

  4. 图像分割 − 为对应不同对象的像素创建标签

  5. Classification

  6. Feature extraction

  7. Registration

我们来讨论如何使用 SciPy 完成其中一些操作。

Opening and Writing to Image Files

SciPy 中的 misc package 带有一些图像。我们使用那些图像来学习图像处理。我们不妨考虑以下示例。

from scipy import misc
f = misc.face()
misc.imsave('face.png', f) # uses the Image module (PIL)

import matplotlib.pyplot as plt
plt.imshow(f)
plt.show()

上述程序将生成以下输出。

opening and writing to image files

任何原始格式的图像都是由以矩阵格式表示的数字表示的颜色组合而成的。机器仅基于这些数字来理解和处理图像。RGB 是流行的表示方法。

让我们看看上图的统计信息。

from scipy import misc
face = misc.face(gray = False)
print face.mean(), face.max(), face.min()

上述程序将生成以下输出。

110.16274388631184, 255, 0

现在,我们知道该图像由数字构成,因此数字值中的任何变化都会改变原始图像。让我们对该图像执行一些几何变换。基本几何操作为裁剪。

from scipy import misc
face = misc.face(gray = True)
lx, ly = face.shape
# Cropping
crop_face = face[lx / 4: - lx / 4, ly / 4: - ly / 4]
import matplotlib.pyplot as plt
plt.imshow(crop_face)
plt.show()

上述程序将生成以下输出。

cropping operation image files

我们还可以执行一些基本操作,例如按照以下说明将图像倒置。

# up <-> down flip
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()

上述程序将生成以下输出。

image turning operation

除此之外,我们还有 rotate() function ,它会根据指定的角度旋转图像。

# rotation
from scipy import misc,ndimage
face = misc.face()
rotate_face = ndimage.rotate(face, 45)

import matplotlib.pyplot as plt
plt.imshow(rotate_face)
plt.show()

上述程序将生成以下输出。

image rotation operation

Filters

让我们讨论过滤器如何帮助进行图像处理。

What is filtering in image processing?

过滤是一种修改或增强图像的技术。例如,你可以过滤图像以强调某些功能或删除其他功能。使用过滤实施的图像处理操作包括平滑、锐化和边缘增强。

过滤是一种邻域操作,其中输出图像中任何给定像素的值由应用于对应输入像素邻域中像素值的某种算法来确定。我们现在使用 SciPy ndimage 执行一些操作。

Blurring

模糊被广泛用于降低图像中的噪声。我们可以执行滤波操作并查看图像中的变化。我们不妨考虑以下示例。

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

上述程序将生成以下输出。

image blurring operation

西格玛值表示在五点量表上的模糊级别。我们可以通过调整西格玛值来查看图像质量的变化。有关模糊的更多详细信息,请点击 → DIP(数字图像处理)教程。

Edge Detection

让我们讨论边缘检测如何帮助进行图像处理。

What is Edge Detection?

边缘检测是一种图像处理技术,用于找到图像内对象的边界。它的工作原理是检测亮度的曲折。边缘检测用于图像处理、计算机视觉和机器视觉等领域中的图像分割和数据提取。

最常用的边缘检测算法包括

  1. Sobel

  2. Canny

  3. Prewitt

  4. Roberts

  5. Fuzzy Logic methods

让我们考虑以下示例。

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, 8)

import matplotlib.pyplot as plt
plt.imshow(im)
plt.show()

上述程序将生成以下输出。

edge detection

图像看起来像一个方块的颜色块。现在,我们将检测这些彩色块的边缘。此处,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)

sx = ndimage.sobel(im, axis = 0, mode = 'constant')
sy = ndimage.sobel(im, axis = 1, mode = 'constant')
sob = np.hypot(sx, sy)

plt.imshow(sob)
plt.show()

上述程序将生成以下输出。

edge detection 2