4
$\begingroup$

Bug introduced in 8.0.4 or earlier, persisting through 11.0.1.


There's a number of *Filter for image processing in Mathematica and they're also supposed to work on array, according to the document. However, they seem not to work properly when Padding -> None option is added, for example:

lap = LaplacianFilter[#, 1, Padding -> None] &;

When the 1st argument of LaplacianFilter is an image, Padding -> None works as expected:

img = Import["ExampleData/rose.gif"];
ImageDimensions@img
(* {223, 164} *)
lap@img // ImageDimensions
(* {221, 162} *)

but when it's just an array, it doesn't:

lap@ConstantArray[1, {3, 3}]
{{-3.21096 + 3.21096 None, -1.52658 + 1.52658 None, -3.21096 + 3.21096 None},
 {-1.52658 + 1.52658 None, 0., -1.52658 + 1.52658 None}, 
 {-3.21096 + 3.21096 None, -1.52658 + 1.52658 None, -3.21096 + 3.21096 None}}

The supposed result is {{0.}}. Apparently LaplacianFilter has just treated None as a plain expression.

I haven't done a thorough test, but it seems that all the *Filters for image processing have the same issue and *Filters for signal processing like BandpassFilter is not influenced.

Is this a bug? Any workaround?

$\endgroup$
2
$\begingroup$

WRI has confirmed to xzczd that this is a bug.

The former here is a sloppy workaround, I prefer the second method.

Reroute with Image@

This works as expected on Image[], so converting to image temporarily to process works:

lap = ImageData@LaplacianFilter[Image@#, 1, Padding -> None] &;
lap@ConstantArray[1, {3, 3}]

{{0.}}

RepeatedTiming[lap@RandomReal[1, {20, 20}];, 2]

{0.00078, Null}

Use Padding->"Fixed"

This works faster as it doesn't require the image to be rendered:

lapfix = LaplacianFilter[#, 1, Padding -> "Fixed"] &
RepeatedTiming[ArrayPad[lapfix@RandomReal[1, {20, 20}], {-1, -1}];, 2]

{0.00028, Null}

This gives the same result:

SeedRandom[1]
a = lap@RandomReal[1, {20, 20}];
SeedRandom[1]
b = ArrayPad[lapfix@RandomReal[1, {20, 20}], {-1, -1}];
a == b

True

$\endgroup$
  • $\begingroup$ What's the definition of lapold? $\endgroup$ – xzczd Nov 30 '16 at 10:16
  • $\begingroup$ @xzczd Just how you defined lap, I'll edit to make that clear. $\endgroup$ – Feyre Nov 30 '16 at 10:16
  • $\begingroup$ Hmm… then I should say this comparison doesn't make much sense. lapold doesn't give the expected result anyway :) . $\endgroup$ – xzczd Nov 30 '16 at 10:19
  • $\begingroup$ @xzczd You're right, it is superfluous. No way of knowing how fast it would run if that did work. $\endgroup$ – Feyre Nov 30 '16 at 10:21
  • $\begingroup$ @xzczd So probably not a bug, but still an absent feature that one would expect to work. $\endgroup$ – Feyre Nov 30 '16 at 10:23

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.