In the previous article , we explained the changes in brightness and contrast in image processing. In this article we will make a threshold function.
The simplest image segmentation method
Thresholding is the simplest image segmentation method.
For example, in order to segment an apple from the picture below, we use the grayscale difference between the foreground and the background to set a threshold. If the pixel is greater than this threshold, it will be represented in black, and if it is less than the threshold, it will be represented in gray.
Five threshold types
Like OpenCV, we will provide five threshold types for easy use.
The following is the waveform representation of the original image. The ordinate represents the gray value of the pixel, and the blue line is the threshold size.
Binary ThresholdingThe formula is:
The image representation is:
It can be seen that if the threshold is exceeded, it becomes the maximum value (that is, 255), otherwise it becomes the minimum value (that is, 0). We need a function to implement this function:
The formula is:
The image representation is:
This is the opposite, if it exceeds the threshold, it becomes the minimum value, otherwise it becomes the maximum value. The function implementation is:
The formula is:
The image representation is:
It can be seen that this is truncated if it exceeds the threshold. The function implementation is:
The formula is:
The image representation is:
In this case, all values smaller than the threshold are set to 0. Function implementation:
The image representation is:
Copy code
The code is as follows:
Copy code
The code is as follows: