Bug in the composite ImageStatistics object

Hi,
I posted this question in the ImageJ mailing list, but since then, I found a work around for the time being. When I ran the following code, the composite Roi which was made from the subtraction of two rectangles was wrong:

importClass(Packages.ij.IJ);
importClass(Packages.ij.plugin.frame.RoiManager);
importClass(Packages.ij.ImagePlus);
importClass(Packages.ij.process.ImageProcessor);
importClass(Packages.ij.gui.Roi);
importClass(Packages.ij.gui.ShapeRoi);
importPackage(java.awt);

rm = RoiManager.getInstance();
if (rm==null) rm = new RoiManager();
imp = IJ.createImage("Untitled", "8-bit ramp", 2000, 2048, 1);
ip = imp.getProcessor();
imp.setRoi(324,366,231,120);
IJ.run("ROI Manager...", "");
rm.addRoi(imp.getRoi());
roi1 = imp.getRoi();
imp.setRoi(231,261,549,354);
rm.addRoi(imp.getRoi());
roi2 = imp.getRoi();
shpRoi1 = ShapeRoi(roi1);
shpRoi2 = ShapeRoi(roi2);
rm.addRoi(shpRoi1);
rm.addRoi(shpRoi2);
imp.show();
ip.setRoi(shpRoi1);
stats = ip.getStatistics();
area = stats.area;
IJ.log("The area for shpRo1 is: " + area);
ip.setRoi(shpRoi2);
stats = ip.getStatistics();
area = stats.area;
IJ.log("The area for shpRo2 is: " + area);
shpRoi3 = shpRoi2.not(shpRoi1);
rm.addRoi(shpRoi3);
ip.setRoi(shpRoi3);
stats = ip.getStatistics();
area = stats.area;
IJ.log("The area for shpRo3 is: " + area);

When experimenting with other fields of the ImageStatistics object, I understood that the bug is in this object.

Therefore, I decided to try to create an ImageStatistics object in another way, and this gave me the right answer:

statsA = imp.getStatistics();
areaA = statsA.area; 
IJ.log("The area for shpRo3 is: " + areaA);

This looks like a bug. I’m using a Mac, so I’m using Java 6. I found a solution that I can use, but other people might have the same issue.

Thanks,
Avital

Edit: I got the following answer from Wayne about the latest daily build:

Hi Avital,
This bug is fixed in the latest ImageJ daily build (1.51h2). Here is a
simpler version of your JavaScript that also reproduces the bug:

   ip = new ByteProcessor(2000,2048);
   roi1 = new Roi(324,366,231,120);
   roi2 = new Roi(231,261,549,354);
   shpRoi1 = new ShapeRoi(roi1);
   shpRoi2 = new ShapeRoi(roi2);
   ip.setRoi(shpRoi1);
   IJ.log("The area for shpRo1 is: " + ip.getStatistics().area);
   ip.setRoi(shpRoi2);
   IJ.log("The area for shpRo2 is: " + ip.getStatistics().area);
   shpRoi3 = shpRoi2.not(shpRoi1);
  ip.setRoi(shpRoi3);

IJ.log("The area for shpRo3 is: " + ip.getStatistics().area);

-wayne

1 Like