Image Thresholding (Otsu's Method)
Automatic threshold selection for binary segmentation. Maximizes inter-class variance. Optimal threshold without user input.
Visualization
Interactive visualization for Image Thresholding (Otsu's Method)
Thresholding:
- • Binary segmentation
Interactive visualization with step-by-step execution
Implementation
1function otsuThreshold(image: number[][]): number {
2 const histogram = new Array(256).fill(0);
3 const total = image.length * image[0].length;
4
5 // Build histogram
6 for (const row of image) {
7 for (const val of row) {
8 histogram[Math.floor(val)]++;
9 }
10 }
11
12 let sum = 0;
13 for (let i = 0; i < 256; i++) sum += i * histogram[i];
14
15 let sumB = 0, wB = 0, wF = 0;
16 let maxVar = 0, threshold = 0;
17
18 for (let t = 0; t < 256; t++) {
19 wB += histogram[t];
20 if (wB === 0) continue;
21
22 wF = total - wB;
23 if (wF === 0) break;
24
25 sumB += t * histogram[t];
26 const mB = sumB / wB;
27 const mF = (sum - sumB) / wF;
28
29 const varBetween = wB * wF * (mB - mF) ** 2;
30
31 if (varBetween > maxVar) {
32 maxVar = varBetween;
33 threshold = t;
34 }
35 }
36
37 return threshold;
38}Deep Dive
Theoretical Foundation
Otsu's method: find threshold maximizing between-class variance σ²_B = w₀(μ₀-μ)² + w₁(μ₁-μ)². w = class weight, μ = mean intensity. Equivalent to minimizing within-class variance.
Complexity
Time
O(w×h + L)
O(w×h + L)
O(w×h + L)
Space
O(L)
Applications
Industry Use
Document image binarization
Object segmentation from background
Medical image analysis
Quality control in manufacturing
Barcode and QR code processing
Preprocessing for OCR systems
Automated microscopy analysis
Use Cases
Related Algorithms
A* Search Algorithm
Informed search algorithm combining best-first search with Dijkstra's algorithm using heuristics. Widely used in pathfinding and graph traversal, A* is optimal and complete when using admissible heuristic. Used in games, GPS navigation, and robotics. Invented by Peter Hart, Nils Nilsson, and Bertram Raphael in 1968.
Convex Hull (Graham Scan)
Find smallest convex polygon containing all points. Graham Scan invented by Ronald Graham in 1972, runs in O(n log n). Essential in computational geometry, computer graphics, and pattern recognition.
Line Segment Intersection
Determine if two line segments intersect. Fundamental geometric primitive used in graphics, CAD, GIS. Uses orientation and collinearity tests.
Caesar Cipher
The Caesar Cipher is one of the oldest and simplest encryption techniques, named after Julius Caesar who used it to protect military messages around 100 BC. It works by shifting each letter in the plaintext by a fixed number of positions down the alphabet. For example, with a shift of 3, A becomes D, B becomes E, and so on. Despite being used for over 2000 years, it's extremely weak by modern standards with only 25 possible keys, making it trivially breakable by brute force or frequency analysis.