Skip to main content

Kuwahara filter in python

Project description

pykuwahara

Kuwahara filter in Python (numpy + OpenCV).

The Kuwahara filter is a non-linear smoothing filter used in image processing for adaptive noise reduction. It is able to apply smoothing on the image while preserving the edges. Source: Wikipedia

This implementation provide two variants of the filter:

  • The classic one, using a uniform kernel to compute the window mean.
  • A gaussian based filter, by computing the window gaussian mean. This is inspired by the ImageMagick approach.

Installation

pip install pykuwahara

Usage

Simple example

import cv2
from pykuwahara import kuwahara

image = cv2.imread('lena_std.jpg')

filt1 = kuwahara(image, method='mean', radius=3)
filt2 = kuwahara(image, method='gaussian', radius=3)    # default sigma: computed by OpenCV

cv2.imwrite('lena-kfilt-mean.jpg', filt1)
cv2.imwrite('lena-kfilt-gaus.jpg', filt2)

Original image

Original image

Filtered with Kuwahara (mean)

Mean method

Filtered with Kuwahara (gaussian)

Gaussian method

Painting effect

Kuwahara filter can be used to apply a painting effet on pictures.

import cv2
from pykuwahara import kuwahara

image = cv2.imread('photo.jpg')

# Set radius according to the image dimensions and the desired effect
filt1 = kuwahara(image, method='mean', radius=4)
# NOTE: with sigma >= radius, this is equivalent to using 'mean' method
# NOTE: with sigma << radius, the radius has no effect
filt2 = kuwahara(image, method='gaussian', radius=4, sigma=1.5)

cv2.imwrite('photo-kfilt-mean.jpg', filt1)
cv2.imwrite('photo-kfilt-gaus.jpg', filt2)

Original image (source: wikipedia)

Original image

Filtered with Kuwahara (mean)

Mean method

Filtered with Kuwahara (gaussian)

Gaussian method

Advanced usage

Color image are supported by grayscaling the source image and using the gray channel to calculate the variance. The user can provide another channel at his convenience, and alternatively give the right color conversion code (default is COLOR_BGR2GARY).

import cv2
from pykuwahara import kuwahara

image = cv2.imread('selfie.jpg')
image = (image / 255).astype('float32')     # pykuwahara supports float32 as well

lab_image = cv2.cvtColor(image, cv2.COLOR_BGR2Lab)
l, a, b = cv2.split(lab_image)

hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(hsv_image)

filt1 = kuwahara(image, method='gaussian', radius=5, sigma=2., image_2d=l)
filt2 = kuwahara(image, method='gaussian', radius=5, sigma=2., image_2d=v)

cv2.imwrite('selfie-kfilt-gaus1.jpg', filt1 * 255)
cv2.imwrite('selfie-kfilt-gaus2.jpg', filt2 * 255)

Original image (source)

Original image

Filtered with Kuwahara on L (Lab)

Lab

Filtered with Kuwahara on V (HSV)

HSV

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

pykuwahara-0.3.2.tar.gz (18.2 kB view details)

Uploaded Source

Built Distribution

pykuwahara-0.3.2-py3-none-any.whl (17.0 kB view details)

Uploaded Python 3

File details

Details for the file pykuwahara-0.3.2.tar.gz.

File metadata

  • Download URL: pykuwahara-0.3.2.tar.gz
  • Upload date:
  • Size: 18.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.6.9

File hashes

Hashes for pykuwahara-0.3.2.tar.gz
Algorithm Hash digest
SHA256 4df994efb92f5fadd244b3e171d9586705da5a995aad059979e23cdb6b588f20
MD5 3760f15ed44b3b2718dc60b6947426a2
BLAKE2b-256 a22f234cebb72cb2923065c41eab786026a6a200916451deb9065d4427fa22d8

See more details on using hashes here.

File details

Details for the file pykuwahara-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: pykuwahara-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 17.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.6.9

File hashes

Hashes for pykuwahara-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 79696b2c5b49a12cd603e946f0b6fb3572f96c333ce10fa4b559b083e872b41d
MD5 f2f4ac4aa4a9295c8814cbb53f6a67c9
BLAKE2b-256 530b11bbca85ffefe872284d98cbf6af1c010a11cf93e618496c97059bbfe914

See more details on using hashes here.

Supported by

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