使用 OpenCV 对图像执行截断阈值化


在本程序中,我们将使用 openCV 对图像执行截断阈值化。阈值化是一个处理,每个像素的值都将相对于阈值而改变。

如果像素小于阈值就赋予它特定值,如果像素大于阈值就赋予它其他值。在截断阈值化中,大于阈值的值被减少到阈值。所有其他像素保持不变。

原始图像

算法

Step 1: Import cv2.
Step 2: Define threshold and max_val.
Step 3: Pass these parameters in the cv2.threshold value and specify the type of thresholding you want to do.
Step 4: Display the output.

示例代码

import cv2

image = cv2.imread('testimage.jpg')
threshold_value = 120
max_val = 255
ret, image = cv2.threshold(image, threshold_value, max_val, cv2.THRESH_TRUNC)
cv2.imshow('TruncateThresholding', image)

输出

更新于:17-3-2021

772 次浏览

开启您的 职业

完成该课程即可取得认证

开始使用
广告
© . All rights reserved.