I asked this question already on StackOverflow and was told to ask it here instead, so I will just copy the content of my question. I do understand the general concept of bilateral filtering and I did read several sources on the topic (including some questions on StacOverflow). Most sources use this equation to describe the process (I think originally it is from Course Notes - A Gentle Introduction to Bilateral Filtering and its Applications source, but at this point I am not sure):
With the "general" gaussian filter (that only considers the space) being represented by this
The Gaussian Filter I did already implement successfully. It is only for gray-scale images and so will the bilateral filter (hopefully) be. It's in no way having a good performance, I am aware, but it does the job. In general I'd think I just need to add the multiplication with (Ip - Iq) to make it work as a bilateral filter. My issue now is: what exactly does (Ip - Iq) represents? Or asked differently, where do I retrieve those values?
I'm not sure if my code is needed but here's my implementation anyways. The kernel-algorithm I took from SO - How to calculate a Gaussian kernel matrix efficiently in numpy.
def gaussfiltering (img = , kernelsize=3, sigma=1.9): rows = len(img) columns = len(img) kernel = gaussian_kernel(kernelsize, sigma) horizontallyfiltered = iterate_horizontally(kernel, img, rows, columns, len(kernel)) filtered = iterate_vertically(kernel, horizontallyfiltered, rows, columns, len(kernel)) return filtered def gaussian_kernel(kernel_size=3, sigma=1.9): x = numpy.linspace(-sigma, sigma, kernel_size+1) y = st.norm.cdf(x) kernel = numpy.diff(y) return((kernel/kernel.sum())) def iterate_horizontally (kernel=, img=, rows=0, columns=0, kernelsize=0): horizontallyfiltered = img.copy() for r in range(rows): for c in range(columns): halved = int(kernelsize/2) if ((c - halved) >= 0 and (c + halved) < columns): #is in range for filter indexOfImage = c - halved summed = 0.0 for i in range(kernelsize): summed += img[r][indexOfImage] * kernel[i] indexOfImage+=1 horizontallyfiltered[r][c]=summed return horizontallyfiltered def iterate_vertically (kernel=, img=, rows=0, columns=0, kernelsize=0): verticallyfiltered = img.copy() for r in range(rows): for c in range(columns): halved = int(kernelsize/2) if ((r - halved) >= 0 and (r + halved) < rows): #is in range for filter indexOfImage = r - halved summed = 0.0 for i in range(kernelsize): summed += img[indexOfImage][c] * kernel[i] indexOfImage+=1 verticallyfiltered[r][c]=summed return verticallyfiltered