Given your threshold k
, you can simply use a binary operation to compute your thresholded image.
Simply put, if I
is your image, the thresholded image can be computed like so:
outThreshold = I >= k;
You can also use outThreshold = im2bw(I,k);
that will do the same operation (thanks @Daniel!). It essentially performs what I just described above.
In any case, this returns a binary map where pixels that have an intensity that is greater than or equal to k
get set to true / 1
while those that are less than k
are set to false / 0
. Here's an example using graythresh
. MATLAB has a built-in threshold finding algorithm using Otsu by graythresh
.
I = imread('cameraman.tif'); %// Built-in to MATLAB - A grayscale image
k = 255*graythresh(im); %// Output is scaled between 0 - 1.
%//Multiply by 255 to get 8-bit intensity
subplot(1,2,1);
imshow(I);
outThresh = I >= k;
subplot(1,2,2);
imshow(outThresh);
As such, you get this figure:
![enter image description here]()
Locations that are white denote those pixels that surpass the threshold while locations that are black are those that didn't.
From your comments (you really should have just updated your post), you wish to seek a method that performs multi-thresholding. This is still quite simple. Supposing you had an array of thresholds like so:
thresholds = [32 96 128 192];
What this is saying is that there are five regions we need to consider:
- Pixels with intensity values between 0 to 31
- Pixels with intensity values between 32 to 95
- Pixels with intensity values between 96 to 127
- Pixels with intensity values between 128 to 191
- Pixels with intensity values between 192 to 255
Note that for this example, the upper end is exclusive. As such, we don't include the end interval, but we do include this number in the next interval and we assign this to be the lower end of the interval.
We can simply use a for
loop and create a map variable that finds all pixels within each range and sets them to an ID number. Let's give these all an ID in increasing order, so the first region gets assigned an ID of 1, the next gets and ID of 2 and so on. This is the code that would do it:
map = zeros(size(I));
thresholdArray = [0 thresholds 255];
for k = 1 : numel(thresholdArray)-1
lower = thresholdArray(k);
upper = thresholdArray(k+1);
map(I >= lower & I < upper) = k;
end
%// Rescale to 0 - 255
outMap = uint8(255*(map - min(map(:))) / (max(map(:)) - min(map(:))));
figure;
subplot(2,2,1);
imshow(I);
subplot(2,2,2);
imhist(I);
subplot(2,2,3);
imshow(outMap);
subplot(2,2,4);
imhist(outMap);
This is what the output looks like:
![enter image description here]()
The code performs multi-thresholding, as well as displaying the original image with its histogram as well as the thresholded image with its histogram. Note that I have rescaled the output map so that it goes from 0 to 255. As such, each class gets assigned a value that is a multiple of (256 / numThresholds)
. In this case, it is 64.