OpenCV基础——threshold函数的使用
2017-08-10 12:30
204 查看
图像的二值化就是将图像上的像素点的灰度值设置为0或255,这样将使整个图像呈现出明显的黑白效果。在数字图像处理中,二值图像占有非常重要的地位,图像的二值化使图像中数据量大为减少,从而能凸显出目标的轮廓。OpenCV中提供了函数cv::threshold();
注意:作者采用OpenCV 3.0.0
函数原型
![](https://img-blog.csdn.net/20170810122723876?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjU2Njc1MQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
参数说明
src:源图像,可以为8位的灰度图,也可以为32位的彩色图像。(两者由区别)
dst:输出图像
thresh:阈值
maxval:dst图像中最大值
type:阈值类型,可以具体类型如下:
具体如下表
![](https://img-blog.csdn.net/20170810122741046?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjU2Njc1MQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
生成关系如下表
![](https://img-blog.csdn.net/20170810122752738?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjU2Njc1MQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
函数参考可以至http://docs.opencv.org/3.0.0/examples.html
测试代码
测试结果
注意:
如果采用彩色图像进行计算会得到彩色效果,而不是预期的二值化结果
注意:作者采用OpenCV 3.0.0
函数原型
参数说明
src:源图像,可以为8位的灰度图,也可以为32位的彩色图像。(两者由区别)
dst:输出图像
thresh:阈值
maxval:dst图像中最大值
type:阈值类型,可以具体类型如下:
编号 | 阈值类型枚举 | 注意 |
1 | THRESH_BINARY | |
2 | THRESH_BINARY_INV | |
3 | THRESH_TRUNC | |
4 | THRESH_TOZERO | |
5 | THRESH_TOZERO_INV | |
6 | THRESH_MASK | 不支持 |
7 | THRESH_OTSU | 不支持32位 |
8 | THRESH_TRIANGLE | 不支持32位 |
生成关系如下表
函数参考可以至http://docs.opencv.org/3.0.0/examples.html
测试代码
Mat gray; cvtColor(src, gray, CV_BGR2GRAY); // 全局二值化 int th = 100; cv::Mat threshold1,threshold2,threshold3,threshold4,threshold5,threshold6,threshold7,threshold8; cv::threshold(gray, threshold1, th, 255, THRESH_BINARY); cv::threshold(gray, threshold2, th, 255, THRESH_BINARY_INV); cv::threshold(gray, threshold3, th, 255, THRESH_TRUNC); cv::threshold(gray, threshold4, th, 255, THRESH_TOZERO); cv::threshold(gray, threshold5, th, 255, THRESH_TOZERO_INV); //cv::threshold(gray, threshold6, th, 255, THRESH_MASK); cv::threshold(gray, threshold7, th, 255, THRESH_OTSU); cv::threshold(gray, threshold8, th, 255, THRESH_TRIANGLE); cv::imshow("THRESH_BINARY", threshold1); cv::imshow("THRESH_BINARY_INV", threshold2); cv::imshow("THRESH_TRUNC", threshold3); cv::imshow("THRESH_TOZERO", threshold4); cv::imshow("THRESH_TOZERO_INV", threshold5); //cv::imshow("THRESH_MASK", threshold6); cv::imshow("THRESH_OTSU", threshold7); cv::imshow("THRESH_TRIANGLE", threshold8); cv::waitKey(0); |
原图 | |
THRESH_BINARY | |
THRESH_BINARY_INV | |
THRESH_TRUNC | |
THRESH_TOZERO | |
THRESH_TOZERO_INV | |
THRESH_OTSU | |
THRESH_TRIANGLE |
如果采用彩色图像进行计算会得到彩色效果,而不是预期的二值化结果
彩色源图 | 灰度源图 |
相关文章推荐
- 基础学习笔记之opencv(24):imwrite函数的使用
- 基础学习笔记之opencv(18):kmeans函数使用实例
- 基础学习笔记之opencv(24):imwrite函数的使用
- 基础学习笔记之opencv(18):kmeans函数使用实例
- 基础学习笔记之opencv(24):imwrite函数的使用
- 基础学习笔记之opencv(24):imwrite函数的使用
- 基础学习笔记之opencv(24):imwrite函数的使用
- 基础学习笔记之opencv(24):imwrite函数的使用
- 使用OpenCv的cvMinAreaRect2函数获取轮廓的可倾斜最小矩形区域
- 【 Makefile 编程基础之四】详解Makefile 函数的语法与使用!
- opencv中c/c++风格函数使用说明
- 基础学习笔记之opencv(4):直方图使用学习
- Node.js高级编程:用JavaScript构建可伸缩应用(6)2.6 核心API基础-使用计时器制定函数的执行计划
- 基础学习笔记之opencv(7):ubuntu下opencv在Qt中的使用
- 基础学习笔记之opencv(17):皮肤检测类CvAdaptiveSkinDetector的使用
- opencv 中的 cvPutText() 函数的使用
- 嵌入式开发之C基础学习笔记05--模块化设计和函数的使用
- vc MFC使用OPenCV常用函数搜集
- 互操作性——使用C/C++类型的非托管函数基础
- 基础备忘:函数模板在数组中的使用