opencv study notes (c) substantially thresholding operation

1. What is the threshold operation?

Here Insert Picture Description
We need to extract the part from one image, it should be compared with the gray value of each pixel of the image and a selected threshold value, and make the appropriate determination. (Note: Select the threshold value depending on the particular problem that is: there may be an object different gray scale values ​​in the different images.)

2. thresholding type:

OpenCV provides threshold function: threshold () function . Function operates: The threshold value and the selected threshold value types, the pixel values of the upper and lower thresholds 255 set to 0 or to achieve effects binarization.

函数原型: void threshold(InputArray src,OutputArray dst,double thresh,double maxval,int type)

Parameters :

src: source image input
dst: an output image
thresh: threshold magnitude
maxval: maximum value of the fifth parameter
type:Thresholding type, Can be filled with digital numbers corresponding to 1-5 different types of thresholds:.
Here Insert Picture Description
1.THRESH_BINARY
Here Insert Picture DescriptionHere Insert Picture Description
in the use of the threshold value type, the first to select a specific threshold amount, for example: 125, so that a new threshold value generating rules construed to be larger than the pixel 125 set to the maximum gradation value (e.g., 8-bit grayscale value 255 is the maximum), the grayscale pixel value is less than the gradation value 125 is set to 0.

2.THRESH_BINARY_INV
Here Insert Picture DescriptionHere Insert Picture Description
corresponds to the first threshold value of a type of inverted.

3.THRESH_TRUNC
Here Insert Picture Description Here Insert Picture Description
likewise need to select a first threshold value, the image pixel is greater than the threshold value is set as the threshold value is less than the threshold remains unchanged. (Example: threshold of 125, 125 that is less than the threshold value does not change, the gradation value greater than 125 (230) on a pixel set as the threshold value).

4.THRESH_TOZERO
Here Insert Picture DescriptionHere Insert Picture Description
first select a threshold value, then the image processed as follows: gray value greater than 1 pixel without any change of the threshold value; 2 pixel grayscale values smaller than the threshold value, the gray values of all variants 0.

5.THRESH_TOZERO_INV
Here Insert Picture DescriptionHere Insert Picture Description
the fourth determination threshold value of the opposite types.

3. Foundation thresholding operation

Implementation code :

#include <opencv2/opencv.hpp>
#include<opencv2\imgproc\imgproc.hpp>
#include <iostream>
#include <math.h>
using namespace cv;
Mat src, gray_src, dst;
int threshold_value = 127;
int threshold_max = 255;
int type_value = 1;
int type_max = 4;
const char* output_title = "binary image";
void Threshold_Demo(int, void*);
int main(int argc, char** argv) {
 src = imread("C:/Users/86159/Desktop/yasina.jpg");
 
 namedWindow("input image", WINDOW_AUTOSIZE);
 moveWindow("input image",50,0);
 namedWindow(output_title, WINDOW_AUTOSIZE);
 moveWindow(output_title, 800, 0);
 imshow("input image", src);
 
 createTrackbar("Threshold Value:", output_title, &threshold_value, threshold_max, Threshold_Demo);		//创建阈值滑动条
 createTrackbar("Type Value:", output_title, &type_value, type_max, Threshold_Demo);
 Threshold_Demo(0, 0);		//创建阈值化类型滑动条
 waitKey(0);
 return 0;
}
void Threshold_Demo(int, void*) {
 cvtColor(src, gray_src, COLOR_BGR2GRAY);
 threshold(src, dst, threshold_value, threshold_max,  type_value);	//阈值化函数
 imshow(output_title, dst);
}

Results are as follows:
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

Released three original articles · won praise 3 · Views 201

Guess you like

Origin blog.csdn.net/Rosen_er/article/details/104094454