0

I have simple example that is not working as expected

import numpy as np
from PIL import Image
from scipy import ndimage

def gaussian_smoothing(image, sigma = 1):
    im = np.array(image)
    sim = ndimage.gaussian_filter(im, sigma=sigma)

    print(im.shape)
    return Image.fromarray(sim)

def main():
    im = Image.open("./data/kodim03.png")

    filtered_image = gaussian_smoothing(im, 1)

    filtered_image.show()

Input Image:

The shape of the image is (512, 768, 3).

Output Image:

The image is greyed out. That is not what I was expecting.

Ive done some digging but I am still confused. From the scipy.ndimage.gaussian_filter doc page:

sigma: scalar or sequence of scalars

Standard deviation for Gaussian kernel. The standard deviations of the Gaussian filter are given for each axis as a sequence, or as a single number, in which case it is equal for all axes.

When I change my code and replace the line sim = ndimage.gaussian_filter(im, sigma=sigma) with sim = ndimage.gaussian_filter(im, sigma=(sigma, sigma, 0)) I get expected result.

Expected output:

I don't understand the concept of the axes. I think the first two axes are X direction and Y direction. But is third axis a color?

My experience with convolutions and filters is very limited but I always thought about it as a simple box moving across the image and applying Gaussian soothing. Since we have three channels here, RGB, I would imagine the process will be repeated across all 3 channels and some sort of averaging will happen.

Can someone explain to me what is the idea about going in X, Y and color direction and how does it work.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
  • Welcome to Stack Overflow. "But is third axis a color?" Yes. Specifically, the `3` values found along this axis represent *components* of the colour - most likely, the "red", "green" and "blue" components. This is not a question about the code; it is a question about *how images are represented on computers*, and [is properly answered](https://meta.stackoverflow.com/questions/261592) by [doing research](https://duckduckgo.com/?q=how+do+computers+store+images), not by asking here. If you must use a Stack Exchange site, [cs.se] is probably more appropriate. – Karl Knechtel Jun 17 '22 at 20:08
  • "Since we have three channels here, RGB, I would imagine the process will be repeated across all 3 channels and some sort of averaging will happen." The data is 3-dimensional; the averaging happens *across* the 3 channels, not just within each channel. Numpy and Scipy **do not know** that the data represents an image; as far as the math is concerned, the `3` is *just another dimension of 3-dimensional data - that will be handled the same way unless you tell it otherwise. Which is why `sigma=(sigma, sigma, 0)` fixes the issue; it says to use `0` blurring across the colour-component axis. – Karl Knechtel Jun 17 '22 at 20:10
  • Since you already have code that fixes the practical aspect of the problem, and the conceptual aspect of the problem seems to be off topic here, I am voting to close as not reproducible. – Karl Knechtel Jun 17 '22 at 20:12
  • @KarlKnechtel It is a little bit of a shame since you helped me a lot. Your second comment made me connect some dots and pointed me in a right direction - without even looking at your links. I dont understand stackoverflow community. I am not saying there is something wrong with it or it should change but I dont understand it. Thank you very much and have a good one. – Howisitgoing Jun 18 '22 at 09:33

0 Answers0