threshold函数作用:
去掉噪,例如过滤很小或很大像素值的图像点。
threshold函数python版原型:
retval, dst = cv.threshold( src, thresh, maxval, type[, dst] )
参数说明:
src:原图像。
dst:结果图像。
thresh:当前阈值。
maxVal:最大阈值,一般为255.
thresholdType:阈值类型,主要有下面几种:
enum ThresholdTypes {
THRESH_BINARY = 0,
THRESH_BINARY_INV = 1,
THRESH_TRUNC = 2,
THRESH_TOZERO = 3,
THRESH_TOZERO_INV = 4,
THRESH_MASK = 7,
THRESH_OTSU = 8,
THRESH_TRIANGLE = 16
};
返回值:
retval: 与参数thresh一致
dst: 结果图像
编号 阈值类型枚举
注意
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位
具体说明如下:
注意:
THRESH_OTSU和THRESH_TRIANGLE是作为优化算法配合THRESH_BINARY、THRESH_BINARY_INV、THRESH_TRUNC、THRESH_TOZERO以及THRESH_TOZERO_INV来使用的。
当使用了THRESH_OTSU和THRESH_TRIANGLE两个标志时,输入图像必须为单通道。
生成关系:
下面通过代码来验证一下threshold的用法:
import numpy as np
import cv2
#创建一个6x6的BGR三通道图片
img6x6 = np.zeros((6,6,3), dtype=np.uint8)
#Blue通道取值0~99随机值
img6x6[:,:,0] = np.random.randint(0, 100, size=36).reshape(6,6)
#Green通道取值100~199随机值
img6x6[:,:,1] = np.random.randint(100, 200, size=36).reshape(6,6)
#Red通道取值200~255随机值
img6x6[:,:,2] = np.random.randint(200, 256, size=36).reshape(6,6)
print(img6x6)
得到img6x6的BGR三通道数据:
[[[ 62 171 226]
[ 29 118 212]
[ 61 151 200]
[ 43 123 206]
[ 58 146 232]
[ 2 124 246]]
[[ 48 101 207]
[ 32 194 228]
[ 90 171 241]
[ 36 152 244]
[ 64 169 245]
[ 71 106 202]]
[[ 27 198 206]
[ 39 193 213]
[ 60 128 207]
[ 53 134 248]
[ 25 187 229]
[ 72 116 229]]
[[ 52 163 201]
[ 39 189 251]
[ 63 190 217]
[ 73 139 212]
[ 54 166 236]
[ 43 191 200]]
[[ 34 119 207]
[ 36 110 209]
[ 40 191 229]
[ 60 100 228]
[ 62 174 250]
[ 41 115 209]]
[[ 35 165 246]
[ 50 182 218]
[ 77 110 239]
[ 44 106 245]
[ 21 189 252]
[ 64 144 241]]]
观察结果我们发现img6x6[2][2][1]=128,我们取该值作为thresh值来演示THRESH_BINARY、THRESH_BINARY_INV等的作用。
1) THRESH_BINARY
我们取参数值thresh=128,maxval=255,flag = THRESH_BINARY
cv2.threshold(img6x6, 128, 255, cv2.THRESH_BINARY)
根据前面对THRESH_BINARY flag值的解释:
预期得到的结果中,img6x6中>128的值均应变为255,<=128的值均应变为0。
我们实际得到的结果为:
[[[ 0 255 255]
[ 0 0 255]
[ 0 255 255]
[ 0 0 255]
[ 0 255 255]
[ 0 0 255]]
[[ 0 0 255]
[ 0 255 255]
[ 0 255 255]
[ 0 255 255]
[ 0 255 255]
[ 0 0 255]]
[[ 0 255 255]
[ 0 255 255]
[ 0 0 255]
[ 0 255 255]
[ 0 255 255]
[ 0 0 255]]
[[ 0 255 255]
[ 0 255 255]
[ 0 255 255]
[ 0 255 255]
[ 0 255 255]
[ 0 255 255]]
[[ 0 0 255]
[ 0 0 255]
[ 0 255 255]
[ 0 0 255]
[ 0 255 255]
[ 0 0 255]]
[[ 0 255 255]
[ 0 255 255]
[ 0 0 255]
[ 0 0 255]
[ 0 255 255]
[ 0 255 255]]]
与预期的结果一致。
2)THRESH_BINARY_INV
THRESH_BINARY_INV的作用与THRESH_BINARY相反,大于thresh的值置0,小于等于thresh的值置maxval:
预期的结果与1)的结果完全相反。
code:
-
ret,dst=cv2.threshold(img6x6, 128, 255, cv2.THRESH_BINARY_INV)
-
print(dst)
[[[255 0 0]
[255 255 0]
[255 0 0]
[255 255 0]
[255 0 0]
[255 255 0]]
[[255 255 0]
[255 0 0]
[255 0 0]
[255 0 0]
[255 0 0]
[255 255 0]]
[[255 0 0]
[255 0 0]
[255 255 0]
[255 0 0]
[255 0 0]
[255 255 0]]
[[255 0 0]
[255 0 0]
[255 0 0]
[255 0 0]
[255 0 0]
[255 0 0]]
[[255 255 0]
[255 255 0]
[255 0 0]
[255 255 0]
[255 0 0]
[255 255 0]]
[[255 0 0]
[255 0 0]
[255 255 0]
[255 255 0]
[255 0 0]
[255 0 0]]]