基本阈值(threshold)操作:
阈值二值化(threshold binary)
阈值反二值化(threshold binary Inverted)
截断 (truncate)
阈值取零 (threshold to zero)
阈值反取零 (threshold to zero inverted)
#include <iostream>
#include<opencv2/opencv.hpp>
#include "math.h"
using namespace std;
using namespace cv;
Mat src, gray_src,dst;//初始化一个操作对象
int threshold_value = 127;
int threshold_max = 255;
int
const char* output_title = "阈值图像";
void Threshold_Demo(int, void*);
int main(int argc, char** argv)
{
src = imread("C:/Users/JAY/Desktop/Others/tang.jpg");
if (!src.data)//判断图片是否加载进来
{
cout << "不能加载图片" << endl;
return -1;
}
namedWindow("加载的图片", WINDOW_AUTOSIZE);
namedWindow(output_title, WINDOW_AUTOSIZE);
imshow("加载的图片", src);//""内命名一致,才能显示在一个窗口
createTrackbar("阈值", output_title, &threshold_value, threshold_max, Threshold_Demo);
Threshold_Demo(0, 0);
waitKey(0);
return 0;
}
void Threshold_Demo(int, void *)
{
cvtColor(src, gray_src, CV_BGR2GRAY);//有的阈值操作只能针对单通道图像
threshold(gray_src, dst, threshold_value, threshold_max, THRESH_BINARY);//阈值二值化
imshow(output_title, dst);
}
结果