使用numpy处理图片——模糊处理

大纲

  • 高斯模糊
  • 方框模糊
  • 其他算法
    • median_filter
    • maximum_filter
    • minimum_filter
    • percentile_filter
    • rank_filter
    • gaussian_laplace
    • correlate
    • morphological_laplace
    • white_tophat
    • morphological_gradient
    • black_tophat

在《使用numpy处理图片——滤镜》一文中,我们尝试了去掉一原色来产生滤镜效果。本文将使用更复杂的算法,来做图像模糊处理。
基本思路还是和前文类似:先切分出各个原色的数组,然后对每个数组用算法进行重新计算,最后把它们堆叠到一起。
区别在于,我们需要把各个原色的数组从3维变成2维。对2维数组进行计算,然后把3个2维数组堆叠出一个3维数组。
使用numpy处理图片——模糊处理_第1张图片

import numpy as np
from PIL import Image
import scipy.ndimage as ndimage

img = Image.open('the_starry_night.jpg')
data = np.array(img)

colorDim3List = np.dsplit(data, 3)
red = colorDim3List[0].reshape((data.shape[0], data.shape[1]))
green = colorDim3List[1].reshape((data.shape[0], data.shape[1]))
blue = colorDim3List[2].reshape((data.shape[0], data.shape[1]))

data就是原始图片的3维数组。
使用numpy处理图片——模糊处理_第2张图片
colorDim3List是一个数组,每个元素是一个3维数组。比如colorDim3List[0]就是红色(R)值构成的3维数组。
使用numpy处理图片——模糊处理_第3张图片
colorDim3List[0].reshape((data.shape[0], data.shape[1]))是通过reshape方法,将3维数组重构成2维数组。
使用numpy处理图片——模糊处理_第4张图片
至此,我们准备工作做完了。下面我们将展现各种模糊处理。算法是由scipy库提供。

import scipy.ndimage as ndimage

最后我们看一眼原图。
使用numpy处理图片——模糊处理_第5张图片

高斯模糊

redGaussian = ndimage.gaussian_filter(red, sigma=1.5)
greenGaussian = ndimage.gaussian_filter(green, sigma=1.5)
blueGaussian = ndimage.gaussian_filter(blue, sigma=1.5)

gaussian = np.dstack((redGaussian, greenGaussian, blueGaussian))
gaussianImg = Image.fromarray(gaussian)
gaussianImg.save('gaussian.png')

使用numpy处理图片——模糊处理_第6张图片

方框模糊

redBox = ndimage.uniform_filter(red, size=15)
greenBox = ndimage.uniform_filter(green, size=15)
blueBox = ndimage.uniform_filter(blue, size=15)

box = np.dstack((redBox, greenBox, blueBox))
boxImg = Image.fromarray(box)
boxImg.save('box.png')

使用numpy处理图片——模糊处理_第7张图片

其他算法

median_filter

redMedian = ndimage.median_filter(red, size=15)
greenMedian = ndimage.median_filter(green, size=15)
blueMedian = ndimage.median_filter(blue, size=15)

median = np.dstack((redMedian, greenMedian, blueMedian))
medianImg = Image.fromarray(median)
medianImg.save('median.png')

使用numpy处理图片——模糊处理_第8张图片

maximum_filter

redMaximum = ndimage.maximum_filter(red, size=15)
greenMaximum = ndimage.maximum_filter(green, size=15)
blueMaximum = ndimage.maximum_filter(blue, size=15)

maximum = np.dstack((redMaximum, greenMaximum, blueMaximum))
maximumImg = Image.fromarray(maximum)
maximumImg.save('maximum.png')

使用numpy处理图片——模糊处理_第9张图片

minimum_filter

redMinimum = ndimage.minimum_filter(red, size=15)
greenMinimum = ndimage.minimum_filter(green, size=15)
blueMinimum = ndimage.minimum_filter(blue, size=15)

minimum = np.dstack((redMinimum, greenMinimum, blueMinimum))
minimumImg = Image.fromarray(minimum)
minimumImg.save('minimum.png')

使用numpy处理图片——模糊处理_第10张图片

percentile_filter

