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

发布时间:2024年01月11日

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

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维数组。
在这里插入图片描述
colorDim3List是一个数组,每个元素是一个3维数组。比如colorDim3List[0]就是红色(R)值构成的3维数组。
在这里插入图片描述
colorDim3List[0].reshape((data.shape[0], data.shape[1]))是通过reshape方法,将3维数组重构成2维数组。
在这里插入图片描述
至此,我们准备工作做完了。下面我们将展现各种模糊处理。算法是由scipy库提供。

import scipy.ndimage as ndimage

最后我们看一眼原图。
在这里插入图片描述

高斯模糊

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

在这里插入图片描述

方框模糊

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

在这里插入图片描述

其他算法

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

在这里插入图片描述

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

在这里插入图片描述

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

在这里插入图片描述

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

在这里插入图片描述

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

在这里插入图片描述

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

在这里插入图片描述

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

在这里插入图片描述

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

在这里插入图片描述

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

在这里插入图片描述

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

在这里插入图片描述

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

在这里插入图片描述

文章来源:https://blog.csdn.net/breaksoftware/article/details/135512428
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。