C/C++教程

关于opencv中 THRESH_TRUNC 参数的疑惑

本文主要是介绍关于opencv中 THRESH_TRUNC 参数的疑惑,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

在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,图片高光部分应该都是灰色的。这到底是怎么回事呢?



 

这篇关于关于opencv中 THRESH_TRUNC 参数的疑惑的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!