魔幻光影滤镜(2):仿Redfield Fractalius滤镜

55 篇文章 205 订阅
43 篇文章 1 订阅

引言

熟悉PS的人也许知道,PS滤镜中有一个Redfield Fractalius,可以让图片展现出魔幻的效果,比如一直可爱的喵星人,经过魔幻光影滤镜处理之后就会展现出下面威武霸气的样子:

这里写图片描述

还有这样

这里写图片描述

这样

这里写图片描述

那么我们能不能用OpenCV来实现这样的效果呢?答案显然是可以的。而且OpenCV还提供了相应的例子,这就大大简化了我们学习的负担。今天我们就来看看这个程序吧。

程序要点

这是OpenCV的自带例程,代码可以在如下路径找到:\opencv\sources\samples\python\gabor_threads.py

所以代码放在最后,我对代码加了部分注释。先说下代码中比较重要的几点。代码里需要注意的有三点,分别是定义的三个函数:

  • build_filters():代码如下:

    
    # 创建滤波器(们)
    
    def build_filters():
      filters = []
      ksize = 31
      # 此处创建16个滤波器,只有getGaborKernel的第三个参数theta不同。
      for theta in np.arange(0, np.pi, np.pi / 16):
          kern = cv.getGaborKernel((ksize, ksize), 4.0, theta, 10.0, 0.5, 0, ktype=cv.CV_32F)
          kern /= 1.5*kern.sum()
          filters.append(kern)
      return filters

    注意到filters后面加s了,即滤波器不止一个。实际上,这个函数通过调用函数getGaborKernel()一共创建了16个Gabor滤波器,不同之处只在于getGaborKernel()函数的第三个参数theta不同。

  • process():单线程处理函数。这个函数也表明了魔幻滤镜效果的处理过程:

    
    # 单线程处理
    
    def process(img, filters):
      # zeros_like:返回和输入大小相同,类型相同,用0填满的数组
      accum = np.zeros_like(img)
      for kern in filters:
          fimg = cv.filter2D(img, cv.CV_8UC3, kern)
          # maximum:逐位比较取其大
          np.maximum(accum, fimg, accum)
      return accum

    总结一下,魔幻滤镜效果可以分为以下几步:

    1. 创建多个Gabor滤波器
    2. 创建一个与原图大小相同的0值矩阵accum
    3. 分别用不同的滤波器对原图进行滤波
    4. 滤波后的图像与accum逐位比较取其大者,再复制给accum
    5. 重复3、4两步。
  • process_threaded():多线程处理函数。可以通过这个函数大致学习下Python中多线程的用法:

    
    # 多线程处理,threadn = 8
    
    def process_threaded(img, filters, threadn = 8):
      accum = np.zeros_like(img)
      def f(kern):
          return cv.filter2D(img, cv.CV_8UC3, kern)
      pool = ThreadPool(processes=threadn)
      for fimg in pool.imap_unordered(f, filters):
          np.maximum(accum, fimg, accum)
      return accum

    其中pool = ThreadPool(processes=threadn)即建立一个八个线程的线程池。后面的for循环两句:

      for fimg in pool.imap_unordered(f, filters):
          np.maximum(accum, fimg, accum)

    pool.imap_unordered(f, filters)即把filters作为参数迭代地传递给函数f并执行。imap_unordered的意思是,不保证返回结果与迭代传入的顺序一致。不过对于这个魔幻滤镜的处理过程来说,顺序不重要。

效果图

程序的效果显然比不了PS各种调整之后的效果。我们调整合适的参数看看,虽然有所不及,但是也有了点魔幻的感觉。

这里写图片描述

处理之后

这里写图片描述

再来一张

这里写图片描述

处理之后

这里写图片描述

需要说明的是:

  • 也许不是所有的图都能做出魔幻的效果。
  • 这个处理过程参数众多,有时或者大部分时候需要同时调整几个参数才能达到理想的

注释过的代码

完整的并且被注视过的代码如下:

#!/usr/bin/env python

'''
gabor_threads.py
=========

Sample demonstrates:
- use of multiple Gabor filter convolutions to get Fractalius-like image effect (http://www.redfieldplugins.com/filterFractalius.htm)
- use of python threading to accelerate the computation

Usage
-----
gabor_threads.py [image filename]

'''

# Python 2/3 compatibility
from __future__ import print_function

import numpy as np
import cv2 as cv
from multiprocessing.pool import ThreadPool

# 创建滤波器(们)
def build_filters():
    filters = []
    ksize = 31
    # 此处创建16个滤波器,只有getGaborKernel的第三个参数theta不同。
    for theta in np.arange(0, np.pi, np.pi / 16):
        kern = cv.getGaborKernel((ksize, ksize), 4.0, theta, 10.0, 0.5, 0, ktype=cv.CV_32F)
        kern /= 1.5*kern.sum()
        filters.append(kern)
    return filters

# 单线程处理
def process(img, filters):
    # zeros_like:返回和输入大小相同,类型相同,用0填满的数组
    accum = np.zeros_like(img)
    for kern in filters:
        fimg = cv.filter2D(img, cv.CV_8UC3, kern)
        # maximum:逐位比较取其大
        np.maximum(accum, fimg, accum)
    return accum

# 多线程处理,threadn = 8
def process_threaded(img, filters, threadn = 8):
    accum = np.zeros_like(img)
    def f(kern):
        return cv.filter2D(img, cv.CV_8UC3, kern)
    pool = ThreadPool(processes=threadn)
    for fimg in pool.imap_unordered(f, filters):
        np.maximum(accum, fimg, accum)
    return accum

if __name__ == '__main__':
    import sys
    from common import Timer

    # 输出文件开头由''' '''包含的注释内容
    print(__doc__)

    try:
        img_fn = sys.argv[1]
    except:
        img_fn = 'cat1.jpg'

    img = cv.imread(img_fn)
    # 判断图片是否读取成功
    if img is None:
        print('Failed to load image file:', img_fn)
        sys.exit(1)

    filters = build_filters()

    with Timer('running single-threaded'):
        res1 = process(img, filters)
    with Timer('running multi-threaded'):
        res2 = process_threaded(img, filters)

    print('res1 == res2: ', (res1 == res2).all())
    cv.imshow('img', img)
    cv.imshow('result', res2)
    cv.waitKey()
    cv.destroyAllWindows()

公众号CVPy,分享有意思的,不仅限于OpenCV和Python的内容。

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值