固定阈值Threshold():
double cv::threshold | ( | InputArray | src, |
OutputArray | dst, | ||
double | thresh, //阈值具体值 | ||
double | maxval, //最大值 | ||
int | type | ||
) |
阈值类型type如下,依次取值为0,1,2,3,4。
图形化的阈值描述:
自适应阈值adatpiveThreshold():
void cv::adaptiveThreshold | ( | InputArray | src, |
OutputArray | dst, | ||
double | maxValue, | ||
int | adaptiveMethod, //用于指定要使用的自适应阈值算法 | ||
int | thresholdType, | ||
int | blockSize, //用于计算阈值大小的一个像素的邻域尺寸,取值3,5,7等 | ||
double | C //减去平均或加权平均值后的常数值通常为正数 | ||
) |
#include<iostream>
#include<opencv2/opencv.hpp>
#include<opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
//定义辅助宏
#define WINDOW_NAME "程序窗口"
//全局变量声明
int g_nThresholdValue = 100;
int g_nThresholdType = 3;
Mat g_srcimg, g_dstimg, g_grayimg;
//全局函数声明
static void showhelptext();//输出帮助文字
void on_Threshold(int, void*);//回调函数
int main()
{
g_srcimg = imread("C:/Users/Administrator/Desktop/3.png");
if (g_srcimg.empty())
{
cout << "请检查图片是否存在..." << endl;
return -1;
}
cvtColor(g_srcimg, g_grayimg, COLOR_BGR2GRAY);
namedWindow(WINDOW_NAME, WINDOW_AUTOSIZE);
//创建滑动条来控制阈值
createTrackbar("参数值", WINDOW_NAME, &g_nThresholdType, 4, on_Threshold);
createTrackbar("参数值", WINDOW_NAME, &g_nThresholdValue, 255, on_Threshold);
//初始化自定义的阈值回调函数
on_Threshold(0, 0);
while (1)
{
int key;
key = waitKey(20);
if ((char)key == 27) { break; }
}
return 0;
}
//阈值回调函数
void on_Threshold(int, void*)
{
threshold(g_grayimg, g_dstimg, g_nThresholdValue, 255, g_nThresholdType);
imshow(WINDOW_NAME, g_dstimg);
}