Threshold函数:
函数的调用方式:
复制代码
函数参数详解:
1
2void cvThreshold( const CvArr* src, CvArr* dst, double threshold, double max_value, int threshold_type );
src 原始数组 (单通道 , 8-bit of 32-bit 浮点数).
dst 输出数组,必须与 src 的类型一致,或者为 8-bit. threshold
阈值 max_value 使用 CV_THRESH_BINARY 和 CV_THRESH_BINARY_INV 的最大值.
threshold_type 阈值类型
函数 cvThreshold 对单通道数组应用固定阈值操作。该函数的典型应用是对灰度图像进行阈值操作得到二值图像。(cvCmpS 也可以达到此目的) 或者是去掉噪声,例如过滤很小或很大象素值的图像点。本函数支持的对图像取阈值的方法由 threshold_type 确定:
复制代码
opencv代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19threshold_type=CV_THRESH_BINARY: dst(x,y) = max_value, if src(x,y)>threshold 0, otherwise threshold_type=CV_THRESH_BINARY_INV: dst(x,y) = 0, if src(x,y)>threshold max_value, otherwise threshold_type=CV_THRESH_TRUNC: dst(x,y) = threshold, if src(x,y)>threshold src(x,y), otherwise threshold_type=CV_THRESH_TOZERO: dst(x,y) = src(x,y), if (x,y)>threshold 0, otherwise threshold_type=CV_THRESH_TOZERO_INV: dst(x,y) = 0, if src(x,y)>threshold src(x,y), otherwise
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <iostream> using namespace std; using namespace cv; int main() { Mat img = imread("d:6.jpg"); Mat dst(img.size(),8,1); cvtColor(img, img, CV_BGR2GRAY); threshold(img, img, 100, 255, CV_THRESH_BINARY); /*dilate(img, dst, NULL, Point(-1, -1), 10, BORDER_DEFAULT, Scalar(0, 0, 255));*/ namedWindow("shiyan"); imshow("shiyan", img); waitKey(0); return 0; }
或者:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78#include "opencv2/imgproc/imgproc.hpp" #include "opencv2/highgui/highgui.hpp" #include <stdlib.h> #include <stdio.h> using namespace cv; /// 全局变量定义及赋值 int threshold_value = 0; int threshold_type = 3;; int const max_value = 255; int const max_type = 4; int const max_BINARY_value = 255; Mat src, src_gray, dst; char* window_name = "Threshold Demo"; char* trackbar_type = "Type: n 0: Binary n 1: Binary Inverted n 2: Truncate n 3: To Zero n 4: To Zero Inverted"; char* trackbar_value = "Value"; /// 自定义函数声明 void Threshold_Demo( int, void* ); /** * @主函数 */ int main( int argc, char** argv ) { /// 读取一副图片,不改变图片本身的颜色类型(该读取方式为DOS运行模式) src = imread( argv[1], 1 ); /// 将图片转换成灰度图片 cvtColor( src, src_gray, CV_RGB2GRAY ); /// 创建一个窗口显示图片 namedWindow( window_name, CV_WINDOW_AUTOSIZE ); /// 创建滑动条来控制阈值 createTrackbar( trackbar_type, window_name, &threshold_type, max_type, Threshold_Demo ); createTrackbar( trackbar_value, window_name, &threshold_value, max_value, Threshold_Demo ); /// 初始化自定义的阈值函数 Threshold_Demo( 0, 0 ); /// 等待用户按键。如果是ESC健则退出等待过程。 while(true) { int c; c = waitKey( 20 ); if( (char)c == 27 ) { break; } } } /** * @自定义的阈值函数 */ void Threshold_Demo( int, void* ) { /* 0: 二进制阈值 1: 反二进制阈值 2: 截断阈值 3: 0阈值 4: 反0阈值 */ threshold( src_gray, dst, threshold_value, max_BINARY_value,threshold_type ); imshow( window_name, dst ); }
最后
以上就是舒适奇迹最近收集整理的关于二值化函数Threshold的全部内容,更多相关二值化函数Threshold内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复