"Bug" in Otsu Threshold


if i use Otsu Thresholding on an image with negative pixel values i get an “out of bound” error.

is that because you are doing a log-scaling before?

some ideas how to fix this?

…the reason for the negative values is a background subtraction.

if one wants the mean or median of the background of an image to be zero, one needs pixels with negative numbers (setting them to zero would give one a wrong background intensity, namely >0…)


Hi Tischi,

A workaround for this to perform the object identification prior to the background subtraction. It sounds like you are simply subtracting a constant value from the image intensities? If this is the case, then performing the object identification should yield the same objects if the rescaling is linear. Then you can do the background subtraction and perform object measurements against the new image.

if you are doing some sort of non-linear background subtraction, then things get more complicated…


Hi Tischi,

One of our more recent trunk builds (post-r11385) incorporates an correction to this issue: any negative values in the image are now masked for Otsu, which prevents the error. The assumption is that negative values are artifacts and should be ignored, especially if there are only a few of them. Of course, this approach makes assumptions about how the images are acquired; the assumptions and the approach aren’t valid if the image values are all negative.


hey guys,

thank you for the answers!
both seem good options!