使用 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 次瀏覽

開啟您的 職業

完成該課程即可取得認證

開始使用
廣告