Gatan | AMETEKSkip to Main Content
No options found

Interactive histogram

DigitalMicrograph Script

Compute a histogram of the frontmost image and allow interactive change of histogram bins.

Preview

/////////////////////////////////////////////////////////////////////
// (c) Gatan Inc.
/////////////////////////////////////////////////////////////////////
// This script compute the histogram of the front most image.
// It will initially use 100 bins for the histogram, but the 
// user can increase or decrease the number of bins using the 
// cursor UP and DOWN keys.
/////////////////////////////////////////////////////////////////////
//	last modified 08-July-2014 BS


// This method update the histogram image hst to display the value range [minV,maxV] with nChannels bins. 
// If the hst image is not yet shown, it will display it next to the source image src.
void UpdateHistogram( image src, image &hst, number nChannels, number minV, number maxV )
{
	// Verify that the source image src is valid.
	if ( !ImageIsValid(src) ) Throw( "Invalid input image source.")
	
	// Ensure minimum of 3 channels
	nChannels = ( nChannels < 3 ) ? 3 : nChannels
	
	// Check if histogram image exists, otherwise create and display
	if ( !ImageIsValid(hst) )
	{
		hst := IntegerImage( "Histogram", 4, 0, nChannels  )	// unsigned 4-byte integer numbers
		
		// Calibrate the X axis
		number origin = minV
		string unit = src.ImageGetIntensityUnitString() 
		number scale = (maxV-minV)/nChannels 
		hst.ImageSetDimensionCalibration(0, origin, scale, unit, 0 )
		
		// Calibrate the intensity axis
		hst.ImageSetIntensityOrigin(0)
		hst.ImageSetIntensityScale(1)
		hst.ImageSetIntensityUnitString("#pixels")
		
		// Find display position of source image and display right of it
		// (Assumes that src is displayed!)
		number t,l,b,r
		src.ImageGetOrCreateImageDocument().ImageDocumentGetWindow().WindowGetFrameBounds(t,l,b,r)
		ShowImage(hst)
		hst.ImageGetOrCreateImageDocument().ImageDocumentGetWindow().WindowSetFramePosition(r,t)
	}
	
	// Update the histogram. 
	// First resize to have the appropriate number of channels.
	// (This keeps the window in place. )
	ImageResize( hst, 1, nChannels )
	
	// Ensure the window displays all channels
	hst.ImageGetImageDisplay(0).LinePlotImageDisplaySetDisplayedChannels(0,nChannels)
	
	// Reset the histogram calibrations. (Image resize keeps the FOV!)
	hst.ImageSetDimensionScale( 0, (maxV-minV)/nChannels )
	
	// Then recompute the histogram values
	ImageCalculateHistogram( src, hst, 0, minV, maxV )	
}



string msg
msg += "<UP> to increase histogram channels\n"
msg += "<DOWN> to decrease histogram channels\n"
msg += "<ESC> to end script"

OKDialog(msg)
image src := GetFrontImage()
image hst
number mi = min(src)
number ma = max(src)
number nChannels = 100
number key
while(key != 27) 	// <ESC>
{	
	UpdateHistogram( src, hst, nChannels, mi, ma )
	key = GetKey() 
	if ( 30 == key )	// <up>
	{
		nChannels--
	}
	else if ( 31 == key )	// <down>
	{
		nChannels++
	}
	else
		continue	// iterate next loop, i.e. jump to begin of while()
}
DeleteImage(hst)