image-rs / imageproc

Image processing operations
MIT License
755 stars 148 forks source link

Bilat filter #462

Closed csheaff closed 3 years ago

csheaff commented 3 years ago

This is a grayscale implementation of bilateral filtering inspired by a Cython function skimage.restoration.denoise_bilateral. See #217. The truth image located at tests/data/truth/lumaphant_bilateral.png can be generated with the following Python code:

import numpy as np
import matplotlib.pyplot as plt
import skimage.restoration
import pathlib
import imageio
import time

home = pathlib.Path.home()
data_dir = home / "<path>" / "<to>" / "imageproc" / "tests" / "data"
lumaphant_path = data_dir / "lumaphant.png"
input_path = lumaphant_path

img = imageio.imread(input_path, pilmode="L")

sigma_color = 20
sigma_spatial = 2
bins = 255

radius = 3 * sigma_spatial
d = radius * 2 + 1

t = time.time()
img_f_skimage = np.round(255 * skimage.restoration.denoise_bilateral(
    img,
    win_size=d,
    sigma_color=sigma_color,
    sigma_spatial=sigma_spatial,
    bins=bins,
    mode='edge',
    multichannel=False,
    )).astype('uint8')
print(f"skimage.restoration.denoise_bilateral runtime:  {time.time() - t}")

write_path = data_dir / "truth" / (input_path.stem + "_bilateral.png")
imageio.imsave(write_path, img_f_skimage)

A test function comparing the generated image to the truth passes with a tolerance of 1 pixel. As for speed, running the above code yields a function runtime of ~ 0.14 seconds. When I run

cargo +nightly test --release test_bilateral_filter

...the test finishes in 0.04 seconds. It might be good to keep in mind that this is a comparison to compiled Python (i.e. Cython) and not regular Python.

theotherphil commented 3 years ago

Generally looks good, but I've left a handful of nits to address.

Thanks!

csheaff commented 3 years ago

Thanks for the review. Ready for a 2nd.

theotherphil commented 3 years ago

Thanks. Looks good now, except that the documentation doesn’t match the rustdoc format - see e.g. https://doc.rust-lang.org/beta/rust-by-example/meta/doc.html for an example.

csheaff commented 3 years ago

Ready for another look.

theotherphil commented 3 years ago

Thanks!