otsuThreshold()
Find the best threshold cut-off for an image with Otsu's method.
otsuThreshold looks at an image’s gray levels and returns the cut-off that best separates the dark pixels from the light ones, using Otsu’s method.
Pass the result as max to threshold, or use it to measure an image before you decide what to do with it.
Signature
import { otsuThreshold } from "jimp";
otsuThreshold(image: JimpInstance, options?: { autoGreyscale?: boolean }): numberotsuThreshold only reads the image. It returns a whole number from 1 to 255.
The options type is exported from jimp as OtsuThresholdOptions.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
image |
JimpInstance |
required | The image to measure. |
options.autoGreyscale |
boolean |
true |
Measure each pixel’s gray value, as greyscale would compute it. With false, the red channel is used, for images that are already gray. |
Example
import { Jimp, otsuThreshold } from "jimp";
const image = await Jimp.read("scan.jpg");
const level = otsuThreshold(image);console.log(level); // e.g. 104
image.threshold({ max: level });
await image.write("scan-clean.png");image.threshold({ max: "otsu" }) does the same in one call. Use otsuThreshold when you want to see or adjust the level first.
Adjust the level
The value Otsu’s method picks is a starting point. Nudge it to keep more or less of the light areas:
const level = otsuThreshold(image);
image.threshold({ max: Math.min(255, level + 10) }); // keep a little more detailMeasure an image that’s already gray
If the image has been converted with greyscale, skip the conversion:
image.greyscale();
const level = otsuThreshold(image, { autoGreyscale: false });The result is the same as otsuThreshold(image) on the original color image.
Try it
The playground computes the level with otsuThreshold and passes it to threshold. Move offset to shift the cut-off from the level Otsu’s method picked, and upload a scan or a photo of a document to see it adapt.
image.threshold({ max: level })Pick the threshold cut-off with Otsu's method, then pass it to threshold. offset moves the cut-off from the level it picked.
import { Jimp } from "jimp"; const image = await Jimp.read("dice.png"); // Change an option to run it await image.write("output.png");
Notes
- How it’s computed.
otsuThresholdbuilds a 256-bin histogram of the gray values and tries every split into a dark group and a light group. It returns the split with the largest variance between the two groups. When several splits tie, the lowest wins. - Ready to use as
max.thresholdkeeps values belowmaxand replaces those at or above it, so the result is the first value of the light group. That’s one more than scikit-image’sthreshold_otsuand OpenCV’sTHRESH_OTSU, which report the last value of the dark group. - Alpha counts. Each pixel counts in proportion to its alpha: fully transparent pixels are ignored and a half-transparent pixel counts half as much as an opaque one.
- Nothing to split. If the image has fewer than two distinct gray values (a solid color, or no visible pixels), it returns
255, sothresholdleaves every pixel below255unchanged. - Errors. An
autoGreyscalethat isn’t a boolean throws aZodError. - Added in 2.0.