Authors: François GANNIER (gannier at univ-tours dot fr) - Côme PASQUALIN University of Tours (France) See also: Others productions for ImageJ from the authors
PCCV group main pageHistory: 2015/07/01: 0.1 - First version
Source: Link to GitHub.
Requires: Tested on ImageJ 1.51g but should works on older. Limitations: Should works on Mac OS, Linux and Windows Installation: Download and copy HRtime.jar in the jars folder then restart ImageJ. Description: Using getTime() in ImageJ is problematic when you want a resolution less than 100 ms. HRtime allows to measure time with a resolution of about 1 microsecond. More you can suspend a process for a definite time with the same precision.
Usage : HRtime.gettime: no argument, return the time in microsecond
HRtime.sleep: two arguments, stop the current process during time.
Arg 1: reference time
Arg 2: time to sleep compared to reference time
Note that arguments are passed as string so you need to format numbers correctly with function d2s to avoid scientific notation. (see example 2).Example 1 : The following code display laps time for each action and total time at end with a precision of microsecond. // open an image run("About ImageJ..."); // store actual time startTime = parseInt(call("HRtime.gettime")); // save as lasttime lastTime = startTime; // Add actions here ex: invert run("Invert"); lapsTime = parseInt(call("HRtime.gettime")); laps = (lapsTime - lastTime)/1000.0; print("laps = " + laps+"ms"); lastTime = lapsTime; // Add another actions here ex:FFT run("FFT"); lapsTime = parseInt(call("HRtime.gettime")); laps = (lapsTime - lastTime)/1000.0; print("laps = " + laps+"ms"); lastTime = parseInt(call("HRtime.gettime")); total = (lastTime - startTime)/1000.0; print("total time :"+total+"ms");
Example 2 : The following code wait for an image from the IJ_IDS_cam plugin without overcharging the CPU. Acquisition is 500Hz so we sleep thread by block of 250us as we are waiting for the next image. Add a timeout of 1s if no image is coming. // set Time out of 1s HRTimeOut = 1e6; // set Time to sleep the thread in us HRwait = 250; // store actual time startTime = parseInt(call("HRtime.gettime")); newTime = startTime; // Init Cam.newImage call("ij.Prefs.set", "Cam.newImage","false"); print("Starting..."); // SPACE key to exit while (!isKeyDown("space")) { currentTime = parseInt(call("HRtime.gettime")); print("Waiting image..."); // loop to wait next image while (call("ij.Prefs.get", "Cam.newImage","false") == "false") { // but reduce CPU usage currentTime = parseInt(call("HRtime.sleep",d2s(currentTime,16), d2s(HRwait,16))); // exit at time out if ((currentTime - newTime) >= HRTimeOut) { print("time out: exit"); exit(); } } // Set "Cam.newImage" to false to prepare next one call("ij.Prefs.set", "Cam.newImage","false"); // store new value newTime = currentTime; laps=(currentTime-startTime)/1000; print("New image at "+laps); // make what you want with the new image } print("Done");