# Grayscale Morphology

How can I achieve grayscale morphological operations on 2D images in Mathematica. With morphology I mean operations like Dilation and Erosion. With grayscale I mean the operations that take images and kernels with scalar values.

The only functions I can find (Dilation[image,ker], Erosion[image,ker],...) only allow binary kernel matrices with values 0 and 1 only.

Is there a built-in function? Or can anyone provide an implementation. Dilation and Erosion are the most important ones, since most others can be implemented by these.

• I may be mistaken here, but I always thought that the definition of the dilation and erosion operators required the kernels to be binary. Dilation picks the maximum value of all the image pixels below the 1's in the kernel for every location the kernel is placed on. Non-binary values would not make sense in this context. Perhaps you are looking for another operation like ImageConvolve? – Sjoerd C. de Vries Jan 29 '16 at 21:16
• @SjoerdC.deVries: What you're thinking of, and what Mathematica apparently implements, is a special case of grayscale morphology with flat kernels. One can certainly use nonflat kernels in principle. – Rahul Jan 29 '16 at 21:23
• en.wikipedia.org/wiki/… – azt Jan 29 '16 at 21:25
• Sjoerd, the grayscale morphology operators are less well known, but very important. See the definition in the link Rahul provided. As an example where it is useful: I used it once to find the path of a spherical milling tool, so that a given target surface should (at least) be left. The inverse operation then gave me the actual surface after milling. – azt Jan 29 '16 at 21:30
• To implement this, the best way is certainly ImageFilter[] (which is not a filter, in the meaning of linear and translation invariant ie convolution) that can apply to each pixel wathever function of the its neighborhood. – andre314 Jan 29 '16 at 21:40

Trying to improve on my comment:

The test image:

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


Since Dilation...

dil = Dilation[img, DiskMatrix[4]]


...can also be written as:

fil = ImageFilter[Max[DiskMatrix[4] #] &, img, 4]


ImageData[dil] == ImageData[fil]
(* True *)


this probably means that a non-binaray Dilation can be written as:

ker = Table[i j/81, {i, 9}, {j, 9}];
ImageFilter[Max[ker #] &, img, 4]


Erosion is somewhat more complex. We need two ColorNegate-s here:

er = Erosion[img, DiskMatrix[4]]


erFil = ColorNegate@ImageFilter[Max[DiskMatrix[4] #] &, ColorNegate@img, 4]


ImageData[er] == ImageData[erFil]
(* True *)


So, the non-binary Erosion would then be:

ker = Table[i j/81, {i, 9}, {j, 9}];
ColorNegate@ImageFilter[Max[ker #] &, ColorNegate@img, 4]


I must admit I'm working on my intuition here and haven't really checked this against any official definitions of these functions.

• I think (?) it should be ImageFilter[Max[# + ker] & ... – Niki Estner Jan 29 '16 at 22:40
• @nikie That doesn't seem to work as replacement of Dilation. You could try that with the above example, yielding this, which is not the same result. – Sjoerd C. de Vries Jan 29 '16 at 22:44
• I think, though, that the kernel itself may need to be flipped. – Sjoerd C. de Vries Jan 29 '16 at 22:46
• I'm just translating the formulas on the wikipedia page azt linked. Apparently, to get the same result as in binary morphology, the kernel should contain 0 and -infinity: greyKernel = DiskMatrix[4] /. {0 -> -\[Infinity], 1 -> 0}; fil = ImageFilter[Max[# + greyKernel] &, img, 4] – Niki Estner Jan 30 '16 at 8:25