Python OpenCV 影像二值化 Image Thresholding

本篇介紹 Python OpenCV 的 cv2.threshold 來作影像二值化 Image Thresholding,在寫 Python 影像處理程式時常會用到 OpenCV cv2.threshold 門檻二值化的功能,接下來介紹怎麼使用 Python 搭配 OpenCV 模組來進行影像二值化 Image Thresholding。

範例. 簡單的二值化

影像二值化簡單的方法,如果像素值pixel大於門檻值threshold,就指定一個新數值(例如白色),否則就指定另一個新數值(例如黑色),
這邊使用 opencv 的 cv2.threshold,第一個參數來源需要是灰階影像,第二個參數是用來對像素值進行分類的門檻值,第三個參數為最大灰階值,第四個參數為二值化的類型如下所示:
cv2.THRESH_BINARY:Threshold Binary,即二值化,將大於門檻值的灰階值設為最大灰階值,小於門檻值的值設為0。
cv2.THRESH_BINARY_INV:Threshold Binary, Inverted,將大於門檻值的灰階值設為0,其他值設為最大灰階值。
cv2.THRESH_TRUNC:Truncate,將大於門檻值的灰階值設為門檻值,小於門檻值的值保持不變。
cv2.THRESH_TOZERO:Threshold to Zero,將小於門檻值的灰階值設為0,大於門檻值的值保持不變。
cv2.THRESH_TOZERO_INV:Threshold to Zero, Inverted,將大於門檻值的灰階值設為0,小於門檻值的值保持不變。
這部份可以參考這張圖 https://docs.opencv.org/2.4/_images/threshold.png

回傳值有兩個,第一個為回傳,第二個為回傳的已二值化的影像。

以下展示這五種二值化類型的結果,詳細程式碼如下:

opencv-threshold.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('gradient.png', 0)
ret, th1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
ret, th2 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV)
ret, th3 = cv2.threshold(img, 127, 255, cv2.THRESH_TRUNC)
ret, th4 = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO)
ret, th5 = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO_INV)

titles = ['Original Image', 'BINARY', 'BINARY_INV', 'TRUNC', 'TOZERO', 'TOZERO_INV']
images = [img, th1, th2, th3, th4, th5]

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()

結果如下圖所示:

cv2.threshold 參數的詳細細節請參考這裡

範例. 自適應二值化

opencv-adaptiveThreshold.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('sudoku.png', 0)
img = cv2.medianBlur(img, 5)

ret, th1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
th2 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C,\
cv2.THRESH_BINARY, 11, 2)
th3 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
cv2.THRESH_BINARY, 11, 2)

titles = ['Original Image', 'Global Thresholding (v = 127)',
'Adaptive Mean Thresholding', 'Adaptive Gaussian Thresholding']
images = [img, th1, th2, th3]

for i in range(4):
plt.subplot(2, 2, i+1), plt.imshow(images[i], 'gray')
plt.title(titles[i])
plt.xticks([]), plt.yticks([])
plt.show()

結果如下圖所示:

cv2.adaptiveThreshold 參數的詳細細節請參考這裡

參考
Image Thresholding — OpenCV-Python Tutorials 1 documentation
https://opencv24-python-tutorials.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_thresholding/py_thresholding.html#thresholding
OpenCV: Image Thresholding
https://docs.opencv.org/master/d7/d4d/tutorial_py_thresholding.html
Python下opencv使用笔记(四)(图像的阈值处理)Python我爱智能-CSDN博客
https://blog.csdn.net/on2way/article/details/46812121

其它相關文章推薦
Python OpenCV 影像平滑模糊化 blur
Python OpenCV 彩色轉灰階(RGB/BGR to GRAY)
Python OpenCV 彩色轉HSV(RGB/BGR to HSV)
Python OpenCV 彩色轉YCbCr(RGB/BGR to YCbCr)
Python OpenCV 灰階轉彩色(Gray to RGB/BGR)
Python OpenCV 影像邊緣偵測 Canny Edge Detection
Python OpenCV resize 圖片縮放
小專案 Python OpenCV 圖片轉字元圖畫