Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

"Top hat" filter in C++

293 views
Skip to first unread message

Raphael Marc

unread,
Apr 25, 2001, 11:28:02 AM4/25/01
to
Hello,

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

Norberto Malpica

unread,
May 16, 2001, 12:39:05 PM5/16/01
to
Hi, Raphael,

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

Xavier MAILLARD

unread,
May 17, 2001, 5:53:52 PM5/17/01
to
Hi,

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

Costas Vlachidis

unread,
May 18, 2001, 12:14:31 PM5/18/01
to
Hi
In Open cv exists a ready to use op-hat function
Costas
? "Xavier MAILLARD" <x.lsi.m...@wanadoo.fr> ?????? ??? ??????
news:9e1hci$qog$1...@wanadoo.fr...

John Russ

unread,
May 18, 2001, 4:58:22 PM5/18/01
to

> > > Raphael Marc wrote:
> > >
> > >> Hello,
> > >>
> > >> Has anyone already coded the "top hat" filter (also named "rolling
> > >> ball") in C++ ?
> > >> I'd be interested by such a piece of code.
> > >>
...and several people have answered, but what they have been describing
is NOT a real top-hat filter. The difference between the original and
the opened image, or the closing of an opening, are at best weak
approximations and in most cases do not do what the real top hat does,
which is find features with specific size/brightness characteristics.

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.

0 new messages