Recover myself from Dilate and Erode for noise reduction

i have obtained some skin detection map (i post below two example of detection map). but, i'm using a normal webcam and the map is noisy. Not a lot, just a bit.

I think i can remove noise only with some combination of dilate and erode, and then threshold it.

But, like a stupid child, i have spent hours in trying to set parameters, use in different combination like: dilate twice, erode three times, blur, and an erode again and so on.

The only result i have obtained is to lost myself: it is not a robust approach at all.

So, for image like this, how can i use those super-popular filters? Or some advice for getting a nice contour of the face and removing the noise?

EDIT:

Those two are two different approach to skin recognition: backprojection and mahalonobis distance, using as training data the output of a simple haar face detection. I'm using both of them because with some illumination setting the first is better, for others the second is better. For my porpouse i can choose the best anytime I'm using my application.

-
Can you explain the images you posted? Are those two skin detection maps with different level of noise, or is the first one an example map and the second one your attempt at a solution? Also, can you maybe post larger images or at least links to those? –  penelope Nov 20 '12 at 9:10
ok thank you, see the edit –  nkint Nov 20 '12 at 9:57
so They would be two different example maps... and you always use the better one? Take a look at my answer, you'll see why I'd like a better view of the maps :) –  penelope Nov 20 '12 at 9:59

This is more of an extension to the ideas mentioned by @Dima and @heltonbiker.

As @heltonbiker said, it might be a good idea to apply thresholding first: I would be more confident this is the right approach if the images you posted were a bit bigger so I could examine the details for myself. Adaptive thresholding is one that is (more or less) robust to background illumination changes, and there's a good post here that you can use as a starting point.

@Dima suggested opening and closing as higher order morphological operations: they are just one step more complicated than erosion and dilation. You should definitely try approaches from the simpler to the more complicated, so I would try his idea first.

But, in case that does not give satisfactory results (be prepared, nothing will give perfect results), let me suggest two morphological approaches that are a tad more complicated:

• first one is area closing and opening, specific types of algebraic openings and closings. They remove bright/dark elements not by direct application of a structuring element, but rather operate on shapes who satisfy a certain criterion (e.g. size $\ge$ or $\le 100$ pixels).

A successful implementation of an area closing would, for example, allow you to fill all the black structures with a size smaller that a threshold completely, while leaving the bigger black structures intact.

I have talked briefly about that in this SO answer (second bulet), and this article/chapter looks useful (although I've never read it): M. Van Droogenbroeck: Anchors of morphological operators and algebraic openings.

• second one is applying alternating sequential filters. They're nice filter filters that treat dark and bright noise equivalently, so you should use them if that's the case. It also tries to "prevent" the removal of bright noise to remove bright objects (the same for dark noise/objects).

First, let me define sequential filters: it's nothing more than simply applying sequential application of opening and closing with an elementary structuring element for the grid in a combination of orders:

$m_i = \gamma_i \phi_i$; $n_i = \phi_i \gamma_i$

$r_i = \phi_i \gamma_i \phi_i$; $s_i = \gamma_i \phi_i \gamma_i$

where $\gamma_i$ is an opening and $\phi_i$ a dual closing. The index $i$ indicates the size of the structuring element.

Sequential filters by itself are sometimes enough: remove bright and then dark specks of certain size (or reverse order). Sometimes, however, if the SE size is not big enough, some things don't get cleaned, while if it's too big, removing bright specks messes with the bright structures. Here, alternating sequential filters come in to play: you start with a small, precise SE and then gradually increase SE size you work with. Note, this means multiple openings and closings, so it's a slow operation.

The general idea to construct an alternating sequential filter is:

$M_i$ = $m_i \cdots m_2 m_1$, and equivalent for other sequential filters.

Some references: discussion that lead to pseudocode in MATLAB, as well as an article that seems useful: S. Pei, C. Lai: An Efﬁcient Class of Alternating Sequential Filters in Morphology

I drew all my explanations from a book P. Soille: Morphological Image Analysis, but since it's a book, not an article, I didn't find a readily accessible PDF copy.

-

First of all, are you using gray-scale morphology? If so, then I would threshold the map first, to get a binary image, and then apply binary morphology to fill in gaps and get rid of the noise.

Second, instead of erosion and dilation, try opening and closing, which are higher order morphological operations. Opening is erosion followed by dilation with the same structuring element. Closing is dilation followed by erosion.

A good thing to try is to do an opening to get rid of the noisy specks, and then do a closing to fill in the gaps. This will simplify your process to three operations: threshold, open, close. So you would only need to tune three parameters: the threshold and the two structuring elements.

-
If you use threshold, I strongly advice trying an adaptive threshold like this: duplicate the original, and blur it; Subtract the blured image from original (possibly multiplying it for a constant factor); Apply threshold. That would be threshold(original - k * (blured_original)). The three parameters would be blur radius, multiplier, and threshold level. –  heltonbiker Nov 19 '12 at 12:19