python_数据_scipy_ndimage

ndimage

start

import numpy as np
import matplotlib.pyplot as plt
from scipy import ndimage

使用中值滤波,高斯滤波处理图片

moon = plt.imread('./moonlanding.png')

python_数据_scipy_ndimage_第1张图片

plt.imshow(moon,cmap='gray')    
'''sigma : scalar or sequence of scalars
    Standard deviation for Gaussian kernel. The standard
    deviations of the Gaussian filter are given for each axis as a
    sequence, or as a single number, in which case it is equal for
    all axes.'''
moon2 = ndimage.gaussian_filter(moon,sigma=1)  # 均值中间填充  可以加权重
  • 高斯滤波gaussian_filter
plt.imshow(moon2,cmap='gray')
# 中位数
moon3 = ndimage.median_filter(moon,size = 5)   # 5 * 5  区域中值填充
  • 中值滤波median_filter
plt.imshow(moon3,cmap='gray')

图片在画布上的移动

cat = ndimage.imread('./cat.jpg')
plt.imshow(cat)

python_数据_scipy_ndimage_第2张图片

cat2 = ndimage.shift(cat,shift=[0,300,0])
plt.imshow(cat2)
  • 将图片向右移动
  • shift 参数一上下移动;参数二左右移动;参数三改变颜色,即,分别对应三维
'''mode : {'reflect', 'constant', 'nearest', 'mirror', 'wrap'}, optional
    The `mode` parameter determines how the input array is extended
    when the filter overlaps a border. Default is 'reflect'. Behavior
    for each valid value is as follows:

    'reflect' (`d c b a | a b c d | d c b a`)
        The input is extended by reflecting about the edge of the last
        pixel.

    'constant' (`k k k k | a b c d | k k k k`)
        The input is extended by filling all values beyond the edge with
        the same constant value, defined by the `cval` parameter.

    'nearest' (`a a a a | a b c d | d d d d`)
        The input is extended by replicating the last pixel.

    'mirror' (`d c b | a b c d | c b a`)
        The input is extended by reflecting about the center of the last
        pixel.

    'wrap' (`a b c d | a b c d | a b c d`)
        The input is extended by wrapping around to the opposite edge.'''
cat2 = ndimage.shift(cat,shift=[0,360,0],mode='reflect')
plt.imshow(cat2)
  • 通过mode参数实现对空白区域的操作,如镜像效果
cat3 = ndimage.rotate(cat,60)
plt.imshow(cat3)
  • rotate 可以实现图片的旋转
cat3 = ndimage.zoom(cat,zoom=[0.5,1.5,1])
plt.imshow(cat3)
  • 改变图片的大小 是通过比例实现

你可能感兴趣的:(ndimage,scipy)