# Reversing the Order of Operators for Edge Detection?

Usually, for edge detection, we perform smoothing and then pass it through difference filter. What if application of difference filter happens first and then smoothing. How the math behind the same will change?

• What is the difference of? Jul 4 '20 at 7:45

In the classic framework both the Smoothing and the Difference Filter are applied using Convolution.
Since it is done using convolution it implies the operation is Linear Spatially Invariant (LSI).
LSI operators can be applied in any order and the result will be the same. This is also a result of the commutativity property of the convolution operator.

Let's say $$f$$ is the smoothing operator and $$g$$ is the difference operator then:

$$y = f \ast g \ast x = g \ast f \ast x$$

In practice, a more computational efficient way is define $$h = f \ast g = g \ast f$$. See Difference of Gaussians.

If these filters are linear and spatially invariant, then they are commutative (because convolution is). See Royi's answer.

If it's really a difference filter, that's a linear operation, so that part would be fine. "smoothing" can mean a lot of things, so I can't tell you whether it's linear.

• Linear is not enough here. Linear operator do not necessarily commute. They must also be Spatially Invariant (Which they are in case they can be represented by convolution) See my answer (We pressed send at the same time).
– Royi
Jul 4 '20 at 7:30
• LTI, not just L... oof, need more coffee. Jul 4 '20 at 7:31
• Yes. For Time line it is LTI for Spatial (Images) I think it goes by LSI.
– Royi
Jul 4 '20 at 9:20

Classical linear smoothing and differences can also be interpreted as: replacing the value of one pixel by a weighted average of it and its surrounding pixels:

$$\widehat{p_{(i,j)}} = \sum_{k,l}w_{(k,l)}p_{(k,l)}\,.$$

Since this a linear operation, they can be combined and iterated (as long as the weights $$w_{(k,l)}$$ don't change, as discussed in the previous answers), and the global result remains a weighted average. Basic intuition may suggest that it is better to remove noise before differentiating, yet for invariant and linear operations, there is no difference.

Finally, the formation process of images is often non-linear, thus many image processing tools introduce a non-linear or non-invariant operations: mathematical morphology, median or stack filters, thresholding, etc. Then, smoothing and differentiating don't commute anymore.