Issue with ImagePlus.getStatistics() method

imagej
roi

#1

Hello there,

I want to report an issue with the performance of the ImagePlus.getStatistics() method. Since version 1.51s of the ImageJ jar-file the following simple Java program takes about 25x times longer to finish (2s vs 50s on my system):

import ij.gui.*;
import ij.ImagePlus;
import ij.IJ;
import ij.process.*;
import java.text.SimpleDateFormat;

public class SpeedTest
{
	public static void main (String[] args)
	{
		SpeedTest.test_Speed();
	}

	public static void test_Speed()
	{	
		ImageStatistics stats = new ImageStatistics();
		ImagePlus image= IJ.createImage("Untitled", "16-bit ramp", 500, 500, 50);
		image.setRoi(new OvalRoi(10,10,5,5));				
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
					
		IJ.log(" "+sdf.format(System.currentTimeMillis()));
		
		for(int i=0;i<500000;i++)
		{	
			stats=image.getStatistics();
		}		
		IJ.log(" "+stats.mean);
		IJ.log(" "+sdf.format(System.currentTimeMillis()));
	}
}

I compared version 1.51r with 1.51s and 1.52a35 as well as 1.51w. Only version 1.51r is fast. This difference in speed can only be seen, when difference between min and max gray value of the total image is big (which is why a test it on an ramp image) and an ROI in the image is selected that is relatively small (which is why I did set such an ROI) compared to the total image. I assume the issue depends on the calibration of the ROI ImageStatistics? I tested this with the standard ImageJ version (not Fiji) without any additional plugins.

Thanks


#2

This regression is fixed in the latest ImageJ daily build (1.52a41).


#3

For those interested in the details: the offending commit seems to be

where the getHistogram2() and getHistogram2(ImageProcessor mask) methods of ShortProcessor and some special-casing for 16-bit images had been removed.

- int[] hist = (ip instanceof ShortProcessor)?((ShortProcessor)ip).getHistogram2():ip.getHistogram();
+ int[] hist = ip.getHistogram();

The latest commit re-adds those methods and re-introduces the special-casing:

- int[] hist = ip.getHistogram();
+ int[] hist = (ip instanceof ShortProcessor)?((ShortProcessor)ip).getHistogram2():ip.getHistogram();