写在前面
阈值二值化操作肯定在灰度图像使用。
阈值类型
THRESH_BINARY
THRESH_BINARY_INV //取反
THRESH_TRUNC 超过阈值的,等于阈值
THRESH_TOZERO 小于阈值的,等于0
THRESH_TOZERO_INV
生成阈值方式
THRESH_OTSU
THRESH_TRIANGLE
代码
#include<iostream>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;
Mat src = imread("A:\\专用\\TestForTheCV\\tae.jpg");
int threshold_value = 127;
int threshold_max = 255;
const char* output = "dst image";
Mat dst;
void thresholdDemo(int ,void*);
int main() {
imshow("原图", src);
namedWindow(output, CV_WINDOW_AUTOSIZE);
createTrackbar("Threshold Value is:", output, &threshold_value, threshold_max, thresholdDemo);
waitKey(0);
return 0;
}
void thresholdDemo(int, void*) {
cvtColor(src, dst, CV_BGR2GRAY);
//threshold(dst, dst, threshold_value, threshold_max, THRESH_TRUNC);
threshold(dst, dst, 0, 255,THRESH_TRIANGLE);
imshow(output, dst);
}