threshold
图像的二值化就是将图像上的像素点的灰度值设置为0或255,这样将使整个图像呈现出明显的黑白效果。在数字图像处理中,二值图像占有非常重要的地位,图像的二值化使图像中数据量大为减少,从而能凸显出目标的轮廓。OpenCV中提供了函数cv::threshold();
注意:作者采用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 |
注意:
如果采用彩色图像进行计算会得到彩色效果,而不是预期的二值化结果
彩色源图 |
灰度源图 |
相关阅读
1.文件偏移 通常调用read或write每读写一个文件,就会改变文件的读写位置。在linux中同样可以使用lseek函数来修改文件偏移量,即读
greatest (max(one),max(two),max(three))求多列的最大值,oracle中的greatest 函数 已知表TB的数据如下 SQL> select * from tb;
Python product函数介绍 product(A,B)函数,返回A和B中的元素组成的笛卡尔积的元组,具体见如下代码:import itertools for item
Pattern.compile函数:Pattern Pattern.compile(String regex, int flag)flag的取值范围如下:Pattern.CANON_EQ,当且仅当两个字符的"
析构函数:在类中声明的一种成员函数①析构函数与类名同名②析构函数无参(不可重载)③表示: ~类名() { 析构函数体; }