1 ////3.1.4双阈值法二值化操作
2 ////利用OpenCV的threshold函数实现双阈值法二值化操作的源码!
3 ////SourceCode:https://blog.csdn.net/wenhao_ir/article/details/51566817
4 #include "opencv2/imgproc/imgproc.hpp"
5 #include "opencv2/highgui/highgui.hpp"
6 int main()
7 {
8 // 图像读取及判断
9 cv::Mat srcImage = cv::imread("D:\\0604.png");
10 if (!srcImage.data)
11 return 1;
12 // 灰度转换
13 cv::Mat srcGray;
14 cv::cvtColor(srcImage, srcGray, CV_RGB2GRAY);
15 cv::imshow("srcGray", srcGray);
16 // 初始化阈值参数
17 const int maxVal = 255;
18 int low_threshold = 150;
19 int high_threshold = 210;
20 cv::Mat dstTempImage1, dstTempImage2, dstImage;
21 // 小阈值对源灰度图像进行阈值化操作
22 cv::threshold(srcGray, dstTempImage1,
23 low_threshold, maxVal, cv::THRESH_BINARY);
24 // 大阈值对源灰度图像进行阈值化操作
25 cv::threshold(srcGray, dstTempImage2,
26 high_threshold, maxVal, cv::THRESH_BINARY_INV);//要特别注意这里的最后一个参数是INV哦
27 // 矩阵与运算得到二值化结果
28 cv::bitwise_and(dstTempImage1,
29 dstTempImage2, dstImage);
30 cv::imshow("dstImage", dstImage);
31 cv::waitKey(0);
32 return 0;
33 }
![]()