Has anyone already coded the "top hat" filter (also named "rolling
ball") in C++ ?
I'd be interested by such a piece of code.
Thanks for any help,
Raphaël
the top-hat is pretty easy to implement.
Top Hat = Original image - open image
Opening(image) = dilation(erosion(image)).
Dilation is just a maximum operator. Take the maximum value from the
neighbourhood and assign it to your pixel. Erosion is the same, using
the minimum.
Norberto
Quite simple, top-hat (haut de forme in french) is simply your original -
the open image
You can code like this :
//rolling-ball algo
template <class X> sig2d<X> Haut_de_forme(const sig2d<X> & I, Voisinage2d &
V)
{
sig2d<X> ouv=Ouverture(I,V); //Open function
sig2d<X> res=I-Ouverture(I,V); // Top-hat
return res;
}
But thinkin' it can't really help you as you don't know exactly how I
implemented it.
The function take here a template signal (depending on what kind of image
you use : float, uchar,...) and a neighborood variable
Tell me if I can help you more.
Xavier MAILLARD
FXGS project admin
Full X Graphic System
A proper top hat has three parameters, which correspond exactly the the
reason for the name. The traditional silk top hat has a brim and a
crown. The top hat filter uses two neighborhoods (usually circular in
shape, but not necessarily so). One corresponds to the inside of the
hat's brim and the other to the outside. Note that the outer
neighborhood is NOT the full circular array, but just the annular pixels
outside the first neighborhood.
Perform a ranking operation on the pixel values in each neighborhood.
Take the difference between the brightest pixel in the inner
neighborhood and the brightest in the outer. (Or, if your image contains
dark features on a bright background, use the darkest values in each
region). Compare this difference to some threshold value (this is the
height of the crown of the hat). If the difference is greater than the
threshold keep the pixels, usually the original value. If not, erase it.
This will let you find and keep features that are smaller than the inner
diameter, separated by the width of the brim, and brighter (or darker)
than the background by the height of the crown. Very effective. You can
find some examples at
http://www.reindeergraphics.com/tutorial/chap3/enhance04.html
and
http://www.reindeergraphics.com/tutorial/chap4/fourier08.html
The first example shows using the top hat to isolate dark fibers in an
image of muscle tissue, and the second shows it used to find bright
peaks in a Fourier power spectrum.
If you reverse the logic, you have a rolling ball. Just replace pixels
for which the difference exceeds the threshold with the mean (or in some
implementations the median) of the pixels in the outer annular region.
This removes dirt from the image.
Hope that helps clarify things somewhat.