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
Two variants: uniform window mean, or Gaussian window mean (ImageMagick-style notes).
Python 3.9+.
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
pip install -U pip
pip install pykuwaharaDev / tests:
pip install -e ".[dev]"
pytest -q
pytest -q --cov=pykuwahara --cov-report=term-missingDependencies: numpy, opencv-python-headless. Do not install another PyPI package that ships its own cv2 alongside it.
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: OpenCV
cv2.imwrite('lena-kfilt-mean.jpg', filt1)
cv2.imwrite('lena-kfilt-gaus.jpg', filt2)
import cv2
from pykuwahara import kuwahara
image = cv2.imread('photo.jpg')
filt1 = kuwahara(image, method='mean', radius=4)
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)
For colour images, variance uses one channel (grayscale by default, or image_2d, or grayconv).
import cv2
from pykuwahara import kuwahara
image = cv2.imread('selfie.jpg')
image = (image / 255).astype('float32')
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)