图像阈值
简单了解图像阈值操作
二值化 THRESH_BINARY
大于阈值的赋值255,小于阈值的赋值0
反二值化 THRESH_BINARY_INV
小于阈值的赋值255,大于阈值的赋值0
截断 THRESH_TRUNC
大于阈值的等于阈值,小于阈值的不变
阈值取零 THRESH_TOZERO
大于阈值的不变,小于阈值的赋值0
阈值反取零 THRESH_TOZERO_INV
大于阈值的赋值0,小于阈值的不变
自动阈值
THRESH_OTSU 待细究
THRESH_TRIANGLE 待细究
代码
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
Mat src, gray_src, dst;
int threshold_value = 127;
int threshold_max = 255;
int loc = 0;
int i[5] = { 0,1,2,3,4 };//几种阈值处理的对应编码是 0-4,具体可以看源码的枚举。
void Threshold_d(int, void*);
int main()
{
src = imread("girl6.jpg");
if (!src.data)
{
printf("Wrong with Loading img!\n");
return -1;
}
namedWindow("out", WINDOW_AUTOSIZE);
createTrackbar("Threshold", "out", &threshold_value, threshold_max, Threshold_d);
createTrackbar("Type", "out", &loc , 4, Threshold_d);
waitKey(0);
return -1;
}
void Threshold_d(int, void*)
{
cvtColor(src, gray_src, COLOR_BGR2GRAY);//8位单通道灰度图像 CV_8UC1
threshold(gray_src, dst, threshold_value, threshold_max, i[loc]);
imshow("out", dst);
}