maweigert / gputools

GPU accelerated image/volume processing in Python
BSD 3-Clause "New" or "Revised" License
108 stars 20 forks source link

Convolve without preserving the size ("valid" convolution) #33

Open jaggiK opened 1 year ago

jaggiK commented 1 year ago

Hi,

Thanks for building this great package.

Convolve seems to produce same output size as input size commonly referred as "same" convolution. Is there a way to perform convolution "valid" convolution where output size is shrinked?

maweigert commented 1 year ago

hi,

convolve does not support "valid" convolution out of the box, but it should be easy to define it yourself:


import numpy as np
from gputools.convolve import convolve

def convolve_valid(x,h, **kwargs):
    y = convolve(x, h, **kwargs)
    # extract valid region
    valid_slices = tuple(slice(s//2, -(s-s//2) if s>1 else None) for s in h.shape)
    return y[valid_slices]

x = np.random.rand(128,128)
h = np.random.rand(3,11)

res = convolve_validx,h)

Hope that helps!