# What's the difference between EdgeDetect and MorphologicalPerimeter (for a white background)?

I am trying to understand the differences between EdgeDetect and MorphologicalPerimeter (while researching this other question). I understand that, if the image background is not white, they will yield completely different results:

However, I am not sure whether they are guaranteed to return the same result if the background is white. In my tests, they do:

Could someone confirm this conclusion, or show a counter-example?

-

No, they're not the same, and will not, in general, give the same result because they're two very different operations.

• MorphologicalPerimeter gives a Boolean output according to the following logic:

1. 1 — If the pixel value is 1 (i.e., white) and at least 1 pixel in its 8-pixel neighbourhood is 0.
2. 0 in all other cases.

Here's an example with a simple 20x20 image with 1 black pixel along the diagonal. You can actually follow the logic above to verify the output (assume 0 outside the edge for pixels along the border)

img = ColorNegate@Image@IdentityMatrix[20];
MorphologicalParameter[img]


• EdgeDetect, by default, uses the Canny edge detector which is a lot more complicated, multi-step algorithm unlike MorphologicalPerimeter. Roughly speaking, it convolves it with a Gaussian filter, followed by a non-maximum suppression step, which basically sets all pixels that aren't a local-maxima to zero. You can read more about it in the Wikipedia article.

Performing an edge detection on the above image gives you:

EdgeDetect[img]


You can visibly see the difference between the two images above, especially at the corners and the borders.

-
thanks for this very clear answer! –  F'x Apr 15 '12 at 7:46

As R.M already indicated both methods are completely different. EdgeDetect uses gradient based methods, which detect areas in which gradients change sharply. This works nicely on grayscale and color images, as gradients are easy to define there. The basic parameter here is the scale of the gradients that are taken into account.

MorphologicalPerimeter on the other hand, uses a strictly binary approach as explained by R.M. So, how can it work on grayscale and color images then? Well, simple, it uses Binarize or something equivalent internally. You can see that the parameters of Binarize and MorphologicalPerimeter behave identically:

The basic parameter of MorphologicalPerimeter is the threshold, which is principally different from the gradient scale of EdgeDetect.

That EdgeDetect and MorphologicalPerimeter are not identical, even in your "figure 8" example where they seem similar can be easily determined using:

ImageData[MorphologicalPerimeter[Graphics[Style[Text["8"], 400]]]] ==
ImageData[EdgeDetect[Graphics[Style[Text["8"], 400]]]]

(*
==> False
*)


In the zebra example MorphologicalPerimeter can output more than a black image if you tweak the scale parameter:

-

In the help file it says "MorphologicalPerimeter[image] is equivalent to MorphologicalPerimeter[image,0]" treats values above t as foreground.. So, using value other than 0 gives the result you need.

-
Sorry, but I don't think that answers the question. I'm asking: for a white background, are MorphologicalPerimeter (with its implied zero as second parameter) the same as EdgeDetect? –  F'x Apr 14 '12 at 22:05
well the point is: the function yields a binary image in which pixels considered to be on the morphological perimeter have value 1 and others have value 0. So every thing is above 0 so it's all black... –  s.s.o Apr 14 '12 at 22:11