In this week, different types of thresholding is implemented as a method of segmentation.
|Original||Global Thresholding||Adaptive Thresholding|
Global thresholding is an unsophisicated segmentation technique that can seperate between the two peaks in the image. Previously, this was implemented using grayscale image.
blackWhite = double(contrastStretched > threshold); This would result in a black and white output (See: main.m).
This is then further extended for colored images:
threshold = mean(contrastStretched(:)); globalThresholding = image; % Loop through each pixel for global thresholding for x = 1:width for y = 1: height if mean(contrastStretched(y, x, :)) > threshold % Wipe out the pixel if larger than threshold globalThresholding(y, x, :) = 1; end end end
The threshold is compared with the mean of the red, green, and blue values of a pixel.
On the other hand, Adaptive thresholding is a form of thresholding that takes into account spatial variations in illumination.
adaptiveThresholding = image; half = 3; % Loop through the image to apply threshold to each pixel for x = half + 1:width - half for y = half + 1: height - half area = contrastStretched(y-half:y+half, x-half:x+half, :); threshold = mean(area(:)) - 0.05; if mean(contrastStretched(y, x, :)) > threshold % Wipe out the pixel if larger than threshold adaptiveThresholding(y, x, :) = 1; end end end
The code above uses the cookie-cutter technique to scan through the entire image using a tiny 7x7 area cookie to decide its threshold, and thus the name adaptive thresholding.