Colorado School of Mines Image and Multidimensional Signal Processing Professor William Hoff Dept of Electrical Engineering &Computer Science Colorado School of Mines Department of Electrical Engineering and Computer Science http://inside.mines.edu/~whoff/ Thresholding Colorado School of Mines Department of Electrical Engineering and Computer Science Thresholding • Label pixel as belonging to one of two (or more) classes 3 Colorado School of Mines Department of Electrical Engineering and Computer Science 4 Colorado School of Mines Department of Electrical Engineering and Computer Science Basic Global Thresholding • A heuristic algorithm 1. Select an initial estimate for threshold T 2. Segment the image using T • • G1 is all pixels with intensities > T G2 is all pixels with intensities <= T 3. Compute averages m1 and m2 for the pixels in G1 and G2 4. Let T = (m1+m2)/2 5. Repeat steps 1-4 until no more change • Note – you can use the histogram to compute the averages (very fast) 5 Colorado School of Mines Department of Electrical Engineering and Computer Science clear all, close all; I = imread('cameraman.tif'); [H,W] = size(I); figure, imshow(I); % Get histogram. hist(i) is the count of pixels with value x(i). [hist x] = imhist(I); figure, imhist(I); % Convert to probability p = hist/(H*W); t = 128; % initial guess for threshold while true disp(t); % Group 1 is all pixels with intensity > t m1 = (x > t); % create a logical mask; true where x>t % Compute mean of group 1 u1 = sum( x(m1) .* p(m1) ) / sum(p(m1)); % Group 2 is all pixels with intensity <= t m2 = (x <= t); % create a logical mask; true where x<=t % Compute mean of group 2 u2 = sum( x(m2) .* p(m2) ) / sum(p(m2)); tnew = (u1 + u2)/2; if t==tnew break; % Quit if no further changes else t = tnew; % Otherwise, this is new threshold end end Colorado School of Mines 6 Department of Electrical Engineering and Computer Science 7 Colorado School of Mines Department of Electrical Engineering and Computer Science Optimal Global Thresholding • Want to pick a threshold that minimizes the error of miss-classifying a point T E1 (T ) p2 ( z ) dz and E2 (T ) p1 ( z ) dz T Unfortunately, we usually don’t know the pdf’s p1(z), p2(z) in advance 8 Colorado School of Mines Department of Electrical Engineering and Computer Science Otsu’s Method for Global Thresholding • Choose threshold to minimize the variance within groups W2 P1 12 P2 22 Used in Matlab’s “graythresh” function 0.03 0.025 • Or equivalently, maximize the variance between groups B2 P1 m1 mG P2 m2 mG 2 0.02 2 0.015 0.01 • where 0.005 k P1 pi , P2 i 0 L 1 p i k 1 i 0 0 50 100 150 200 250 300 • mG is the global mean; mk is the mean of class k 9 Colorado School of Mines Department of Electrical Engineering and Computer Science 10 Colorado School of Mines Department of Electrical Engineering and Computer Science Matlab Examples • Images – cameraman.tif, eight.tif, pout.tif • Functions – t = graythresh(I) % Otsu algorithm – BW = im2bw(I,t); % performs thresholding – or use imtool(I,[]) and use the “adjust contrast” tool 11 Colorado School of Mines Department of Electrical Engineering and Computer Science Example Matlab image “pout.tif” The minimum within-group variance occurs at threshold 114 12 Colorado School of Mines Department of Electrical Engineering and Computer Science Multiple Thresholds • Otsu’s method can be generalized to find multiple thresholds • In the case of K classes, we maximize the between-class K variance 2 B Pk mk mG 2 k 1 where Pk pi iCk mk 1 Pk i p iCk i 13 Colorado School of Mines Department of Electrical Engineering and Computer Science Variable Thresholding • • Global thresholding may not work if illumination or reflectance varies across image Can partition image into subimages, compute threshold for each 14 Colorado School of Mines Department of Electrical Engineering and Computer Science “Split-and-merge” Segmentation • Find a partitioning of connected regions such that each region has a consistent property (eg, color or texture) within it • “Split and merge”: – Split regions that are inconsistent – Merge adjoining regions that are mutually consistent – Repeat until no further change is possible 15 Colorado School of Mines Department of Electrical Engineering and Computer Science Example Consistency property: Q = TRUE if > a and m < b 16 Colorado School of Mines Department of Electrical Engineering and Computer Science Quadtree Representation • Recursively divide image into four quadrants • Stop dividing when each quadrant is consistent • A very concise and efficient representation 17 Colorado School of Mines Department of Electrical Engineering and Computer Science Summary / Questions • Thresholding is a way to segment an image, such that each pixel is labeled as belonging to one of (typically) two classes. • A global thresholding algorithm chooses a single threshold, based on the image histogram across the entire image. • What property does the Otsu thresholding algorithm seek to minimize (or maximize)? 18 Colorado School of Mines Department of Electrical Engineering and Computer Science