Skip to main content

Implementation of 《Image Smoothing via L0 Gradient Minimization》

Project description

Image Smoothing via L0 Gradient Minimization


Blog Post | Report Bug on Numpy Version | Report Bug on PyOpenCL Version

Table of Contents

About the Project

This repository is the Python implementation of the paper: Image Smoothing via L0 Gradient Minimization

Flower
Rock

Getting Started

Installation

Install from pypi

pip install L0-Smoothing

Setup with source code

# Clone the repository
git clone https://github.com/TsXor/L0-Smoothing.git

# build package
# build.bat is batch script for Windows cmd, and will not work on linux.
# build.bat consist of mostly commands to move files, so it is easy to be rewritten into a bash script.
# However, I don't have a linux machine available now, hope someone can write one and open a PR.
cd builder
./build.bat

# install via pip
cd dist
pip install L0_Smoothing-*-py3-none-any.whl

Usage

Import in your script

from L0_Smoothing import L0_Smoothing, L0_Smoothing_accel

# It is not recommended to use cv2.imread because it easily throws error
# just because you are missing some unimportant parameters and cannot
# read image from path with Chinese (and maybe other non-ascii) characters.
# Just read it with PIL and convert it to numpy array!
# Note that you need to convert image to BGR with cv2.cvtColor if you read with PIL.
import numpy as np
from PIL import Image
img = np.asarray(Image.open(r'/path/to/your/image'))

# Parameters:
# L0_Smoothing(img, asHSV=False, lambda_=2e-2, kappa=2.0, beta_max=1e5, mode='pyvkfft')
# L0_Smoothing_accel(img, asHSV=False, lambda_=2e-2, kappa=2.0, beta_max=1e5)
#     img: numpy array of the image to be smoothed
#     asHSV: This module does operation per channel, and you can choose to convert it to HSV
#            while operating by giving parameter asHSV=True.
#     lambda_, kappa, beta_max: read the paper
#     mode: the OpenCL FFT backend to use
smoothed = L0_Smoothing_accel(img)

If you are programming with pyopencl, you can use this module like this:

import pyopencl as cl
import numpy as np
import pyopencl.array as clArray

from L0_Smoothing import L0_Smoothing_CL

ctx = cl.create_some_context(interactive=False)
queue = cl.CommandQueue(ctx)

img = np.asarray(Image.open(r'/path/to/your/image'))
S = clArray.to_device(queue, img/255)
S_smoothed = L0_Smoothing_CL(S)
img = S.get()*255
img = np.clip(img, 0, 255).astype(np.uint8)

Hint: You can try to apply blur before doing smoothing if the smoothing effect is not ideal.

Execute from terminal

Notes:

  • It support only jpg and png images now.
  • Input and output path should be both file or both folder.
  • You can give lambda_, kappa, beta_max via --params.
  • You can choose to use slower numpy version with switch --noaccel.
  • If you give show for output path, processed image with not be saved but showed.
# get some help
L0-Smoothing --help

# process single image and save it somewhere
L0-Smoothing /path/to/your/image /path/you/want/to/save

# process all images in a folder and save it somewhere
L0-Smoothing /path/to/your/image/folder /path/you/want/to/save

Maybe FAQ

  • When I use its command from terminal, it throws error!
    Try adding switch --noaccel to use slower numpy version.
  • pip throws errors (on compiling) when I am installing pyvkfft!
    Download pyvkfft package bundled with OpenCL SDK from release and install it with pip.
    Or just download binary package from release and install it.
  • Binary packages have problem on my machine.
    Use reikna as fft backend, it is pure python.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

L0-Smoothing-0.1.3.tar.gz (11.9 kB view hashes)

Uploaded Source

Built Distribution

L0_Smoothing-0.1.3-py3-none-any.whl (17.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page