在《使用numpy处理图片——模糊处理》一文中,我们介绍了如何使用scipy库进行滤镜处理。本文我们将通过9宫格的形式,展现不同参数时滤镜效果。
import sys
sys.path.append("..")
from frame import *
import scipy.ndimage as ndimage
generate('lena.png', 'black_tophat.png', ndimage.black_tophat, 1, 91, 10)
对应的size(ndimage.black_tophat第二个参数)的值
1 | 11 | 21 |
---|---|---|
31 | 41 | 51 |
61 | 71 | 81 |
import sys
sys.path.append("..")
from frame import *
import scipy.ndimage as ndimage
def func(*args):
return ndimage.white_tophat(args[0], args[1])
generate('lena.png', 'white_tophat.png', func, 1, 91, 10)
对应的size(ndimage.white_tophat第二个参数)的值
1 | 11 | 21 |
---|---|---|
31 | 41 | 51 |
61 | 71 | 81 |
import sys
sys.path.append("..")
from frame import *
import scipy.ndimage as ndimage
def func(*args):
weights = np.eye(args[1])
return ndimage.convolve(args[0], weights)
generate('lena.png', 'convolve.png', func, 1, 10, 1)
对应的weights(ndimage.convolve第二个参数)的维度是
1 | 2 | 3 |
---|---|---|
4 | 5 | 6 |
7 | 8 | 9 |
import sys
sys.path.append("..")
from frame import *
import scipy.ndimage as ndimage
def func(*args):
weights = np.eye(args[1])
return ndimage.correlate(args[0], weights)
generate('lena.png', 'correlate.png', func, 1, 10, 1)
对应的weights(ndimage.correlate第二个参数)的维度是
1 | 2 | 3 |
---|---|---|
4 | 5 | 6 |
7 | 8 | 9 |
import sys
sys.path.append("..")
from frame import *
import scipy.ndimage as ndimage
generate('lena.png', 'gaussian_filter.png', ndimage.gaussian_filter, 1, 10, 1)
对应的sigma(ndimage.gaussian_filter第二个参数)的值
1 | 2 | 3 |
---|---|---|
4 | 5 | 6 |
7 | 8 | 9 |
import sys
sys.path.append("..")
from frame import *
import scipy.ndimage as ndimage
generate('lena.png', 'gaussian_laplace.png', ndimage.gaussian_laplace, 0.2, 1.9, 0.2)
对应的sigma(ndimage.black_tophat第二个参数)的值
0.2 | 0.4 | 0.6 |
---|---|---|
0.8 | 1.0 | 1.2 |
1.4 | 1.6 | 1.8 |
import sys
sys.path.append("..")
from frame import *
import scipy.ndimage as ndimage
generate('lena.png', 'maximum_filter.png', ndimage.maximum_filter, 1, 10, 1)
对应的size(ndimage.maximum_filter第二个参数)的值
1 | 2 | 3 |
---|---|---|
4 | 5 | 6 |
7 | 8 | 9 |
import sys
sys.path.append("..")
from frame import *
import scipy.ndimage as ndimage
generate('lena.png', 'median_filter.png', ndimage.median_filter, 1, 10, 1)
对应的size(ndimage.median_filter第二个参数)的值
1 | 2 | 3 |
---|---|---|
4 | 5 | 6 |
7 | 8 | 9 |
import sys
sys.path.append("..")
from frame import *
import scipy.ndimage as ndimage
generate('lena.png', 'minimum_filter.png', ndimage.minimum_filter, 1, 10, 1)
对应的size(ndimage.minimum_filter第二个参数)的值
1 | 2 | 3 |
---|---|---|
4 | 5 | 6 |
7 | 8 | 9 |
import sys
sys.path.append("..")
from frame import *
import scipy.ndimage as ndimage
def func(*args):
return ndimage.percentile_filter(args[0], percentile=args[1], size=args[1])
generate('lena.png', 'percentile_filter.png', func, 1, 10, 1)
对应的percentile和size(ndimage.percentile_filter第二、三个参数)的值
1 | 2 | 3 |
---|---|---|
4 | 5 | 6 |
7 | 8 | 9 |
import sys
sys.path.append("..")
from frame import *
import scipy.ndimage as ndimage
def func(*args):
return ndimage.prewitt(args[0])
generate('lena.png', 'prewitt.png', func, 1, 2, 1)
import sys
sys.path.append("..")
from frame import *
import scipy.ndimage as ndimage
def func(*args):
return ndimage.rank_filter(args[0], rank=args[1], size=args[1]*2)
generate('lena.png', 'rank_filter.png', func, 1, 10, 1)
对应的rank(ndimage.rank_filter第二个参数)的值
1 | 2 | 3 |
---|---|---|
4 | 5 | 6 |
7 | 8 | 9 |
对应的size(ndimage.rank_filter第三个参数)的值
2 | 4 | 6 |
---|---|---|
8 | 10 | 12 |
14 | 16 | 18 |
import sys
sys.path.append("..")
from frame import *
import scipy.ndimage as ndimage
def func(*args):
return ndimage.sobel(args[0])
generate('lena.png', 'sobel.png', func, 1, 2, 1)
import sys
sys.path.append("..")
from frame import *
import scipy.ndimage as ndimage
def func(*args):
return ndimage.spline_filter(args[0], args[1]).astype(np.uint8)
generate('lena.png', 'spline_filter.png', func, 2, 5, 1)
对应的size(ndimage.black_tophat第二个参数)的值
2 | 3 | 4 |
---|
import sys
sys.path.append("..")
from frame import *
import scipy.ndimage as ndimage
def func(*args):
return ndimage.uniform_filter(args[0], args[1])
generate('lena.png', 'uniform_filter.png', func, 1, 10, 1)
对应的size(ndimage.uniform_filter第二个参数)的值
1 | 2 | 3 |
---|---|---|
4 | 5 | 6 |
7 | 8 | 9 |
# frame.py
import numpy as np
from PIL import Image
import scipy.ndimage as ndimage
def generate(image_from, image_to, filter, start = 1, end = 10, step = 1):
source = np.array(Image.open(image_from))
colorDim3List = np.dsplit(source, 3)
red = colorDim3List[0].reshape(source.shape[0], source.shape[1])
green = colorDim3List[1].reshape(source.shape[0], source.shape[1])
blue = colorDim3List[2].reshape(source.shape[0], source.shape[1])
def inline_filter(red, green, blue, some_value):
redFilter = filter(red, some_value)
greenFilter = filter(green, some_value)
blueFilter = filter(blue, some_value)
return np.dstack((redFilter, greenFilter, blueFilter))
varrays = []
harrays = []
hindex = 0
for i in np.arange(start, end, step):
filter3D = inline_filter(red, green, blue, i)
harrays.append(filter3D)
hindex += 1
if hindex % 3 == 0:
varrays.append(np.hstack(harrays))
harrays = []
hindex = 0
if varrays == []:
varrays.append(np.hstack(harrays))
full3D = np.vstack(varrays)
Image.fromarray(full3D).save(image_to)
https://github.com/f304646673/scipy-ndimage-example/tree/main/