一、固定阈值化Threshold()函数
double threshold( InputArray src, OutputArray dst, double thresh, double maxval, int type );
二、自适应阈值操作:adaptiveThreshold()函数
void adaptiveThreshold( InputArray src, OutputArray dst, double maxValue, int adaptiveMethod, int thresholdType, int blockSize, double C );
1 #include<opencv2/opencv.hpp> 2 #include<iostream> 3 4 using namespace cv; 5 using namespace std; 6 7 #define WINDOW_NAME "【程序窗口】" 8 9 int g_nThresholdValue = 100; 10 int g_nThresholdType = 3; 11 Mat g_srcImage, g_grayImage, g_dstImage; 12 13 void on_Threshold(int, void *); 14 15 int main(int argc, char** argv) { 16 g_srcImage = imread("C:\Users\Nelsoner\Desktop\Camera Roll\05.jpg"); 17 18 //存留一份源图的灰度图 19 cvtColor(g_srcImage, g_grayImage, COLOR_RGB2GRAY); 20 21 namedWindow(WINDOW_NAME); 22 imshow(WINDOW_NAME, g_srcImage); 23 24 createTrackbar("模式", WINDOW_NAME, &g_nThresholdType, 4, on_Threshold); 25 createTrackbar("参数值", WINDOW_NAME, &g_nThresholdValue, 255, on_Threshold); 26 on_Threshold(0, 0); 27 28 waitKey(0); 29 return 0; 30 } 31 32 void on_Threshold(int, void *) { 33 threshold(g_grayImage, g_dstImage, g_nThresholdValue, 255, g_nThresholdType); 34 imshow(WINDOW_NAME, g_dstImage); 35 }