# Histogram Hot-shots Required

• Subject: Histogram Hot-shots Required
• From: davidf(at)dfanning.com (David Fanning)
• Date: Thu, 15 Jul 1999 23:54:23 -0600
• Newsgroups: comp.lang.idl-pvwave
• Organization: Fanning Software Consulting
• Xref: news.doit.wisc.edu comp.lang.idl-pvwave:15691

```Ok you histogram cowboys. Let's see what ya got!

I don't know if it's early on-set Alzheimers, or
I'm just pressing with a long soccer weekend coming
up, or I just haven't had enough beers yet, but
this one is stumping me. I thought I'd give you
folks a chance to see if you can explain something
simply enough that even I can understand it. :-)

Here's what I've got. I have an array (call it an
image) in one window, and a plot of the array's
histogram in another window. I want to plot
the pixel density along the vertical Y axis and
the image pixel value along the X axis (rather
than, say, the bin number). So my code looks
roughly like this:

histdata = info.image->Histogram()
imgRange = info.image->GetImageRange()
bins = Findgen(N_Elements(histdata))
bins = Scale_Vector(bins, Min(imgRange), Max(imgRange))
xrange = [Min(bins), Max(bins)]
Plot, bins, histdata, YTitle='Pixel Density', \$
XTitle='Bin Value' , Title=title, \$
background=charcoal, Color=green, /NoData, \$
XRange=xrange, XStyle=1
OPlot, bins, histdata, Color=yellow

Yeah, yeah, it's an image object, but the Histogram
method is just basically returning the results of
the HISTOGRAM function. The method does other things
like calculate the binsize based on the type of data
I have in the image window, etc. The Scale_Vector
function just scales the bin values so they cover
the entire range of array values.

Now, here is the neat part. As I move my cursor
in the image window, I obtain the image value under
the cursor. I want to plot this value on my histogram
plot as a vertical line. No problem. I just
restore the proper plotting system variables and use
PlotS like this:

PlotS, [value, value], !Y.CRange, Color=red

This works great. Because of the way I scaled the
bins and set up the X axis I can get the line on
the histogram plot in exactly the location I want
it in.

But here is what I don't get. What I want now
(well, this is really a matter of a what my *client*
wants now) is to print out on the histogram plot not
just the value of the image at the cursor location,
but the pixel density at that location. In other words,
this pixel belongs to a particular bin. I want to print
out the total number of other pixels that also belong
in that bin.

What I can't figure out tonight is how to find out
what bin that pixel is in, given that I know the pixel
value. (Even as I write this sentence I have the sense
that this is a trivial exercise, but I'm afraid it is
not yielding the shear number of hours I have spent
on it. At least not for me.)

I've tried a number of things, most of which I'm too
embarrassed to mention. My most promising result looks
like this:

binNumbers = Where(value LT bins)
binNum = binNumbers[0] - 1
XYOuts, 0.975, 0.05, Align=1.0, 'Pixel Density: ' + \$
StrTrim(histData[binNum], 2), /Normal, Color=yellow

This *almost* works, but a close look at the value and the
graph shows some discrepancy. The vertical value line
will cross the graph at, say, 75 and the pixel density will
be calculated at 92. I've tried changing the bin size, and
hence the number of bins in the resulting histogram, but
this doesn't seem to help.

In any case, I'm fresh out of ideas as well as beer. So
I thought I'd turn it over to you. Any ideas will be
*gratefully* accepted. I'm sure it has something to do
with that Reverse_Indices keyword, but whatever it is
escapes me. :-)

Cheers,

David

--
David Fanning, Ph.D.
Fanning Software Consulting
Phone: 970-221-0438 E-Mail: davidf@dfanning.com
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Toll-Free IDL Book Orders: 1-888-461-0155

```