threshold
Usage
Used for image binarization. It compares the pixel values in an image with the specified threshold and creates a binary image that contains only two values based on the comparison result.
Interface
1 | double threshold(InputArray src, OutputArray dst, double thresh, double maxval, int type); |
Parameters
Parameter |
Description |
Value Range |
Input/Output |
|---|---|---|---|
src |
Input image. |
Not null |
Input |
dst |
Output image. |
Mat class |
Output |
thresh |
Threshold. |
(-inf, inf) |
Input |
maxval |
Maximum value to be assigned to pixels that satisfy the threshold condition. |
(-inf, inf) |
Input |
type |
Threshold type. |
THRESH_BINARY, THRESH_BINARY_INV, THRESH_TRUNC, THRESH_TOZERO, and THRESH_TOZERO_INV |
Input |
Return Value
- Threshold
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include <opencv2/opencv.hpp> #include <iostream> int main() { // Create a 5x5 grayscale image. cv::Mat src = (cv::Mat_<uint8_t>(5, 5) << 50, 50, 50, 50, 50, 50, 150, 150, 150, 50, 50, 150, 200, 150, 50, 50, 150, 150, 150, 50, 50, 50, 50, 50, 50); // Apply the binarization threshold. Set the threshold to 100 and the maximum value to 255. cv::Mat dst; cv::threshold(src, dst, 100, 255, cv::THRESH_BINARY); // Output image std::cout<< dst << std::endl; return 0; } |
Output:
1 2 3 4 5 | [[ 0 0 0 0 0] [ 0 255 255 255 0] [ 0 255 255 255 0] [ 0 255 255 255 0] [ 0 0 0 0 0]] |
Parent topic: Interface Definition