1 ////Source Code:https://blog.csdn.net/gone_huilin/article/details/53222752
2 #include "opencv2/imgproc/imgproc.hpp"
3 #include "opencv2/highgui/highgui.hpp"
4 int main()
5 {
6 // 读取源图像及判断
7 cv::Mat srcImage = cv::imread("D:\\0604.png");//注意路径中冒号是英文!
8 if (!srcImage.data)
9 return 1;
10 // 转化为灰度图像
11 cv::Mat srcGray;
12 cv::cvtColor(srcImage, srcGray, CV_RGB2GRAY);
13 cv::imshow("srcGray", srcGray);
14 cv::Mat dstImage;
15 // 初始化阈值参数
16 int thresh = 50;
17 // 初始化阈值化处理的类型
18 /* 0: 二进制阈值 1: 反二进制阈值 2: 截断阈值
19 3: 0阈值 4: 反0阈值*/
20 int threshType = 0;
21 // 预设最大值
22 const int maxVal = 255;
23 // 固定阈值化操作
24 cv::threshold(srcGray, dstImage, thresh,
25 maxVal, threshType);
26 cv::imshow("dstImage", dstImage);
27 cv::waitKey(0);
28 return 0;
29 }
![]()