opencv-basic threshold operation

Image threshold (threshold)

Threshold means limit, and the threshold is the limit value.
such as

  • A pile of apples with a diameter greater than 7 cm is considered qualified, and less than 7 cm is unqualified
  • Test scores, a score greater than 60 is considered a pass, and a score less than 60 fails

Through the above classification, we divide all variables into two categories, and the classification standard, that is, the limit, is the threshold.

What are the thresholds

  • (1) Threshold binary.

  • (2) Threshold binary Inverted

  • (3) truncate (truncate)

  • (4) Threshold to zero

  • (5) Threshold to zero inverted

Threshold binarization

Threshold binarization is to divide all cases into two cases by threshold

  • 1. If greater than the threshold, take the maximum value.

  • 2. The threshold is less than or equal to 0.

Insert picture description here

Insert picture description here
For the left picture, if we perform the binarization operation, then all the lines above the blue line become the maximum value, and the lines below the blue line all become 0, which is the right picture

Threshold debinarization

Threshold de-binarization also divides all cases into two cases by threshold, but it is the opposite of binarization:

  • 1. If greater than the threshold, take 0.

  • 2. Take the maximum value less than or equal to the threshold.

Insert picture description here

Insert picture description here
For the left picture, if we perform the de-binarization operation, then all the lines below the blue line become the maximum value, and the lines above the blue line all become 0, that is, as shown in the right picture

Truncated

Truncation is relatively simple to understand from the literal meaning. You can think about the gardener who is building flowers and plants. For the sake of beauty, all trees will be cut to the same height, that is, the ones larger than the threshold will be removed:

  • 1. If it is greater than the threshold, take the threshold.

  • 2. Take the original value less than or equal to the threshold.

If expressed by a formula, it is the following formula.
Insert picture description here
Insert picture description here
For the left picture, if we perform a truncation operation, it is as shown in the right picture.

Threshold value is zero

The threshold value of zero is equivalent to setting a limit. Everyone considers the competition. If the competition advances, the result is valid and the competition continues. If the result is not up to the standard, it will be regarded as a loss and the result will be reset to zero, that is, the value less than the threshold will be reset to zero.

  • 1. If it is greater than the threshold, take the original value.

  • 2. The threshold is less than or equal to 0.

Insert picture description here
Insert picture description here
For the left image, we get the right image after the threshold is zero

Threshold is reversed to zero

The threshold value is reversed to zero and the threshold value is zero, that is, the value greater than the threshold value will be zero:

  • 1. If it is greater than the threshold, take 0.

  • 2. Take the original value less than or equal to the threshold.

If you use a formula to express it, it is the following formula.
Insert picture description here
Insert picture description here
For the left picture, the threshold value is reversed to zero, and the threshold value above is set to 0, as shown in the right picture

to sum up

Insert picture description here
How to find the threshold, opencv provides two methods

  • THERSH_OTSU Japanese THERSH_OTSU

API

double threshold( 
    InputArray src, 
    OutputArray dst,                         
    double thresh, 
    double maxval,
    int type
);
  • (1) src of InputArray type, input image, multi-channel, 8-bit or 32-bit floating point.

  • (2) dst of the OutputArray type, the output image, the size, type, and number of channels of the image are the same as the input image.

  • (3) Thresh of double type, threshold.

  • (4) Double type maxval, the maximum value when using THRESH_BINARY and THRESH_BINARY_INV threshold types.

  • (5) Type of int type, threshold type, for values ​​see: cv::ThresholdTypes.

Code display

Combine the slider to write code

We control the threshold by adjusting the trackbar and realize the switching back and forth of the threshold type

#include<iostream>
#include<opencv2/opencv.hpp>

using namespace std;
using namespace cv;

//全局变量
Mat src, dst;
int thresholdValue = 127;
int thresholdMax = 255;
int typeValue = 2;
int typeMax = 4;
const char* output_title = "binary img";//窗口名称“二值化图像”

void Threshold_Demo(int, void*);//二值化演示函数

								//主函数入口
int main()
{
    
    
	src = imread("C:/Users/86176/Pictures/pics/lena(1).tiff");
	if (!src.data)
	{
    
    
		cout << "could not load image..." << endl;
		return -1;
	}
	imshow("show image", src);
	namedWindow(output_title, CV_WINDOW_AUTOSIZE);
	createTrackbar("Threshold Value", output_title, &thresholdValue, thresholdMax, Threshold_Demo);//动态调整
	createTrackbar("Type Value", output_title, &typeValue, typeMax, Threshold_Demo);

	Threshold_Demo(0, 0);
	imshow(output_title, dst);
	waitKey(0);
	return 0;
}
void Threshold_Demo(int, void*)
{
    
    
	threshold(src, dst, thresholdValue, thresholdMax, typeValue);

	imshow(output_title, dst);

}

effect

Opencv basic threshold operation

to sum up

thresholdType = 0
threshold binarization

  • The threshold is set to 0, all even pixels are greater than 0, and the maximum value is 255, which is white
  • The threshold is set to 255, all pixels are less than 255, all are 0, which is black

thresholdType = 1
threshold de-binarization

  • The threshold is set to 0, all even pixels are greater than 0, and the minimum value is 0, which is black
  • The threshold is set to 255, all pixels are less than 255, and the maximum value is 255, which is white

thresholdType = 2
truncation

  • The threshold is set to 0, all even pixels are greater than 0, and the threshold is 0, which is black
  • The threshold is set to 255, all pixels are less than or equal to 255, all take their original values, and the image remains unchanged

thresholdType = 3 The
threshold is 0

  • The threshold is set to 0, all even pixels are greater than 0, all take the original value, and the image remains unchanged
  • The threshold is set to 255, all pixels are less than or equal to 255, all are 0, the image is black

thresholdType = 4 The
threshold is reversed to 0

  • The threshold is set to 0, all even pixels are greater than 0, and all are set to 0, the image is black
  • The threshold is set to 255, all pixels are less than or equal to 255, all take the original value, and the image remains unchanged

Basic threshold operation can be used for image dewatermarking

Guess you like

Origin blog.csdn.net/qq_28258885/article/details/112789014