redPercentile = ndimage.percentile_filter(red, percentile=50, size=15)
greenPercentile = ndimage.percentile_filter(green, percentile=50, size=15)
bluePercentile = ndimage.percentile_filter(blue, percentile=50, size=15)

percentile = np.dstack((redPercentile, greenPercentile, bluePercentile))
percentileImg = Image.fromarray(percentile)
percentileImg.save('percentile.png')

使用numpy处理图片——模糊处理_第11张图片

rank_filter

redRank = ndimage.rank_filter(red, rank=15, size=15)
greenRank = ndimage.rank_filter(green, rank=15, size=15)
blueRank = ndimage.rank_filter(blue, rank=15, size=15)

rank = np.dstack((redRank, greenRank, blueRank))
rankImg = Image.fromarray(rank)
rankImg.save('rank.png')

使用numpy处理图片——模糊处理_第12张图片

gaussian_laplace

redGaussianLaplace = ndimage.gaussian_laplace(red, sigma=1.5)
greenGaussianLaplace = ndimage.gaussian_laplace(green, sigma=1.5)
blueGaussianLaplace = ndimage.gaussian_laplace(blue, sigma=1.5)

gaussianLaplace = np.dstack((redGaussianLaplace, greenGaussianLaplace, blueGaussianLaplace))
gaussianLaplaceImg = Image.fromarray(gaussianLaplace)
gaussianLaplaceImg.save('gaussianlaplace.png')

使用numpy处理图片——模糊处理_第13张图片

correlate

redCorrelate = ndimage.correlate(red, weights=np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]]))
greenCorrelate = ndimage.correlate(green, weights=np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]]))
blueCorrelate = ndimage.correlate(blue, weights=np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]]))

correlate = np.dstack((redCorrelate, greenCorrelate, blueCorrelate))
correlateImg = Image.fromarray(correlate)
correlateImg.save('correlate.png')

使用numpy处理图片——模糊处理_第14张图片

morphological_laplace

redMorphologicalLaplace = ndimage.morphological_laplace(red, size=15)
greenMorphologicalLaplace = ndimage.morphological_laplace(green, size=15)
blueMorphologicalLaplace = ndimage.morphological_laplace(blue, size=15)

morphologicalLaplace = np.dstack((redMorphologicalLaplace, greenMorphologicalLaplace, blueMorphologicalLaplace))
morphologicalLaplaceImg = Image.fromarray(morphologicalLaplace)
morphologicalLaplaceImg.save('morphologicallaplace.png')

使用numpy处理图片——模糊处理_第15张图片

white_tophat

redWhiteTophat = ndimage.white_tophat(red, size=15)
greenWhiteTophat = ndimage.white_tophat(green, size=15)
blueWhiteTophat = ndimage.white_tophat(blue, size=15)

whiteTophat = np.dstack((redWhiteTophat, greenWhiteTophat, blueWhiteTophat))
whiteTophatImg = Image.fromarray(whiteTophat)
whiteTophatImg.save('whitetophat.png')

使用numpy处理图片——模糊处理_第16张图片

morphological_gradient

redMorphologicalGradient = ndimage.morphological_gradient(red, size=15)
greenMorphologicalGradient = ndimage.morphological_gradient(green, size=15)
blueMorphologicalGradient = ndimage.morphological_gradient(blue, size=15)

morphologicalGradient = np.dstack((redMorphologicalGradient, greenMorphologicalGradient, blueMorphologicalGradient))
morphologicalGradientImg = Image.fromarray(morphologicalGradient)
morphologicalGradientImg.save('morphologicalgradient.png')

使用numpy处理图片——模糊处理_第17张图片

black_tophat

redBlackTophat = ndimage.black_tophat(red, size=15)
greenBlackTophat = ndimage.black_tophat(green, size=15)
blueBlackTophat = ndimage.black_tophat(blue, size=15)

blackTophat = np.dstack((redBlackTophat, greenBlackTophat, blueBlackTophat))
blackTophatImg = Image.fromarray(blackTophat)
blackTophatImg.save('blacktophat.png')

使用numpy处理图片——模糊处理_第18张图片

你可能感兴趣的:(numpy,numpy)