使用scipy处理图片——滤镜处理

发布时间:2024年01月17日

《使用numpy处理图片——模糊处理》一文中,我们介绍了如何使用scipy库进行滤镜处理。本文我们将通过9宫格的形式,展现不同参数时滤镜效果。

black_tophat

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第二个参数)的值

11121
314151
617181

在这里插入图片描述

white_tophat

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第二个参数)的值

11121
314151
617181

在这里插入图片描述

convolve

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第二个参数)的维度是

123
456
789

在这里插入图片描述

correlate

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第二个参数)的维度是

123
456
789

在这里插入图片描述

gaussian_filter

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第二个参数)的值

123
456
789

在这里插入图片描述

gaussian_laplace

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.20.40.6
0.81.01.2
1.41.61.8

在这里插入图片描述

maximum_filter

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第二个参数)的值

123
456
789

在这里插入图片描述

median_filter

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第二个参数)的值

123
456
789

在这里插入图片描述

minimum_filter

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第二个参数)的值

123
456
789

在这里插入图片描述

percentile_filter

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第二、三个参数)的值

123
456
789

在这里插入图片描述

prewitt

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)

在这里插入图片描述

rank_filter

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第二个参数)的值

123
456
789

对应的size(ndimage.rank_filter第三个参数)的值

246
81012
141618

在这里插入图片描述

sobel

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)

在这里插入图片描述

spline_filter

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第二个参数)的值

234

在这里插入图片描述

uniform_filter

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第二个参数)的值

123
456
789

在这里插入图片描述

基础代码

# 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/

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