OpenCV基础课程笔记15基本阈值操作

写在前面

阈值二值化操作肯定在灰度图像使用。
阈值类型
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);
}

运行结果

在这里插入图片描述

posted @ 2020-08-27 14:26  爱和九九  阅读(33)  评论(0)    收藏  举报