# Re: Image thresholding

• Subject: Re: Image thresholding
• From: Struan Gray <struan.gray(at)sljus.lu.se>
• Date: 18 May 1999 10:41:22 GMT
• Distribution: world
• Newsgroups: comp.lang.idl-pvwave
• Organization: This line intentionally left bland
• References: <3740387C.72B259F7@clinmed.gla.ac.uk> <mgs-1705991122460001@teton.ivsoftware.com> <37405D6E.DA3E3250@ssec.wisc.edu> <7hq0ro\$i24@ds2.acs.ucalgary.ca>
• Xref: news.doit.wisc.edu comp.lang.idl-pvwave:14793

```Liam Gumley, Liam.Gumley@ssec.wisc.edu writes:

> With all due respect to my esteemed colleagues
> David Kastrup and David Fanning, I believe the
> fastest method is as follows:
>
> ((a lt 5) or (a gt 10)) * a

Speed is hard to pin down, and I appreciate the august company I
am keeping here, so I won't make any absolutist remarks. However, for
my sort of data on my sort of machines the HISTOGRAM function usually
works as well as or better than direct comparisons:

a = indgen(100)
hist = histogram(a, reverse_indices=r)
a(r(r(5):r(10)-1)) = 0

This is especially good for things like interactive image
thresholding because you only need to calculate the histogram once.
Also, as your datasets get larger you start to save significant
amounts of memory, since the histogram is usually much smaller than
the comparison arrays.  For arbitrary data with an unknown maximum or
minimum value, there is a danger that you reference elements of the
reverse_indices array which don't exist (try the above with
a=indgen(10)), but cunning use of the BINSIZE, MAX and MIN keywords
usually solves the problem.

For one-time use further speed gains can be had by only
constructing the histogram for the data range you are interested
in, viz:

a = indgen(100)
hist = histogram(a, min=5, max=10, reverse_indices=r)
a(r(r(0):r(5)-1)) = 0

Struan

```