thresholdcv
Applies a threshold to each element of the given image, handle, resulting in a grayscale image, R, whose pixels represent the elements exceeding the threshold.
Syntax
[computedthreshold, R] = thresholdcv(handle, threshold, max, type)
Inputs
- handle
- Handle of an image.
- threshold
- Real vector of [width height] representing the dimensions of R.
- fx
- Threshold value.
- max
- Maximum value to use with type value of 8 or 16.
- type
- Threshold type. Valid values are:
- 0
- R(x,y) = max if handle(x,y) > threshold and 0 otherwise.
- 1
- R(x,y) = 0 if handle(x,y) > threshold and max otherwise.
- 2
- R(x,y) = threshold if handle(x,y) > threshold and handle(x,y) otherwise.
- 3
- R(x,y) = handle(x,y) if handle(x,y) > threshold and 0 otherwise.
- 4
- R(x,y) = 0 if handle(x,y) > threshold and handle(x,y) otherwise.
- 8
- Uses Otsu algorithm for optimal threshold.
- 16
- Uses Triangle algorithm for optimal threshold.
Outputs
- computedthreshold
- Computed threshold value.
- R
- Handle of the resized image.
Example
Apply a threshold to an image:
handle = imreadcv('bird4.jpg', 0);
[computedthreshold, mask] = thresholdcv(handle, 10, 255, 0);
Apply a threshold to an image containing numbers written in grayscale equal to the number itself with threshold equals to zero and maximum value equals to 255 (white):
src = imreadcv('thresholdcv_fig1.png');
figure(1);
imshowcv(src);
thresh = 0;
maxValue = 255;
[computedthreshold, mask] = thresholdcv(src, thresh, maxValue, 0); %Binary threshold
figure(2);
imshowcv(mask);
Apply a threshold to the same image as described above containing numbers with threshold equals to 127, which removes all numbers less than or equal to 127:
src = imreadcv('thresholdcv_fig1.png');
figure(1);
imshowcv(src);
thresh = 127;
maxValue = 255;
[computedthreshold, mask] = thresholdcv(src, thresh, maxValue, 0); %Binary threshold
figure(2);
imshowcv(mask);
Apply a threshold as described in the previous example with a maximum value of 150, to set the value of the thresholded regions to 150:
src = imreadcv('thresholdcv_fig1.png');
figure(1);
imshowcv(src);
thresh = 127;
maxValue = 150;
[computedthreshold, mask] = thresholdcv(src, thresh, maxValue, 0); %Binary threshold
figure(2);
imshowcv(mask);
Apply a threshold to the same image as described above with threshold equals to zero and maximum value equals to 255 (white), but using the inverse binary thresholding:
src = imreadcv('thresholdcv_fig1.png');
figure(1);
imshowcv(src);
thresh = 0;
maxValue = 255;
[computedthreshold, mask] = thresholdcv(src, thresh, maxValue, 1); %Inverse binary threshold
figure(2);
imshowcv(mask);
Apply a threshold to the same image as described above with threshold equals to 150 and maximum value equals to 255 (white), using the truncate thresholding. All values above the threshold are set to the threshold value and all values below remain unchanged:
src = imreadcv('thresholdcv_fig1.png');
figure(1);
imshowcv(src);
thresh = 150;
maxValue = 255;
[computedthreshold, mask] = thresholdcv(src, thresh, maxValue, 2); %Inverse binary threshold
figure(2);
imshowcv(mask);