Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does Pyvips have uniformly sampled noise? #436

Closed
stephandooper opened this issue Oct 17, 2023 · 2 comments
Closed

Does Pyvips have uniformly sampled noise? #436

stephandooper opened this issue Oct 17, 2023 · 2 comments

Comments

@stephandooper
Copy link

stephandooper commented Oct 17, 2023

Hi,

I'm trying to create a function in Pyvips that perform elastic deformation transformations. I managed to get the function itself to work, however the algorithm I am trying to implement expects uniform sampled noise in the [0,1] range[1]. My question is if there is such a function in Pyvips, since I managed to only find gaussian, perlin, and worley noise.

The way I circumvented this is by using an inverse box-muller transformation on two independent Gaussians, but I recon that this is not the most efficient way to do this.

P.s.: is there a way to set the seed globally for the noise functions, or is defined per function?

def elastic_deformation(image, sigma=16, alpha=1):
    width, height, channels = image.width, image.height, image.bands

    # Create a random displacement field, pyvips does not have uniform
    # instead, use a Gaussian and convert using Box-Muller inverse
    z1 = pyvips.Image.gaussnoise(width, height, sigma=1.0, mean=0.0, seed=1)
    z2 = pyvips.Image.gaussnoise(width, height, sigma=1.0, mean=0.0, seed=1)

    # Compute box-muller inverse to get approximate uniform values
    dx = (-(z1*z1 + z2*z2) / 2).exp()
    dx = (2 * dx - 1).gaussblur(sigma) * alpha

    dy = (z1 / z2).atan()
    dy = (2 * dy - 1).gaussblur(sigma) * alpha


    grid = pyvips.Image.xyz(image.width, image.height)
    map = grid + dx.bandjoin([dy])

    image = image.mapim(map, interpolate=pyvips.Interpolate.new('bilinear'), background=[255, 255, 255])
    return image

[1] Simard2003] Simard, Steinkraus and Platt, "Best Practices for Convolutional Neural Networks applied to Visual Document Analysis", in Proc. of the International Conference on Document Analysis and Recognition, 2003

@jcupitt
Copy link
Member

jcupitt commented Oct 17, 2023

Hi @stephandooper,

You're right, there's no simple uniform noise generator, I suppose I've never needed one. Let's tag this as an enhancement for 8.16.

I can see a global seed might be useful too. You can call g_random_set_seed():

https://developer-old.gnome.org/glib/stable/glib-Random-Numbers.html#g-random-set-seed

But of course that's not exposed by pyvips.

@jcupitt
Copy link
Member

jcupitt commented Oct 17, 2023

I added an enhancement issue, let's continue any discussion there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants