机器学习与计算机视觉实战OpenCV与深度学习

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

2018-01-26  本文已影响16人  冰不语

引言

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

这里写图片描述

还有这样

这里写图片描述

这样

这里写图片描述

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

程序要点

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

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

效果图

程序的效果显然比不了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的内容。
这里写图片描述
上一篇 下一篇

猜你喜欢

热点阅读