6
$\begingroup$

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

Image[GaussianFilter[test, 2]]

Filter then Image

ImageAdjust[%]

Filter then Image, Adjusted

GaussianFilter[Image@test, 2]

Image then Filter

ImageAdjust[%]

Image then Filter, Adjusted

ImageSubtract[%2, %4]

Difference1

ImageSubtract[%4, %2]

Difference2

$Version

(*"10.3.1 for Microsoft Windows (64-bit) (December 9, 2015)"*)
$\endgroup$
  • $\begingroup$ If you use ImageAdjust[] in both your first and second snippets, what do you get? $\endgroup$ – J. M. will be back soon May 22 '16 at 1:39
  • $\begingroup$ @J.M. They still are different. This time, the difference is more visible. $\endgroup$ – JungHwan Min May 22 '16 at 2:08
  • $\begingroup$ Added image examples for clarity. $\endgroup$ – JungHwan Min May 22 '16 at 2:33
9
$\begingroup$

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]

enter image description here

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]

enter image description here

$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – JungHwan Min May 22 '16 at 4:23
  • 3
    $\begingroup$ @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. $\endgroup$ – Rashid May 22 '16 at 4:30
  • $\begingroup$ 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?) $\endgroup$ – JungHwan Min May 22 '16 at 4:46
  • $\begingroup$ @JHM, sorry for the mix up. Yes, that's completely right. $\endgroup$ – Rashid May 22 '16 at 4:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.