Scipy 简明教程

SciPy - Ndimage

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

The SciPy ndimage submodule is dedicated to image processing. Here, ndimage means an n-dimensional image.

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

Some of the most common tasks in image processing are as follows &miuns;

  1. Input/Output, displaying images

  2. Basic manipulations − Cropping, flipping, rotating, etc.

  3. Image filtering − De-noising, sharpening, etc.

  4. Image segmentation − Labeling pixels corresponding to different objects

  5. Classification

  6. Feature extraction

  7. Registration

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

Let us discuss how some of these can be achieved using SciPy.

Opening and Writing to Image Files

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

The misc package in SciPy comes with some images. We use those images to learn the image manipulations. Let us consider the following example.

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()

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

The above program will generate the following output.

opening and writing to image files

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

Any images in its raw format is the combination of colors represented by the numbers in the matrix format. A machine understands and manipulates the images based on those numbers only. RGB is a popular way of representation.

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

Let us see the statistical information of the above image.

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

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

The above program will generate the following output.

110.16274388631184, 255, 0

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

Now, we know that the image is made out of numbers, so any change in the value of the number alters the original image. Let us perform some geometric transformations on the image. The basic geometric operation is cropping

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()

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

The above program will generate the following output.

cropping operation image files

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

We can also perform some basic operations such as turning the image upside down as described below.

# 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()

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

The above program will generate the following output.

image turning operation

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

Besides this, we have the rotate() function, which rotates the image with a specified angle.

# 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()

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

The above program will generate the following output.

image rotation operation

Filters

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

Let us discuss how filters help in image processing.

What is filtering in image processing?

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

Filtering is a technique for modifying or enhancing an image. For example, you can filter an image to emphasize certain features or remove other features. Image processing operations implemented with filtering include Smoothing, Sharpening, and Edge Enhancement.

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

Filtering is a neighborhood operation, in which the value of any given pixel in the output image is determined by applying some algorithm to the values of the pixels in the neighborhood of the corresponding input pixel. Let us now perform a few operations using SciPy ndimage.

Blurring

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

Blurring is widely used to reduce the noise in the image. We can perform a filter operation and see the change in the image. Let us consider the following example.

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()

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

The above program will generate the following output.

image blurring operation

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

The sigma value indicates the level of blur on a scale of five. We can see the change on the image quality by tuning the sigma value. For more details of blurring, click on → DIP (Digital Image Processing) Tutorial.

Edge Detection

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

Let us discuss how edge detection helps in image processing.

What is Edge Detection?

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

Edge detection is an image processing technique for finding the boundaries of objects within images. It works by detecting discontinuities in brightness. Edge detection is used for image segmentation and data extraction in areas such as Image Processing, Computer Vision and Machine Vision.

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

The most commonly used edge detection algorithms include

  1. Sobel

  2. Canny

  3. Prewitt

  4. Roberts

  5. Fuzzy Logic methods

让我们考虑以下示例。

Let us consider the following example.

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()

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

The above program will generate the following output.

edge detection

图像看起来像一个方块的颜色块。现在,我们将检测这些彩色块的边缘。此处,ndimage 提供一个名为 Sobel 的函数来执行此操作。而 NumPy 提供 Hypot 函数将得到的两个矩阵组合成一个。

The image looks like a square block of colors. Now, we will detect the edges of those colored blocks. Here, ndimage provides a function called Sobel to carry out this operation. Whereas, NumPy provides the Hypot function to combine the two resultant matrices to one.

让我们考虑以下示例。

Let us consider the following example.

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()

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

The above program will generate the following output.

edge detection 2