在opencv的阈值处理中,有个截断阈值的参数 THRESH_TRUNC 让我有些疑惑。因为从官方资料解释来看,是大于阈值会被设为阈值,小于阈值的保持不变
(DSTI = (SRCI > thresh) ? THRESH : SRCI),且官方解释截图也是如此:
Python版代码如下:
import cv2 img_gray = cv2.imread("cat.jpg", cv2.IMREAD_GRAYSCALE) import matplotlib.pyplot as plt ret, thresh1 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY) ret, thresh2 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY_INV) ret, thresh3 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_TRUNC) ret, thresh4 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_TOZERO) ret, thresh5 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_TOZERO_INV) titles = ['Original Image', 'BINARY', 'BINARY_INV', 'TRUNC', 'TOZERO', 'TOZERO_INV'] images = [img_gray, thresh1, thresh2, thresh3, thresh4, thresh5] for i in range(6): plt.subplot(2, 3, i+1), plt.imshow(images[i], 'gray') plt.title(titles[i]) plt.xticks([]), plt.yticks([]) plt.show()
得到图像对比结果:
奇怪的是,TUNC图中,小于阈值127的没有问题,但高于阈值的并非保持127,却变成了最大值255(可取值验证),如果是保持127,图片高光部分应该都是灰色的。这到底是怎么回事呢?