OpenCV-阈值
阈值:图像分割的标尺
阈值的类型
**
-
阈值二值化(threshold binary)
高于阈值的部分取最大值,低于阈值部分取0; -
阈值反二值化(threshold binary Inverted)
高于阈值的部分取0,低于阈值部分取最大值; -
截断(truncate)
高于阈值的部分取阈值,低于阈值部分不变; -
阈值取零 (threshold to zero)
高于阈值的部分不变,低于阈值部分取0; -
阈值反取零 (threshold to zero Inverted)
高于阈值的部分取0,低于阈值部分取不变;
**
#include<opencv2/opencv.hpp>
#include<iostream>
#include<opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
Mat src, dst, gray_src;
int threshold_value = 127;
int threshold_max = 255;
int type_value = 2;
int type_max = 4;
const char* output_title = "OUTPUT";
void threshold_Demo(int, void*);
int main(int argc, char ** argv)
{
src = imread("E://VS-pro//images//3.jpg");
imshow("原图", src);
namedWindow(output_title, WINDOW_AUTOSIZE);
//控制阈值大小
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);
imshow("灰度图", gray_src);
threshold(gray_src, dst, threshold_value, threshold_max, type_value);
imshow(output_title, dst);
}