Table of contents
The content of the last class (the author still encourages students to learn in order): [OpenCV in C++] Lesson 8 - Common Operations of OpenCV Images (5): Image Morphology - Image Pyramid (Gaussian pyramid, Laplacian pyramid) and upward The use and principle of (down)sampling
1. The concept of threshold (thresh)
- First of all, as the name implies, " threshold " is a range or limit , so "threshold" is a certain limit value (this value has a certain mathematical meaning, that is, " critical value ", for example, the height of a vehicle height limit pole is a threshold value, It cannot be exceeded; or children under 1.1 meters are free of charge, and children over 1.1 meters will be charged.)
- Secondly, the threshold in graphics often refers to a certain pixel value that you want to set.
2. The use of threshold in graphics
The image thresholding process is used in the field of image separation. According to a certain threshold, the image is separated to obtain the region of interest.
Of course, based on this idea, it can also be applied to deeper and higher fields, such as medical image analysis and other fields. In short, its application value is very high. Just like the content of the previous chapter.
3. The function and operation of the threshold
3.1 Threshold operations that can be performed in OpenCV
Next, we use images and function formulas to express its principles and functions as much as possible.
Note that in the image below, the red solid line is the set threshold!
-
- Mode introduction:
- Explanation:
When the pixel value of the source image is greater than the threshold , these pixels in the processed image take the latest maximum pixel as the result. If the pixel value of the source image is less than or equal to the threshold , the pixel value in the result image is 0. - Graphic:
-
Pixel value data before processing:
-
Processed pixel value data:
-
- Mode introduction:
-
- Mode introduction:
- Explanation: Just the opposite of
pixel binary mode! That is: when the pixel value of the source image is greater than the threshold , the corresponding pixel value of the result image is 0; when the pixel value of the source image is less than or equal to the threshold , the corresponding threshold of the result image takes the MaxValue (maximum value) set in advance. - Graphic:
- Pixel value data before processing:
- Processed pixel value data:
- Pixel value data before processing:
- Mode introduction:
-
- Explanation:
In a word, set the values of all pixels higher than the threshold to be equal to the threshold, and the remaining pixel values remain unchanged . - Graphic:
- Image data before processing:
- Processed image data:
- Image data before processing:
- Explanation:
-
-
Mode introduction:
-
Explanation: Pixels
in the source image below or equal to the threshold are set to 0 . -
Graphic:
- Image data before processing:
- Processed image data:
- Image data before processing:
-
-
- Mode introduction:
- Explanation: Reverse the data of the 0 threshold value above, that is, when the pixel value of the source image is greater than the threshold value, the value of the corresponding pixel position of the generated image is 0; otherwise, the original pixel value is retained.
- Graphic:
- The data of the image before processing:
- The data of the processed image:
- The data of the image before processing:
- Mode introduction:
3.2 Operation examples
3.2.1 Threshold() function introduction
- Function prototype:
double cv::threshold(
InputArray src,
OutputArray dst,
double thresh,
double maxval,
int type
)
- Parameter explanation:
- src : original image
- dst: processed image
- thresh: threshold
- maxval: The maximum value of the pixel, which is the MaxValue mentioned above
- type: The method of threshold processing. Here, the threshold modes mentioned above are corresponding to the following integers:
- 0: Binary, binary mode
- 1: Binary Inverted, binary inversion
- 2: Threshold Truncated, threshold truncated
- 3: Threshold to Zero, zero threshold
- 4: Threshold to Zero Inverted, zero threshold inversion
3.2.2 Examples
Reference source code: sample source code
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
using namespace cv;
int main(void)
{
Mat srcImg = imread("/home/aelx-chen/demo.jpg");
Mat grayImg;
Mat dstImg;
cvtColor(srcImg, grayImg, COLOR_BGR2GRAY);
threshold(grayImg, dstImg, 90, 255, 1);
/*
将原图像,设置阈值为90,最大阈值为255,采用二进制方式处理
*/
imshow("source image",srcImg);
imshow("gray image",grayImg);
imshow("destination image",dstImg);
waitKey(0);
return 0;
}