10
$\begingroup$

In versions 8.0.4 and 9.0.1 applying Blur to an image gives expected result:

img = Import["http://i.stack.imgur.com/UsJTa.png"]

blurred = Blur[img, 5]

ColorSeparate[blurred]

image

image

output

But in the recent versions Blur as well as GaussianFilter and ImageConvolve simply do nothing (checked with versions 10.0.1, 10.4.1, 11.1.1 and 11.2.0):

blurred = Blur[img, 5]
ImageData[blurred] == ImageData[img]

image

True
filtered = GaussianFilter[img, 5]
ImageData[blurred, Byte] == ImageData[img, Byte]

image

True

Why this happens? Is it a bug? Is there a workaround?

$\endgroup$
11
$\begingroup$

Starting from Mathematica 10.0 such functions as Blur, GaussianFilter, ImageConvolve etc. don't affect alpha channel of an Image anymore. This change makes their behavior consistent with such related functions as ImageAdjust, Dilation, ImageEffect etc. which didn't change the alpha channel in previous versions. Hence it looks more like a bug fix.

To reproduce the old behavior, we should remove the ColorSpace information which is used to determine which color channel is the alpha channel. According to the Documentation page for ImageColorSpace, Automatic colorspace means that "no color space specified". Let us try:

Image[Blur[Image[img, ColorSpace -> Automatic], 5], ColorSpace -> ImageColorSpace[img]]

ColorSeparate[%]

image

output

Equivalently one can apply Blur separately to each channel:

ColorCombine[Blur[#, 5] & /@ ColorSeparate[img], ImageColorSpace[img]]

image


Another way is to use a function which is still applied to the alpha channel.

As of Mathematica 11.2.0, ImageFilter works with all the channels:

ImageFilter[Mean[Flatten[#]] &, img, 3]
ColorSeparate[%]

image

output

A bit surprisingly, but ImageCorrelate is also still applied to the alpha channel (Mathematica 11.2.0):

ImageCorrelate[img, GaussianMatrix[5]]
ColorSeparate[%]

image

output

I don't know why the exception is made for these two functions. May be a bug?

$\endgroup$
  • $\begingroup$ For the down-voter: please comment why you down voted? I fail to see your reasoning. $\endgroup$ – Alexey Popkov Sep 23 '17 at 5:55
6
$\begingroup$

This is happening because the Imagedata in your image is all zeros

img = Import["http://i.stack.imgur.com/UsJTa.png"]

When you filter (or Blur or ImageConvolve) an image of all zeros, you get back an image with all zeros. The reason you see anything at all is because the alpha channel is sometimes zero and sometimes one. You can verify this by removing the alpha channel:

RemoveAlphaChannel[img]

enter image description here

So the bottom line is that Blur (and the other functions, presumably) only work on the image data, and not on the alpha channel.

To make Blur will work on this image, you can place the alpha channel into a data channel. Here's one way:

img2 = Image[ImageData[img] /. {r_, g_, b_, alpha_} -> 1 - alpha];
Blur[img2, 5]

enter image description here

$\endgroup$
  • 1
    $\begingroup$ I'm still unsure that this behavior is in accord with the Documentation statement for Blur: "For multichannel images, it operates separately on each channel." So actually "it operates separately on each channel" EXCEPT the alpha channel (which is a part of ImageData)? $\endgroup$ – Alexey Popkov Sep 21 '17 at 0:06
  • 1
    $\begingroup$ Alpha channels are always tricky to handle since they are so different from color channels, but yes, the functions are not being applied to the alpha channel. $\endgroup$ – bill s Sep 21 '17 at 0:12

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.