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 details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

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

Uploaded Python 3

File details

Details for the file L0-Smoothing-0.1.3.tar.gz.

File metadata

  • Download URL: L0-Smoothing-0.1.3.tar.gz
  • Upload date:
  • Size: 11.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.1

File hashes

Hashes for L0-Smoothing-0.1.3.tar.gz
Algorithm Hash digest
SHA256 a67499a79312abb407797ba145434b2e8925025d58a0130b8e30c83fbec82e92
MD5 dcb27dd24cd6ea916e41752be97fc82e
BLAKE2b-256 0475fbd425dc4a3763cfbaded4179e008a33c8ed2555108cae20863da3b0362f

See more details on using hashes here.

File details

Details for the file L0_Smoothing-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: L0_Smoothing-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 17.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.1

File hashes

Hashes for L0_Smoothing-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 2fb3487bc3b28b5ef07e63000913f2cc360e14e0c5d946feefa12190baf894b7
MD5 72a656b1729a0ad9967ae5b06572e66f
BLAKE2b-256 9dd1c3c82d880f5e52ca00c3153f5b2b54b8105878f71e6e8e7c86adf1542bed

See more details on using hashes here.

Supported by

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