AutoExpose.bsh script

Good morning,

I am starting with Micromanager (version 2.0 gama). I tried to run script autoExpose.bsh, but it threw a run-time error (line 84): “Cannot cast void value to double”. I can print the maxValue but I am not able to convert it to double.

Would you give me some tips or tricks?

Thank You,
Jiri

Welcome to this forum @Jiri,

It looks like I never updated the autoexpose script for the latest version of Micro-Manager. I did so now (the working version should be included in future nightly builds), and copy the working version below:

/*
 * Micro-Manager script that automatically determines the optimum exposure time
 *
 * An image is taken with the current exposure time.  The value of the brighest 
 * pixel (ignoring 'cutoff_' fraction of the bright pixels) will be determined. 
 * When the pixel is too bright exposure will be reduced 'tooBright_' fold and
 * the test will be performed again
 * When the bright pixel is less than 'lowFraction_' of the maximum, try again.
 *
 * Nico Stuurman, 9/1//2008.  Copyright UCSF
 * 2/16/2021, updated for MM 2.0
 */



import ij.process.ByteProcessor;
import ij.process.ImageProcessor;
import ij.process.ShortProcessor;
import ij.ImagePlus;
import ij.process.ImageStatistics;
import ij.measure.Measurements;
import java.lang.Math;
import javax.swing.JOptionPane;

// Percentage of pixels that will be ignored in analysis of bright pixels
double cutoff_ = 0.002;
// Exposure will be reduced tooBright_ fold when the image was too bright
double tooBright_ = 5;
// Aim for the brightest pixel to be this fraction of the dynamic range
double aim_ = 0.85;
// When brightest pixel is higher than this fraction of the dynamic range, 
// consider the image to be saturated
double saturated_ = 0.95;
// When the bright pixel is less than 'lowFraction_' of the maximum, try again.
double lowFraction_ = 0.5;
// Highest allowed exposure: to avoid extreme exposures, do not go higher than this:
double maxExposure_ = 10000;


boolean testExposure () {
   double exposure = mmc.getExposure();
   if (exposure < 1) {
      exposure = 1;
      mmc.setExposure(exposure);
   }
   image = mm.live().snap(false).get(0);
   int width = image.getWidth();
   int height = image.getHeight();

   int nrPixels = width * height;

   ImageProcessor ip;

   pixels = image.getRawPixels();
   if (pixels instanceof byte[]) {
      ip = new ByteProcessor(width, height);
      ip.setPixels((byte[]) pixels);
   }
	else if (pixels instanceof short[]) {
      ip = new ShortProcessor(width, height);
      ip.setPixels((short[]) pixels);
 	}
 	else {
       return false;
 	}

	ImagePlus imp = new ImagePlus("tmp", ip);

	ImageStatistics stats = imp.getStatistics(ij.measure.Measurements.MIN_MAX);

	maxValue = 0.0;
    if (pixels instanceof byte[]) {
       int[] hist = stats.histogram;
       int j=255; 
       int pixelsFound = 0;
       while (pixelsFound < (nrPixels * cutoff_)) {
          pixelsFound += hist[j];
          j--;
       }
       maxValue = j;
    } else {
		maxValue = stats.max;
	}
	double maxValueD = (double) maxValue;
	double nrBits = image.getMetadata().getBitDepth();
	double maxPossible = java.lang.Math.pow (2.0, nrBits);

	if (maxValueD > (saturated_ * maxPossible) ) {
      nextExposure = 1/tooBright_ * exposure;
      mmc.setExposure(nextExposure);
		gui.message ("Saturated image, cutting down on Exposure");
		return false;
	}

	double nextExposure = aim_ * maxPossible / maxValueD * exposure;
	mm.scripter().message("Max pixel Value: " + maxValue + " Max Possible: " + maxPossible + " Best Exposure: " + nextExposure);

	if (nextExposure > maxExposure_) {
		JOptionPane.showMessageDialog(null, "Sample too dim, exposure higher than " + maxExposure_/1000 + " sec. needed");
		return true;
	}

   mmc.setExposure(nextExposure);

   if (maxValueD/maxPossible > lowFraction_)
		return true;
	else
		return false;
}


boolean finished = false;
while (!finished)
	finished = testExposure();
mm.app().refreshGUI();
mm.live().snap(true);


Thank You. Now it run without any error. But when I run it in Demo.cfg several time in the row, the Exposure time always increase until hits maxExposure_, which is not good I suppose. Am I do something wrong?

Reading scripts is good way how to learn scripting, if I am starting with Beanshell. Are there any other scripts which could be read somewhere? Besides contorling cameras and moving stages I am also interested to control laser intensity and LED diod intesity. Where can I find the commands?

Thank You.
Best regards,
Jiri