# Filters on image vs data

I was using GaussianFilter and realized that applying it to an array yields a different result from applying it to an image. For instance:

test = RandomReal[1, {20, 20, 3}];
Image[GaussianFilter[test, 2]]


and

GaussianFilter[Image@test, 2]


yield similar, but not identical, images.

The same seems to happen with other filters.

What is the cause of this and which one should I use?

Edit: Here is some relevant information (Images are scaled up for visibility):

Image[test]


Image[GaussianFilter[test, 2]]


ImageAdjust[%]


GaussianFilter[Image@test, 2]


ImageAdjust[%]


ImageSubtract[%2, %4]


ImageSubtract[%4, %2]


\$Version

(*"10.3.1 for Microsoft Windows (64-bit) (December 9, 2015)"*)

• If you use ImageAdjust[] in both your first and second snippets, what do you get? May 22 '16 at 1:39
• @J.M. They still are different. This time, the difference is more visible. May 22 '16 at 2:08
• Added image examples for clarity. May 22 '16 at 2:33

Using GaussianFilter on the raw RGB data will produce a convolution in all 3 dimensions of the list (rather than just the 2 spatial dimensions of the image). So filtering the raw data will also convolve RGB values at the same pixel.

To see this effect, we can start with a uniform red image where all RGB values are {1,0,0}:

test2 = Table[{1, 0, 0}, {m, 20}, {n, 20}];


If we use a GaussainFilter on the Image, we return the original uniform red image (since spatial filtering should not change the uniform image):

filteredImage = ImageData[GaussianFilter[Image@test2, 2]];
filteredImage[[1, 1]]
(*returns {1,0,0}*)

Image[filteredImage,ImageSize->Small]


However, if we filter the data first, we also convolve over RGB values and start to get non-zero G and B values:

filteredData = ImageData[Image[GaussianFilter[test2, 2]]];
filteredData[[1, 1]]
(*returns {0.737279, 0.262721, 0.0508822}*)

Image[filteredData,ImageSize->Small]


• So, if I use Image first, the RGB values are treated as one thing, but when I use GaussianFilter first, the RGB values are treated separately? May 22 '16 at 4:23
• @JHM, I think it is actually the other way around. If you evaluate Image first, then Mathematica knows that GaussianFilter should just convolve over the 2D image and the RGB values are convolved separately (R with adjacent R, G with adjacent G, etc.). However, if you ask Mathematica to evaluate GaussianFilter on a 3D array (list of lists), it assumes it should convolve in all directions (x,y, and RGB). So evaluating GaussianFilter on the data first mixes up RGB values. May 22 '16 at 4:30
• Ah, I understand now. (I meant by "separately" that the GaussianFilter would think each value in RGB is separate (instead of treating it as one pixel), meaning it would not know that the RGB values should not be convolved; is this correct?) May 22 '16 at 4:46
• @JHM, sorry for the mix up. Yes, that's completely right. May 22 '16 at 4:49