5
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – Sjoerd C. de Vries Jan 29 '16 at 21:16
  • $\begingroup$ @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. $\endgroup$ – Rahul Jan 29 '16 at 21:23
  • $\begingroup$ en.wikipedia.org/wiki/… $\endgroup$ – azt Jan 29 '16 at 21:25
  • 1
    $\begingroup$ 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. $\endgroup$ – azt Jan 29 '16 at 21:30
  • $\begingroup$ 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. $\endgroup$ – andre314 Jan 29 '16 at 21:40
2
$\begingroup$

Trying to improve on my comment:

The test image:

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

enter image description here

Since Dilation...

dil = Dilation[img, DiskMatrix[4]]

Mathematica graphics

...can also be written as:

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

Mathematica graphics

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]

Mathematica graphics

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

er = Erosion[img, DiskMatrix[4]]

Mathematica graphics

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

Mathematica graphics

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]

Mathematica graphics

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

$\endgroup$
  • $\begingroup$ I think (?) it should be ImageFilter[Max[# + ker] & ... $\endgroup$ – Niki Estner Jan 29 '16 at 22:40
  • $\begingroup$ @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. $\endgroup$ – Sjoerd C. de Vries Jan 29 '16 at 22:44
  • $\begingroup$ I think, though, that the kernel itself may need to be flipped. $\endgroup$ – Sjoerd C. de Vries Jan 29 '16 at 22:46
  • $\begingroup$ 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] $\endgroup$ – Niki Estner Jan 30 '16 at 8:25

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.