Lesson 8 homework assignments

Saw this paper and wanted to try it out:

The histogram matching thing didn’t work that well for me, but luminance only style transfer worked pretty well.

import os
from scipy.misc import imread, imsave, imresize
from skimage.color import luv2rgb, rgb2luv

def save_luminance(fp, suffix='lum', img_format='png'):
    # save luminance from image, need to do this for style and content
    # then use style transfer on luminance images
    op = '{}_{}.{}'.format(os.path.splitext(fp)[0], suffix, img_format)
    img = imread(fp)
    lum = rgb2luv(img)[...,0]
    imsave(op, lum, format=img_format)

def combine_luminance(a, fp):
    # add uv from original file to output of luminance style transfer
    if a.ndim > 2:
        # grayscale
        a = a.mean(axis=-1)
    assert a.ndim == 2, 'Can only accept 2D or 3D data.'
    img = imresize(imread(fp), (a.shape[0], a.shape[1], 3))
    luv = rgb2luv(img)
    
    # need to rescale
    
    mean_a = a.mean()
    std_a = a.std()
    mean_lum = luv[...,0].mean()
    std_lum = luv[...,0].std()
    adjusted_a = (a - mean_a)*(std_lum/std_a) + mean_lum
    
    luv[...,0] = adjusted_a
    return luv2rgb(luv)

starry night style with luminance only style transfer:

1 Like