首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >使用numpy处理图片——模糊处理

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

作者头像
方亮
发布2024-01-12 08:37:14
发布2024-01-12 08:37:14
1.4K0
举报
文章被收录于专栏:方亮方亮
《使用numpy处理图片——滤镜》一文中,我们尝试了去掉一原色来产生滤镜效果。本文将使用更复杂的算法,来做图像模糊处理。 基本思路还是和前文类似:先切分出各个原色的数组,然后对每个数组用算法进行重新计算,最后把它们堆叠到一起。 区别在于,我们需要把各个原色的数组从3维变成2维。对2维数组进行计算,然后把3个2维数组堆叠出一个3维数组。
代码语言:javascript
复制
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库提供。

代码语言:javascript
复制
import scipy.ndimage as ndimage

最后我们看一眼原图。

高斯模糊

代码语言:javascript
复制
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')

方框模糊

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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')
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-01-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 在《使用numpy处理图片——滤镜》一文中,我们尝试了去掉一原色来产生滤镜效果。本文将使用更复杂的算法,来做图像模糊处理。 基本思路还是和前文类似:先切分出各个原色的数组,然后对每个数组用算法进行重新计算,最后把它们堆叠到一起。 区别在于,我们需要把各个原色的数组从3维变成2维。对2维数组进行计算,然后把3个2维数组堆叠出一个3维数组。
  • 高斯模糊
  • 方框模糊
  • 其他算法
    • median_filter
    • maximum_filter
    • minimum_filter
    • percentile_filter
    • rank_filter
    • gaussian_laplace
    • correlate
    • morphological_laplace
    • white_tophat
    • morphological_gradient
    • black_tophat
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档