Multi-level Otsu¶
| Fact | Value |
|---|---|
| Also called | Multi-Otsu thresholding, Multilevel Otsu thresholding |
| Status | Implemented and tested |
| Family | Segmentation and regions |
| Engine | Science engine |
| Type | Nonlinear, statistical, multilevel thresholding |
| Output | Labels |
| Operator shown as | algorithm (exact) |
| Support radius | global |
| Code | engine/src/pixelnexus_engine/filters/segmentation.py, multi_otsu |
| Operator code | engine/src/pixelnexus_engine/filters/segmentation.py, multi_otsu_representation |
| Packages | scikit-image |
Overview¶
Multi-level Otsu splits the gray levels of the image into several classes, such as dark, middle and bright. It picks the thresholds that make the classes as different as possible, the same rule Otsu's method uses for two classes. Liao, Chen and Chung showed how to find them quickly. The classes go to the Features panel with their area, centroid and mean brightness, and are drawn over the image in their label colors. Click a row number to make a class the selection. The filter can also show the classes as gray levels. It runs in the PixelNexus engine, with scikit-image.
How it works¶
- Take the 8-bit luma and its histogram of 256 bins.
- Find the M - 1 thresholds with the largest variance between classes.
- Give each pixel the class of its gray level.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(t_j\): The thresholds
- \(\omega_k, \mu_k\): The share and mean gray level of class k
- \(M\): The number of classes
The histogram of the luma is split by M - 1 thresholds into M classes. Class k has the share omega_k of the pixels and the mean gray level mu_k, and mu is the mean of the whole image. The thresholds are those that make the variance between the classes largest, as Otsu did for two classes. Liao and coworkers showed how to compute this quickly with lookup tables, but the search still tries every set of thresholds, so its cost grows fast with M. A pixel belongs to class c when its gray level is above exactly c thresholds. A level equal to a threshold belongs to the class below, as in Otsu's method. The classes are not split into connected parts, so one class can have many separate pieces.
Otsu's criterion looks only at the histogram. A split is good when the classes have means far from the mean of the whole image, weighted by their size. For M classes the variance between classes sums over all of them, and the best thresholds make it largest.
Maximizing the variance between classes is the same as minimizing the variance within them, since the two add up to the fixed total variance. So the classes are as tight as possible around their means.
The search tries every set of thresholds. Liao and coworkers made each try cheap with lookup tables of the shares and means of every range of gray levels. But the number of sets grows like L to the power M - 1, so more than 4 or 5 classes become slow.
Kernel or operator¶
There is no kernel. The Inspector asks the engine for the stages with the current settings.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Output output |
Classes | Classes, Gray level for each class, Boundaries over the image, Mean color of each class | The classes as a label map, drawn over the image and listed in the Features panel. Or evenly spaced gray levels from black to white, the class boundaries in yellow over the image, or each class filled with its mean color. |
Classes classes |
3 | 2 to 5 | The number of classes, one more than the number of thresholds. scikit-image uses 3. |
Implementation¶
multi_otsu in engine/src/pixelnexus_engine/filters/segmentation.py calls skimage.filters.threshold_multiotsu on the 8-bit luma with 256 bins. Each threshold is a gray level that belongs to the class below it, as in Otsu's method, where only pixels above the threshold are foreground. So a pixel's class is the number of thresholds strictly below its gray level, numpy.digitize with right=True, plus 1. The example in scikit-image's documentation uses numpy.digitize without right=True, which puts a pixel equal to a threshold in the class above. On an image of three flat levels, its thresholds are the two lower levels themselves. That version would move both lower levels up one class, so the middle level would join the top one. The gray levels output spreads the classes evenly from black to white.
The regions output is a label map. The engine sends it with a table of the regions, and the studio draws it over the image. See the science engine for how label maps travel.
Worked example¶
| Classes | Thresholds | Share of pixels per class |
|---|---|---|
| 2 | 101 | 0.69, 0.31 |
| 3 | 85, 162 | 0.68, 0.22, 0.11 |
| 4 | 52, 90, 163 | 0.37, 0.31, 0.21, 0.11 |
| 5 | 51, 85, 119, 168 | 0.35, 0.33, 0.11, 0.11, 0.10 |
The test image of the figure. The image was made with three levels, about 40 to 60, 120 and 210, before the blur and the noise.
With 3 classes, the thresholds 85 and 162 split the ground, the squares and the disks. With 2 classes, the squares and disks share a class. With 4 classes, the extra threshold cuts the ground in two along its brightness ramp, since the ground is the largest and widest group of levels. With 5 classes, one more threshold falls at 119, right inside the squares, and splits them by their noise. More classes do not find more objects, they split what is wide in the histogram.
Complexity¶
- Time: \(O(N + L^(M - 1))\)
- Memory: \(O(N + L^2)\)
N is the number of pixels and L = 256 the number of gray levels. The search over thresholds dominates for more classes.
Artifacts and limitations¶
Limitations
- Only the gray levels count, not where pixels are.
- The search becomes slow beyond 4 or 5 classes.
Artifacts
- Speckle where noise crosses a threshold.
Tests¶
The classes must follow scikit-image's thresholds with a level equal to a threshold in the class below, for 2, 3 and 4 classes. On three flat levels of 20, 120 and 230, where scikit-image's thresholds are 20 and 120, each level must get its own class. The gray levels output must use 0, 128 and 255. The boundaries and mean colors outputs must match mark_boundaries and the rounded means.
References¶
- Otsu, N. A threshold selection method from gray-level histograms. IEEE Transactions on Systems, Man, and Cybernetics 9(1), 62 to 66, 1979. DOI 10.1109/TSMC.1979.4310076.
- Liao, P.-S., Chen, T.-S., and Chung, P.-C. A fast algorithm for multilevel thresholding. Journal of Information Science and Engineering 17(5), 713 to 727, 2001. DOI 10.6688/JISE.2001.17.5.1.
- scikit-image documentation, skimage.filters.threshold_multiotsu.