Negative mean from ImageStatistics?

Skip to first unread message


Jan 14, 2021, 5:22:41 PM1/14/21
to BoofCV
Hey all,

Would you expect a negative mean from an image whose smallest value is 0?

BufferedImage bi =imp.getStack().getProcessor(m).convertToShortProcessor().get16BitBufferedImage();
System.out.println("Stats: "+imp.getStack().getProcessor(m).convertToShortProcessor().getStatistics() );
Stats: stats[count=2114116, mean=1775.7265202098654, min=0.0, max=65535.0]  //ok
GrayU16 input16 = ConvertBufferedImage.convertFromSingle(bi,null,GrayU16.class);
System.out.println("Mean of input image: " +ImageStatistics.mean(input) );
System.out.println("Max of input image: " +ImageStatistics.max(input) );
System.out.println("Min of input image: " +ImageStatistics.min(input) );

Mean of input image: -255.84000499499555.  //???
Max of input image: 65535
Min of input image: 0

I also scanned through the pixels manually and did not detect any negative values.

Thanks in advance.


Jan 14, 2021, 5:25:13 PM1/14/21
to BoofCV
input = input16...typo.  


Jan 14, 2021, 5:26:15 PM1/14/21
to BoofCV
Question still relevant.

Peter A

Jan 14, 2021, 6:56:02 PM1/14/21
to boofcv
I'll double check, but I'm betting the integer overflowed as it was summing. Might need to add a "safe" variant which is slower but much harder to overflow.

You received this message because you are subscribed to the Google Groups "BoofCV" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To view this discussion on the web visit

"Now, now my good man, this is no time for making enemies."    — Voltaire (1694-1778), on his deathbed in response to a priest asking that he renounce Satan.


Jan 15, 2021, 11:51:32 AM1/15/21
to BoofCV
Thanks for looking into it!

For some additional information:  I attempted to compute the image mean manually.  Retrieving the individual pixel values using the get() accessor:

int v = subInput.get(i,j);
sum = sum + v;

Sum:   -1210085007


If I retrieve them as double or long, I get a positive result.

Reply all
Reply to author
0 new messages